Dim firstName As String
Console.Write("Please enter your first name: ")
firstName = Console.ReadLine() 'Accepts user's first name
Now how would I compare the data type of "firstName" to another data type, for example integer? That way, I could use an If statement to compare the data types and output an error message if the data the user inputted is erroneous.
I tried this:
Dim firstName As String
Console.Write("Please enter your first name: ")
firstName = Console.ReadLine()
If firstName IsNot GetType(String) Then
Console.Write("Error, please try again: ")
End If
This did not work because every time, the statement would equal false and so the error message would never be outputted regardless of the data type inputted.
If you want to check the string for letters do something like that:
Function CheckForAlphaCharacters(ByVal StringToCheck As String)
For i = 0 To StringToCheck.Length - 1
If Not Char.IsLetter(StringToCheck.Chars(i)) Then
Return False
End If
Next
Return True 'Return true if all elements are characters
End Function
You can also check for whitespaces in the string:
Function CheckForAlphaCharacters(ByVal StringToCheck As String)
For i = 0 To StringToCheck.Length - 1
If Not Char.IsLetter(StringToCheck.Chars(i)) And Not Char.IsWhiteSpace(StringToCheck.Chars(i)) Then
Return False
End If
Next
Return True 'Return true if all elements are characters or whitespaces
End Function