[RESOLVED] the problem with variable in "if....end"
codes like below:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim n As Integer = 3
Dim b As Integer = 7
Dim c As integer
Dim k As String
Dim x As Integer
If n > 1 Then
If x = 5 Then
k = "23"
End If
If x = 6 Then
k = "33"
End If
Else
k = "99"
End If
If b > 5 Then
c = b + k
Else
c = b - k
End If
textbox1.text = c
End Sub
when I run the codes above, error message indicated:
Variable k is used before it has been assigned a value.A null reference exception could result at runtime
pls,thanks
Re: the problem with variable in "if....end"
It isn't an error, its a warning - there is a difference : with an error your code won't compile, with a warning it will compile but there is a good chance it will raise an exception when run.
All its saying is that by the time you reach this line :
Code:
If b > 5 Then
c = b + k
Else
c = b - k
End If
K does not necessarily contain anything, ie if n isn't >1 then k will not get set.
You can remedy this by changing the declaration to :
Dim K as string = ""
Although I don't know why it is a string anyway - you are doing purely numerical operations with it as far as I can see.
Re: the problem with variable in "if....end"
If you have "option strict" enabled then all warnings will be treated as errors otherwise as Paul said you can continue with your program despite the warning.
Cheers,
--Stav.
Re: the problem with variable in "if....end"
Turn Option Strict On. Fix the errors.
You are mixing data types, which will cause you a runtime error: that is, your program may compile but it will crash when someone tries to run it. (perhaps not in this instance, but the laziness will catch up with you in the future).
For example:
dim x as Integer
x = 5.23 + "fred"
What is the value of x?
Re: the problem with variable in "if....end"
Quote:
Originally Posted by
stavris
If you have "option strict" enabled then all warnings will be treated as errors otherwise as Paul said you can continue with your program despite the warning.
Cheers,
--Stav.
That's not true. Having Option Strict On has nothing to do with treating warnings as errors. Open the Compile tab of the project properties and look at the Warning Configurations list. With Option Strict Off the first three are None, i.e. not Warnings or Errors. With Option Strict On those first three are Errors. The setting to treat Warnings as Errors is a completely separate box below that list.
Re: the problem with variable in "if....end"
Quote:
Originally Posted by
keystone_paul
It isn't an error, its a warning - there is a difference : with an error your code won't compile, with a warning it will compile but there is a good chance it will raise an exception when run.
All its saying is that by the time you reach this line :
Code:
If b > 5 Then
c = b + k
Else
c = b - k
End If
K does not necessarily contain anything, ie if n isn't >1 then k will not get set.
You can remedy this by changing the declaration to :
Dim K as string = ""
Although I don't know why it is a string anyway - you are doing purely numerical operations with it as far as I can see.
thanks everyone, especially for paul, one more question:
paul has mentioned "if n isn't >1 then k will not get set." but actually I set the value of K to "99" in the condition of "n isn't >1" my codes as below:
If n > 1 Then
If x = 5 Then
k = "23"
End If
If x = 6 Then
k = "33"
End If
Else
k = "99"
End If
Re: the problem with variable in "if....end"
That's a good reason to use the code tags. Without the tags, reading that can get a bit confusing.
The problem still stands. For K to remain as nothing, all that is necessary is that n be greater than 1, while x is neither 5 nor 6.
I would also argue for Option Strict ON.
Re: the problem with variable in "if....end"
Quote:
Originally Posted by
vbsunst
thanks everyone, especially for paul, one more question:
paul has mentioned "if n isn't >1 then k will not get set." but actually I set the value of K to "99" in the condition of "n isn't >1" my codes as below:
If n > 1 Then
If x = 5 Then
k = "23"
End If
If x = 6 Then
k = "33"
End If
Else
k = "99"
End If
Incorrect. If k would always get set, then you wouldn't get the warning (VB is smart like that). Step through the EXACT code you have posted and you'll see.
Re: [RESOLVED] the problem with variable in "if....end"
Quote:
paul has mentioned "if n isn't >1 then k will not get set." but actually I set the value of K to "99" in the condition of "n isn't >1" my codes as below
Sorry - my mistake
As ShaggyHiker pointed out, the CODE tags help make your code more readable (its a bugger to work out the maching IFs and END IFs when there's no indentation!) and there are still circumstances in which k doesn't get set.