Types comparison in VB.NET
How i can compare type data type in VB.NET? My code:
Private Function Equal(ByVal parameter As String, ByVal paramenterName As String, ByVal dataType As Type) As String
If dataType = String Then
return 1;
End If
End Functio开发者_如何学编程n
Any ideas?
If dataType = GetType(String) Then
return 1
End If
If datatype Is GetType(String) Then
'do something
End If
Substitute Is
for =
and everything works
The accepted answer has a syntax error. Here is the correct solution:
If dataType = GetType(String) Then
Return 1
End If
Or
If dataType.Equals(GetType(String)) Then
Return 1
End If
Or
If dataType Is GetType(String) Then
Return 1
End If
The last way is probably the best way to check because it won't throw an exception if the object is null.
Also see https://msdn.microsoft.com/en-us/library/system.object.gettype(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
This is probably the best way to do it in VB.
If dataType Is String Then
return 1
End If
精彩评论