Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Looking to resolve a warning
Message
De
16/12/2005 19:54:54
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivie
 
 
À
16/12/2005 18:17:55
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01078852
Message ID:
01078874
Vues:
9
This message has been marked as the solution to the initial question of the thread.
While I am not sure how to solve it, let me comment on why the error appears. The FOR EACH is executed once for each file, but there is no guarantee that there is any. The TRY part might have other instructions, outside the FOR EACH loop, that may trigger the CATCH part. In your code, there happen to be no more commands, but the compiler probably doesn't notice that.

One possibility would be to put the TRY/CATCH within the FOR EACH loop, instead of the other way round, but you must decide for yourself whether that fits into your logic.

(BTW, I believe that C# is much stricter with the "definite assignment rule", and the equivalent of your code would not compile at all.)

HTH,

Hilmar.

>Since I have upgraded to VS 2005, I have a warning with the following code:
>
>
>        Dim loDirectory As New DirectoryInfo(Input.Text)
>        Dim loFileList As FileInfo() = loDirectory.GetFiles("*.xml")
>        Dim loFile As FileInfo
>
>        Try
>
>            ' Get all the files in this one
>            For Each loFile In loFileList
>                lcFile = Input.Text + "\" + loFile.Name
>            Next
>
>        Catch loError As Exception
>            lnEnd = Date.Now.Ticks
>            lcHTMLLocal = ""
>            lcHTMLLocal = lcHTMLLocal + "<TR VALIGN=TOP>"
>            lcHTMLLocal = lcHTMLLocal + "<TD>Sending " + Input.Text + "\" + loFile.Name
>        End Try
>
>
>The message is "Variable 'loFile' is used before it has been assigned a value. A null reference exception could result at runtime".
>
>This is in regards to the last line in the Catch. However, in this case, loFile is not initialized in the Catch because it is only initialized during the For Next which is in the Try. So, how can I adjust this code to avoid the warning?
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform