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
Printable View
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
Found my own Answer. Thank you
Ans: If TypeName(x) = "Integer"
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?
Like this :
VB Code:
Dim i As Integer Dim t As Type =I.GetType MessageBox.Show(t.Tostring)
You might do something like this :
VB Code:
Select Case (t.ToString) Case "Integer" 'do this Case "String" 'do this
The problem I am having is that all the values come in as a string because I am doing these operations in a Function.
Maybe this clarifies what I am trying to do.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
Thanks
The problem I am having is that all the values come in as a string because I am doing these operations in a Function.
Maybe this clarifies what I am trying to do.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
Thanks
Do you mean , you want to check if the passed string is Integer or String type ? or what ?:confused:
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:Quote:
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.
Maybe this clarifies what I am trying to do.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
Thanks
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
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
Hi,
Sorry I was a bit slow in posting my response just now. I was trying it out in the middle of posting.
I'm not at my dev computer now . If you got no more help , I'll make you a demo .
Thanks to your help it works now.
-Patrick
Which did you use?Quote:
Originally posted by SoonerToucan
Thanks to your help it works now.
-Patrick
I used the code you posted Taxes, it worked great.
Thanks again to everyone for all the help
-Patrick