Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Finally code
Message
General information
Forum:
ASP.NET
Category:
Other
Title:
Miscellaneous
Thread ID:
00970841
Message ID:
00971014
Views:
15
Yes, the compiler complains because i would be possible the variable could never be assigned to, in your sample code, it obvious it is assigned to but sometime the compiler is not bright enough...

just assign a default to the variable, int i = 0; is'different for value type like int because you can't test for null. Reference type don't have this limitation.


>I tried what you sugested, but I got a compile error that stated that myObject had not been initialized I found that very odd because that is what I want to check for in the finally block.
>
>Here is a little console app I wrote to ilustrate:
>
>int myInt;
>try
>{
>	myInt = 1;
>}
>finally
>{
>	if (myInt != 1)
>	{
>		myInt = -4;
>	}
>}
>Console.WriteLine(myInt.ToString());
>
>
>This gives me the following error:
>use of unassigned local variable myInt
>in line 10 (if (myInt != 1))
>
>Any clue?
>
>Einar
>
>
>
>>2. You get an exception because your variable is out of scoop in the finally block. Declare if before the try:
>>
>>
>>Object myObject;
>>try
>>{
>>     myObject = new SomeThing();
>>...
>>}
>>finally
>>{
>>    if (myObject !=null)
>>    {...}
>>}
>>
>>
Previous
Reply
Map
View

Click here to load this message in the networking platform