Link to home
Create AccountLog in
Avatar of ianinspain
ianinspain

asked on

500pts: var cannot be declared in this scope because it would give a different meaning??

Hi there,

Got a bit of strange one here and wondered if anybody can help, i have some code that creates an object and right after i destroy it. (set xxx = null) then further down i try to use the same varble name to create a  new object and i get the following error

C:\Documents and Settings\igregson\My Documents\Visual Studio Projects\Tests\frmMainForm.cs(1747): A local variable named 'cmdParser' cannot be declared in this scope because it would give a different meaning to 'cmdParser', which is already used in a 'parent or current' scope to denote something else

if anybody can help with this ---- i would really appreciate it... Thanks in advance

Here is the code:

      // Now lets look for the NoUpdate argument
                  CommandLineParser      cmdParser = new CommandLineParser      ( args );
                  if( cmdParser.check( "-nu" ) == true )
                  {
                        _doNotUpdate = true; // set var for NOT updating
                  }
                  else
                  {
                        _doNotUpdate = false; // set var for checking to update
                  }

                  cmdParser = null; // I RELEASE OBJECT HERE

                  
            
                  if( args.Length == 5 )
                  {
                        CommandLineParser      cmdParser = new CommandLineParser      ( args ); // THIS LINE ERRORS


ASKER CERTIFIED SOLUTION
Avatar of adathelad
adathelad
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of ianinspain
ianinspain

ASKER

Thanks for your comments, yep this is great... But will the IDE not let you set it to null and then re-declare fully?

Ian
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
>> But will the IDE not let you set it to null and then re-declare fully?
No as you would then be trying to declare 2 variables with the same name within the same scope which is not allowed.
If it allowed it, it would cause no end of problems.e

e.g.
int myVar = 10;
string myVar = "Ten";

If it allowed you to do what you are trying to do, then the above would be also valid and you can see that this is wrong. myVar can't be an integer and a string at the same time.
Thanks all!! Much appreciated

I presumed i could do what i was doing as i set it to null but the compiler is obviously catching it...

Thanks again!

Ian
No problem.

I am confused as to why you awarded points to issamtaher as well though - his post merely repeated my answer, over an hour after I posted mine. Generally it's accepted if the same answer is given in quick succession by different experts then split the points,  but as his was after such a long delay, it could be seen as hijacking points ;)