Types comparison in VB.NET

后端 未结 4 987
春和景丽
春和景丽 2021-02-12 07:24

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         


        
相关标签:
4条回答
  • 2021-02-12 07:47
    If dataType = GetType(String) Then
        return 1
    End If
    
    0 讨论(0)
  • 2021-02-12 07:59
    If datatype Is GetType(String) Then
        'do something
    End If
    

    Substitute Is for = and everything works

    0 讨论(0)
  • 2021-02-12 07:59

    This is probably the best way to do it in VB.

    If dataType Is String Then
        return 1
    End If
    
    0 讨论(0)
  • 2021-02-12 08:00

    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

    0 讨论(0)
提交回复
热议问题