If STATUS is a string and I have this code:
VB Code:
Dim status as string = 5 If status <= 7 Then go here end if
why does this code see the IF statement as true, since STATUS is not an integer?
just wondering...
Printable View
If STATUS is a string and I have this code:
VB Code:
Dim status as string = 5 If status <= 7 Then go here end if
why does this code see the IF statement as true, since STATUS is not an integer?
just wondering...
you mean like this? :
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim status As String = 5 If Convert.ToInt32(status) < 7 Then MessageBox.Show("the value is less!") Else MessageBox.Show("the value is more!") End If End Sub
I think it attempts to convert it and fails so it uses the default value of 0 which is less than 7 making the statement true.
Edneesis, thats makes sense...but say I have this code instead:
VB Code:
Dim status as string = "5" If status = 5 Then go here end if
it still runs the IF statement as true...if 5 is a string, how can it equal an integer?
The answer is "Implicit Datatype Conversion"
You can use Option Strict to force implicit conversion as eshaanye said. Or as long as the string is meant to contain a string version of a number than you can use the parse method of an integer to convert it.
VB Code:
Dim status as string = "5" If Integer.Parse(status) = 5 Then go here end if
that'll do...thanks