Results 1 to 14 of 14

Thread: How to check the data type of a variable

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36

    How to check the data type of a variable

    Is it possible to check and see what data type a variable is in a conditional statement?

    Ex:
    Dim x as Integer = 1

    If x Is Integer Then
    MessageBox.Show("Int")
    Else
    MessageBox.Show("Other")
    End IF

    Thanks,
    -Patrick

  2. #2

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    Found my own Answer. Thank you

    Ans: If TypeName(x) = "Integer"

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    Actually this brought up another problem.

    I guess what I am looking for is a function that will let me know if I can cast, a passed, string variable into an integer.

    I have a function that takes either a, b, c or a number from 1-10. I am trying to write some validation code that will make sure it meets those criteria. If the passed string is either a-c or 1-0 I would like to change the varialbe type of a, b, or c to 11, 12 or 13.

    Any Ideas?

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Like this :
    VB Code:
    1. Dim i As Integer
    2. Dim t As Type =I.GetType
    3. MessageBox.Show(t.Tostring)

    You might do something like this :

    VB Code:
    1. Select Case (t.ToString)
    2.  
    3. Case "Integer"
    4. 'do this
    5.  
    6. Case "String"
    7. 'do this

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    The problem I am having is that all the values come in as a string because I am doing these operations in a Function.

    Code:
    Public Function Validate(ByVal Number As String)
    Dim TmpCard as Integer
    
    'The purpose of this function is to take in text from a textbox that represents a playing card and checks to make sure its either 1-13 or a, j, q, or k
    
                Select Case UCase(Number)
                    Case "A"
                        TmpCard = 1
                    Case "J"
                        TmpCard = 11
                    Case "Q"
                        TmpCard = 12
                    Case "K"
                        TmpCard = 13
                    Case Else
                        TmpCard = Number  **Errors When I pass in 'ggg' (cannot convert type string to integer)**
                End Select
    
            If TmpCard > 0 And TmpCard < 14 Then
                Return TmpCard
            Else
                MessageBox.Show("Enter Card number 1-13 or a, j, q, k")
            End If
    
    End Function
    Maybe this clarifies what I am trying to do.

    Thanks
    Last edited by SoonerToucan; Jun 10th, 2004 at 05:08 PM.

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    The problem I am having is that all the values come in as a string because I am doing these operations in a Function.

    Code:
    Public Function Validate(ByVal Number As String)
    Dim TmpCard as Integer
    
                Select Case UCase(Number)
                    Case "A"
                        TmpCard = 1
                    Case "J"
                        TmpCard = 11
                    Case "Q"
                        TmpCard = 12
                    Case "K"
                        TmpCard = 13
                    Case Else
                        TmpCard = Number  **Errors When I pass in 'ggg' (cannot convert type string to integer)**
                End Select
    
            If TmpCard > 0 And TmpCard < 14 Then
                Return TmpCard
            Else
                MessageBox.Show("Enter Card number 1-13 or a, j, q, k")
            End If
    
    End Function
    Maybe this clarifies what I am trying to do.

    Thanks

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Do you mean , you want to check if the passed string is Integer or String type ? or what ?

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by SoonerToucan
    The problem I am having is that all the values come in as a string because I am doing these operations in a Function.

    Code:
    Public Function Validate(ByVal Number As String)
    Dim TmpCard as Integer
    
    'The purpose of this function is to take in text from a textbox that represents a playing card and checks to make sure its either 1-13 or a, j, q, or k
    
                Select Case UCase(Number)
                    Case "A"
                        TmpCard = 1
                    Case "J"
                        TmpCard = 11
                    Case "Q"
                        TmpCard = 12
                    Case "K"
                        TmpCard = 13
                    Case Else
                        TmpCard = Number  **Errors When I pass in 'ggg' (cannot convert type string to integer)**
                End Select
    
            If TmpCard > 0 And TmpCard < 14 Then
                Return TmpCard
            Else
                MessageBox.Show("Enter Card number 1-13 or a, j, q, k")
            End If
    
    End Function
    Maybe this clarifies what I am trying to do.

    Thanks
    May be I am misunderstanding you but TmpCard only exists within the Function. You should be using "Return". Also, it is dangerous to use a VB.NET keyword as the name of a function. Try:

    In the calling event, assuming the card value comes from TextBox1:

    Dim TmpCard as Integer
    TmpCard=val(CardValidate(Textbox1.Text))
    If TmpCard<1 or TmpCard>13 Then
    MessageBox.Show("Invalid Card Entry)
    End If


    Public Function CardValidate(ByVal Number As String)
    Select Case UCase(Number)
    Case "A"
    Return "1"
    Case "J"
    Return "11"
    Case "Q"
    Return "12
    Case "K"
    Return "13"
    Case Else
    Return Number
    End Select

    End Function
    Last edited by taxes; Jun 10th, 2004 at 05:41 PM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    In a sense yes. In non programming terms I want the user to input some text in a textbox that is supposed to represent a value of a playing card. Once they have entered the value I would like to check it to make sure it is either a number from 1 to 13 or the letters A, J, Q or K.

    The function works fine until someone types in jibberish like 'ggg9'

    I hope that clairifies it some.

    Thanks,
    -Patrick

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Sorry I was a bit slow in posting my response just now. I was trying it out in the middle of posting.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I'm not at my dev computer now . If you got no more help , I'll make you a demo .

  12. #12

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    Thanks to your help it works now.

    -Patrick

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by SoonerToucan
    Thanks to your help it works now.

    -Patrick
    Which did you use?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  14. #14

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    I used the code you posted Taxes, it worked great.

    Thanks again to everyone for all the help

    -Patrick

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