Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Looking to resolve a warning
Message
From
16/12/2005 19:54:54
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
 
 
To
16/12/2005 18:17:55
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01078852
Message ID:
01078874
Views:
8
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)
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform