Results 1 to 7 of 7

Thread: data type

  1. #1

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075

    data type

    If STATUS is a string and I have this code:


    VB Code:
    1. Dim status as string = 5
    2. If status <= 7 Then
    3.    go here
    4. 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

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you mean like this? :
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim status As String = 5
    3.         If Convert.ToInt32(status) < 7 Then
    4.             MessageBox.Show("the value is less!")
    5.         Else
    6.             MessageBox.Show("the value is more!")
    7.         End If
    8.     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]

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  4. #4

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075
    Edneesis, thats makes sense...but say I have this code instead:


    VB Code:
    1. Dim status as string = "5"
    2. If status = 5 Then
    3.    go here
    4. 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

  5. #5
    New Member
    Join Date
    Mar 2003
    Location
    China
    Posts
    7
    The answer is "Implicit Datatype Conversion"

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Dim status as string = "5"
    2. If Integer.Parse(status) = 5 Then
    3.    go here
    4. end if

  7. #7

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075
    that'll do...thanks
    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
  •  



Click Here to Expand Forum to Full Width