Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to increase the scope in an if statement [modified]

How to increase the scope in an if statement [modified]

Scheduled Pinned Locked Moved C#
csstutorial
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nathan Revka
    wrote on last edited by
    #1

    I need to increase the scope of double fee and total. The following code doesn't work because fee and total are out of the scope on the writeline.

    if (time <= 3)
    {
    fee = 2;
    total = total + fee;
    }

    I have more if statements involving fee and total but they all will be applied to the following writeline

    Console.WriteLine("Current charge: {0:c}, Total receipts: {1:c}", fee, total);

    Edit: I made fee and total a static variable above the main method and now it works even though I set the new value of fee and total inside a block with less scope. I am not sure how this works, but it works, maybe someone can tell me why it works.

    modified on Saturday, July 11, 2009 7:11 PM

    L L D 3 Replies Last reply
    0
    • N Nathan Revka

      I need to increase the scope of double fee and total. The following code doesn't work because fee and total are out of the scope on the writeline.

      if (time <= 3)
      {
      fee = 2;
      total = total + fee;
      }

      I have more if statements involving fee and total but they all will be applied to the following writeline

      Console.WriteLine("Current charge: {0:c}, Total receipts: {1:c}", fee, total);

      Edit: I made fee and total a static variable above the main method and now it works even though I set the new value of fee and total inside a block with less scope. I am not sure how this works, but it works, maybe someone can tell me why it works.

      modified on Saturday, July 11, 2009 7:11 PM

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Nathan Revka wrote:

      fee and total are out of the scope on the writeline.

      They aren't, at least not in the code that you showed.

      1 Reply Last reply
      0
      • N Nathan Revka

        I need to increase the scope of double fee and total. The following code doesn't work because fee and total are out of the scope on the writeline.

        if (time <= 3)
        {
        fee = 2;
        total = total + fee;
        }

        I have more if statements involving fee and total but they all will be applied to the following writeline

        Console.WriteLine("Current charge: {0:c}, Total receipts: {1:c}", fee, total);

        Edit: I made fee and total a static variable above the main method and now it works even though I set the new value of fee and total inside a block with less scope. I am not sure how this works, but it works, maybe someone can tell me why it works.

        modified on Saturday, July 11, 2009 7:11 PM

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, I'm not sure what you are doing, but it could be very wrong.

        public void someMethod1() {
        if (time <= 3) {
        int fee = 2;
        }
        Console.WriteLine("Fee: {0:c}", fee);
        }

        does not compile since fee does not exist outside the if block.

        public void someMethod1() {
        int fee = 0;
        if (time <= 3) {
        int fee = 2;
        }
        Console.WriteLine("Fee: {0:c}", fee);
        }

        does compile since fee exists outside the if block AND is guaranteed to hold a value.

        int fee; // or static int fee;
        public void someMethod1() {
        fee=0;
        if (time <= 3) {
        fee = 2;
        }
        Console.WriteLine("Fee: {0:c}", fee);
        }
        public void someMethod2() {
        fee=0;
        if (time <= 2223) {
        fee = 2222;
        }
        Console.WriteLine("Fee: {0:c}", fee);
        }

        is probably not OK since both methods share a variable they did not intend to share. Both methods could run at the same time (using threads) and confusing each other. If the data link between those methods is not intended, the methods really should use local variables. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        1 Reply Last reply
        0
        • N Nathan Revka

          I need to increase the scope of double fee and total. The following code doesn't work because fee and total are out of the scope on the writeline.

          if (time <= 3)
          {
          fee = 2;
          total = total + fee;
          }

          I have more if statements involving fee and total but they all will be applied to the following writeline

          Console.WriteLine("Current charge: {0:c}, Total receipts: {1:c}", fee, total);

          Edit: I made fee and total a static variable above the main method and now it works even though I set the new value of fee and total inside a block with less scope. I am not sure how this works, but it works, maybe someone can tell me why it works.

          modified on Saturday, July 11, 2009 7:11 PM

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          Nathan Revka wrote:

          I made fee and total a static variable above the main method

          Unless you have a particular need for a field to be accessible globaly and not tied to an instance, I would not have it as static. You mention your Main method, is this a console app? If so, create a seperate class and put stuff in that, and instanciate that class within the main method.

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups