|
-
Aug 1st, 2003, 09:39 AM
#1
Thread Starter
Frenzied Member
data type
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...
It's tough being an unhandled exception...
___________
VB.NET 2008
VB.NET 2010
ORACLE 11g
CRYSTAL 11
-
Aug 1st, 2003, 11:13 AM
#2
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
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Aug 1st, 2003, 11:20 AM
#3
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.
-
Aug 1st, 2003, 12:15 PM
#4
Thread Starter
Frenzied Member
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?
It's tough being an unhandled exception...
___________
VB.NET 2008
VB.NET 2010
ORACLE 11g
CRYSTAL 11
-
Aug 1st, 2003, 12:21 PM
#5
New Member
The answer is "Implicit Datatype Conversion"
-
Aug 1st, 2003, 01:16 PM
#6
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
-
Aug 1st, 2003, 01:19 PM
#7
Thread Starter
Frenzied Member
It's tough being an unhandled exception...
___________
VB.NET 2008
VB.NET 2010
ORACLE 11g
CRYSTAL 11
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
|