|
-
Jun 4th, 2009, 04:43 AM
#1
Thread Starter
Junior Member
[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
-
Jun 4th, 2009, 04:53 AM
#2
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.
-
Jun 4th, 2009, 05:07 AM
#3
Addicted Member
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.
-
Jun 4th, 2009, 06:42 AM
#4
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?
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Jun 4th, 2009, 07:45 AM
#5
Re: the problem with variable in "if....end"
 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.
-
Jun 4th, 2009, 08:45 AM
#6
Thread Starter
Junior Member
Re: the problem with variable in "if....end"
 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
-
Jun 4th, 2009, 09:07 AM
#7
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.
My usual boring signature: Nothing
 
-
Jun 4th, 2009, 09:12 AM
#8
Re: the problem with variable in "if....end"
 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.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Jun 4th, 2009, 10:44 AM
#9
Re: [RESOLVED] the problem with variable in "if....end"
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|