Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
The process cannot access the file
Message
 
À
01/10/2008 09:36:06
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 8.0
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
Visual FoxPro
Divers
Thread ID:
01351711
Message ID:
01352011
Vues:
22
>>>>Hi Paul,
>>>>
>>>>>Also, I'd suggest putting the Close() in a Finally section of a Try/Catch.
>>>>
>>>>Why do I get a "Use of unassigned local variable "stream" in the finally section.
>>>>
>>
>>It's because of this line:
>>
>>FileStream stream;
>>
>>You haven't assigned anything to it, so the compiler is complaining about it (it doesn't try to determine if it's assigned further down).
>>
>>Change it to:
>>
>>FileStream stream = null;
>
>Hmm, originally I had:
>FileStream stream = File.Create(xmlFileName); Inside the try along with the stream.Close and I had to put stream = null in the finally.

Yeah, variables are scoped to the enclosing curly braces, so stream isn't in scope in the finally if it was created within in try block.

>Adding FileStream stream = null before the try does work. I am surprised since the initiialization was the first item in the try nothing could trip the catch except the stream. I guess we can't expect the compiler to know everything.

It's kind of annoying that the compiler doesn't catch the assignment further down in code (so the = null isn't necessary), but I'm sure there is some perfectly reasonable sounding explanation for it that I can't see. At least that's what I keep telling myself. I told myself the same thing when first learning C# about why the variable type was required before a variable name when doing a new:
MyObjType sample = new MyObjType();
Let's see, I'm doing a new MyObjType() - what else besides MyObjType() could be created that makes it necessary for me to specify the type of the variable?! Yeah, we now have the "var" keyword, but it's still not really necessary. The types can be inferred pretty easily.

OK, enough complaining <g>
-Paul

RCS Solutions, Inc.
Blog
Twitter
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform