Variable is use before it has been assigned a value (What about Multidimetional Arrays)?  
Author Message
ECE





PostPosted: Visual Basic Express Edition, Variable is use before it has been assigned a value (What about Multidimetional Arrays)? Top

This is anoing is just too much to correct
I know that this makes it work
Dim MyVar as String = ""

But shoulndn't the fact that it's just been decalred makes it nothing

I guess I can live with it But what about My ARRAYS
I have TYPES that Contain many Variables

Public
Structure MyStructure
Dim PPDate As String
Dim Track As String
Dim TrackCode As String
Dim RaceNumber As String
Dim RaceCode As String 'A
Dim Purse As String
Dim Conditions As String
Dim RaceType As String
Dim Distance As String
Dim Time1 As String
Dim Time2 As String
Dim Time3 As String
Dim Time4 As String
D
im PostPos As String
Dim Pos1 As String
Dim Pos2 As String
Dim Pos3 As String
Dim Pos4 As String
Dim Pos5 As String
Dim Fin As String
Dim FinTime As String
Dim Medicin As String
Dim Odds As String
Dim Driver As String
Dim Comment As String
Dim Starters As String
Dim Company As String
Dim TempVar As String
End Structure

'My Array

Dim MyInfoArray () as MyStructure

WHY DO I HAVE TO INITIALIZE IT BEFORE USING IT"
IF I DO, I WILL HAVE TO DO IT LATER WHEN I REDIM IT
WHY THE WARNING

I DONT WANT TO WRITE MORE CODE TO ASSIGN SOMETHING BEFORE I USE IT
THERE ARE INSTANCES WHERE
IF MyInfoArray.Driver = "" then
Do something
End If
WHAT'S WRONG WITH THIS

IMAGINE MULTIDIMENTIONAL ARRAYS A NIGHTMARE

How to avoid this Please Help



Visual Studio Express Editions26  
 
 
elenaar-ms





PostPosted: Visual Basic Express Edition, Variable is use before it has been assigned a value (What about Multidimetional Arrays)? Top

Hi, by default the message in the error list show as a warning so you are aware of you can ignore it. But if you don't want to see this warnings you can change the settings in the Project Designer Compile page:

1. Go to menu Project/Properties

2. Select Compile Pane

3. Change notification for "Use of Variable prior to assignment" as None instead of Warning.

Hope that will help

Elena Arzac

VB Test Team


 
 
Connie Daniel





PostPosted: Visual Basic Express Edition, Variable is use before it has been assigned a value (What about Multidimetional Arrays)? Top

I am finding that I need to change this setting for each individual project.

Is there a way to make this change a default setting for all projects

Thanks in advance for your assistance.

Connie Daniel

Hi, by default the message in the error list show as a warning so you are aware of you can ignore it. But if you don't want to see this warnings you can change the settings in the Project Designer Compile page:

1. Go to menu Project/Properties

2. Select Compile Pane

3. Change notification for "Use of Variable prior to assignment" as None instead of Warning.

Hope that will help

Elena Arzac

VB Test Team


 
 
SJWhiteley





PostPosted: Visual Basic Express Edition, Variable is use before it has been assigned a value (What about Multidimetional Arrays)? Top

I would recommend not turning off this warning: simply because it will lead to buggy code. If you eliminate the warning by not showing it, then you may end up with a runtime error which is a magnitude harder to fix (as well as costly and time consuming). Personally, I don't like programs which crash unexpectedly, I don't know about you.

It's pretty trivial to check for null exceptions (you need that in there), and even initialize the structure to some default values. The overhead is minimal compared to the potential of a runtime error.