|
-
Jun 10th, 2004, 04:25 PM
#1
Thread Starter
Member
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
-
Jun 10th, 2004, 04:32 PM
#2
Thread Starter
Member
Found my own Answer. Thank you
Ans: If TypeName(x) = "Integer"
-
Jun 10th, 2004, 04:45 PM
#3
Thread Starter
Member
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?
-
Jun 10th, 2004, 04:53 PM
#4
Sleep mode
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
-
Jun 10th, 2004, 05:03 PM
#5
Thread Starter
Member
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.
-
Jun 10th, 2004, 05:21 PM
#6
Thread Starter
Member
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
-
Jun 10th, 2004, 05:24 PM
#7
Sleep mode
Do you mean , you want to check if the passed string is Integer or String type ? or what ?
-
Jun 10th, 2004, 05:36 PM
#8
PowerPoster
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.
-
Jun 10th, 2004, 05:36 PM
#9
Thread Starter
Member
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
-
Jun 10th, 2004, 05:45 PM
#10
PowerPoster
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.
-
Jun 10th, 2004, 05:55 PM
#11
Sleep mode
I'm not at my dev computer now . If you got no more help , I'll make you a demo .
-
Jun 10th, 2004, 06:45 PM
#12
Thread Starter
Member
Thanks to your help it works now.
-Patrick
-
Jun 10th, 2004, 06:48 PM
#13
PowerPoster
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.
-
Jun 10th, 2004, 07:00 PM
#14
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|