![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 227
Rep Power: 4
![]() |
Code annoyance, variables not available outside of if statements?
So I have the following code
if (i==1)
Long J = 10;
else
Long J = 20;
System.Console.Writeline("J is {0}",J);
Long J;
if (i==1)
J = 10;
else
J = 20;
System.Console.Writeline("J is {0}",J);(I know it's a trivial example, but I'm trying to understand if this is something I'm doing wrong, or really the way it's supposed to work). |
|
|
|
|
|
#2 |
|
Programmer
|
Arla:
I would generally follow the principle that if something is only declared within curly braces, for example: if( thisIsTrue )
{
int a = 9;
}
else
{
int a = 10;
}...then that variable( in this case int a ) will go out of scope and thus not be available outside of those curly braces. so, to answer your question: Long J;
if (i==1)
J = 10;
else
J = 20;
System.Console.Writeline("J is {0}",J);...as you said yourself, this will work for you, and is the right way to use J!
__________________
public class MySignature extends Post implements JavaSyntax throws NuttinHoney{
MySignature()
{
Salutation();
Name();
}
public string Salutation()
{
Screen.printLn( "Sincerely,\n" );
}
public string Name()
{
Screen.printLn( "Kevin Parkinson" );
}
}Last edited by new User(Kevin.Parkinson); May 12th, 2005 at 10:55 PM. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 227
Rep Power: 4
![]() |
Okay, just wanted to check, seems a bit odd compared to other languages I've worked with, but then again, they all have there oddities right!
|
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
It's so you can declare a variable in, for example, a loop, and declare it again in another one. For example:
for (int i = 0; i < 10; i++)
{
Console.WriteLine ("{0}\n", i);
}
for (float i = 0; i < 1; i += 0.05)
{
Console.WriteLine ("{0}\n", i);
} |
|
|
|
|
|
#5 |
|
Programming Guru
![]() ![]() ![]() |
I also had to jump this hurdle when I was first exposed to C#.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
hmm, my experience is this is usually true.
a variable declared within a block is only usable within that block. I'm just curious.. what langauges don't behave this way? |
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
C is a good example. You also can't declare variables within if/for/while/do-while statements in C.
|
|
|
|
|
|
#8 |
|
Programmer
|
It's the same in Java
__________________
countdown++; |
|
|
|
|
|
#9 |
|
Programming Guru
![]() |
Yeah i learned all this from java, then went to C and realised i cant initilise a variable in the for loop brackets :/,
Just look up scope of variables on good ole' google to help you out a bit more.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
|
|
#10 |
|
Newbie
Join Date: May 2005
Posts: 5
Rep Power: 0
![]() |
c'mon people, it's called a scope, it's like a vision thing, what you declare in a function, or a statement,stays on the inside. it's one of the basics of C#
__________________
The Code Lord
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|