How to increase the scope in an if statement [modified]
-
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
-
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
-
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
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.
-
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
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)