Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Finally code
Message
 
À
21/12/2004 11:04:19
Del Despain
Colorado Plateau Associates
Hurricane, Utah, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Divers
Thread ID:
00970841
Message ID:
00971023
Vues:
16
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)
>>>    {...}
>>>}
>>>
>>>
Semper ubi sub ubi.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform