Dim dr as DataRow = ?? in VS 2005

dseydel

Member
Joined
Aug 24, 2005
Messages
12
Programming Experience
10+
In VS 2005 many of my datarow statements result in the following exception:

"Variable (DataRow) is used before it is assigned a value. A null reference exception could result at runtime."

Is there a way to "Dim drows() as DataRow = Something" to prevent that exception? What is that Something?
 
Dim DataRow

Dim tblTest As DataTable
tblTest = DsTest1.Tables("Test")

Dim drCurrent As DataRow
drCurrent = tblTest.NewRow()


Looking for something like this?
 
I'll tell you why that warning message appears. In C programming there are lvalues and rvalues. lvalues are variables that appear on the left-hand side of an assignment (read: equals sign) while rvalues are variables that appear on the right hand side. In C, C++, C# and I would guess Java as well, every variable must be used as an lvalue before it can be used as an rvalue, i.e. you MUST assign a value to a variable before you can use that variable in any other way, e.g. as a method argument. This has been partially adopted in VB now too, in that you will be WARNED if you use a variable as an rvalue before using it as an lvalue. In short, you should initialise EVERY variable, even if only with Nothing, e.g.
VB.NET:
Dim myDataRow As DataRow = Nothing
This doesn't have any real effect on the variable itself but it tells the compiler that you know that the variable is Nothing and you specifically intended for it to be Nothing, not that it just happened to be Nothing by default because you forgot to assign it a real value.
 
Back
Top