Bug or OOB ???

  • Thread starter Thread starter Tamir Khason
  • Start date Start date
T

Tamir Khason

Strange problem:
In C# compiler detects that some code in case constraint is unreacheble, BUT
it do not detect redefined variables which can not be executed together.
(BTW: in VB.NET - this works perfect !!! )
IDEAS?
See there:
public static IDataHelper GetDataHelper(DataHelperType helperType)

{

switch (helperType)

{

case DataHelperType.OleDB:

SQLDataHelper helper = new SQLDataHelper();

return helper;

//break; //Compiler detects that the code is unreachable!!!

case DataHelperType.SQLServer:

OleDBDataHelper helper = new OleDBDataHelper(); //Compiler return error " A
local variable named 'helper' is already defined in this scope"


return helper;

//break;

}

}
 
Tamir Khason said:
Strange problem:
In C# compiler detects that some code in case constraint is unreacheble, BUT
it do not detect redefined variables which can not be executed together.

It's correct. Even though the code is unreachable, the variable's scope
is still the same.

Easy to fix though:

case DataHelperType.OleDB:
{
SQLDataHelper helper = new SQLDataHelper();
return helper;
}
case DataHelperType.SQLServer:
{
OleDBDataHelper helper = new OleDBDataHelper();
return helper;
}

(By the way, I suspect you have something else wrong in your code -
shouldn't these cases be the other way round?)

Another, simpler alternative:

case DataHelperType.OleDB:
return new SQLDataHelper();

case DataHelperType.SQLServer:
return new OleDBDataHelper();

There's no real need for the local variables at all.
 
Hi Tamir,

Is your problem resolved?
If you still have anything unclear, please feel free to tell me.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thank you for response
I did it without local variables and now it is OK

Thank you again
 
Back
Top