Humm, after testing i got it wrong, initialize the variable to null if you want it to work...
IPEndPoint ipEP = null;
System.Net.Sockets.Socket udpSocket = null;
try
{...}
>Del and Stephane,
>Correct for a trivial example like an
int I can just initialize it, but my original problem is for a
Socket>
>Socket udpSocket = new Socket(ipEP.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
>
>and if I initialize the Socket before the try block I am SOL if the initilazation of the Socket throws an error.
>
>I hope this makes sense.
>
>Einar
>
>>I've run into the same thing with if/else constructs. Initializing the variable with a value seems to resolve it;
>>
>>int myInt = 0;
>>
>>>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)
>>>> {...}
>>>>}
>>>>
>>>>