Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 12th, 2005, 7:06 PM   #1
Arla
Hobbyist Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 227
Rep Power: 4 Arla is on a distinguished road
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);
And I get a compile error, since J seems to only be valid inside of the If statement? Is that really true, do you have to create this as

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).
Arla is offline   Reply With Quote
Old May 12th, 2005, 10:45 PM   #2
new User(Kevin.Parkinson)
Programmer
 
new User(Kevin.Parkinson)'s Avatar
 
Join Date: Mar 2005
Location: Edmonton, Alberta, Canada
Posts: 37
Rep Power: 0 new User(Kevin.Parkinson) is on a distinguished road
Send a message via MSN to new User(Kevin.Parkinson)
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.
new User(Kevin.Parkinson) is offline   Reply With Quote
Old May 13th, 2005, 11:50 AM   #3
Arla
Hobbyist Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 227
Rep Power: 4 Arla is on a distinguished road
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!
Arla is offline   Reply With Quote
Old May 13th, 2005, 1:08 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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);
}
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 16th, 2005, 9:11 AM   #5
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old May 16th, 2005, 12:12 PM   #6
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
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?
spydoor is offline   Reply With Quote
Old May 16th, 2005, 1:04 PM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
C is a good example. You also can't declare variables within if/for/while/do-while statements in C.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 16th, 2005, 1:20 PM   #8
HeX
Programmer
 
HeX's Avatar
 
Join Date: May 2005
Location: Kosova
Posts: 94
Rep Power: 4 HeX is on a distinguished road
Send a message via MSN to HeX
It's the same in Java
__________________
countdown++;
HeX is offline   Reply With Quote
Old May 17th, 2005, 3:18 AM   #9
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
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
Berto is offline   Reply With Quote
Old May 21st, 2005, 8:48 PM   #10
HeavyMetal
Newbie
 
Join Date: May 2005
Posts: 5
Rep Power: 0 HeavyMetal is on a distinguished road
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
HeavyMetal is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:00 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC