i am new in vb i am trying to demonstrates a function that returns a customer data type. but i got the errors
"Warning 1 Variable 'Company' is used before it has been assigned a value. A null reference exception could result at runtime",
Warning 2 Variable 'Address' is used before it has been assigned a value. A null reference exception could result at runtime,
"Warning 3 Variable 'city' is used before it has been assigned a value. A null reference exception could result at runtime.
" Warning 4 Variable 'Country' is used before it has been assigned a value. A null reference exception could result at runtime".
i have tried to solve these but not fix these errors. please tell me how to fix these
Generally you should declare the variable when you need it and assign it a value directly, for example instead of this:
Dim b As Button
b = New Button
you do this:
Dim b As Button = New Button
or better yet the short form:
Dim b As New Button
If this is not possible you can explicitly assign a null reference to get rid of the warning:
Dim b As Button = Nothing
If condition Then
b = new Button
Else ...
By doing this you're telling compiler you're aware that this variable is initially a null reference, and implicitly that you should be taking precautions to avoid causing a null reference exception at runtime later in code.
Bookmarks