RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

IF sucks ?

Nova

Wanderer
IF sucks ?

Hi,
I have this problem with the if command
[code:1]if(value == 1)
{
Item item = new Item( 0x203C );
}
if(value == 2)
{
Item item = new Item( 0x203D );
}
[/code:1]
and so on

The problem is:
[code:1]A local Variable named 'item' cannot be declared in this scope because it would give a different meaning to 'item', which is already used in a 'parent or current' scope to denote something else[/code:1]

Anybody knows what I can do ?
 
R

RedLineR

Guest
Doesn't C# have a Case argument? If it does that would be a much cleaner way to code it... */me goes to the manual to check*
 

p3nt4x0r

Wanderer
you have to name the item something like MyItem, otherwise it ist taken as type identifier.. ;)

There is a select statement!
[code:1]
switch ( blabla ) {
case 1:
break;
case 2:
break;
}
[/code:1]
 

Nova

Wanderer
thnx that works now
but now i have a new problem:

I have a function:
[code:1]
public void MyFunction( int thisvar, NetState state )
{
blablubb
}
[/code:1]

[code:1]
switch( Value )
{
case 2: { int newvar = 5; }
case 3: { int newvar = 23; }
}
MyFunction( newvar, from );
[/code:1]

From is defendied a bit above, thats no problem
But with the newvar he got say that 'newvar' is not defenied
What can I do here ?
 
H

Hotdog

Guest
I don't have a compiler on this machine to check the syntax but you should be able to do this.

[code:1]
int newvar;
switch( Value )
{
case 2: { newvar = 5; }
case 3: { newvar = 23; }
}
MyFunction( newvar, from );
[/code:1]
 

Falkarma

Wanderer
Your first problem dont work because de local varible exist only in the scope of your { }

You can't do:

...
if ( GetBlabla() )
{
int iWrong = 20;
}
return iWrong;
...

Because iWrong exist only in between the { and }

so you should do

...
int iRight = 0;
if (GetBlabla() )
{
iRight = 20
}
returm iRight;
...

Its the same thing in "case", you must not declare local variable inside the { and } (execpt if you only use them there.

Falk
 
Top