VB.NET 中的类型比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4012827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:05:56  来源:igfitidea点击:

Types comparison in VB.NET

vb.nettypes

提问by Harold Sota

How i can compare type data type in VB.NET? My code:

我如何比较 VB.NET 中的类型数据类型?我的代码:

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 Function

Any ideas?

有任何想法吗?

采纳答案by thecoolmacdude

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

另请参阅https://msdn.microsoft.com/en-us/library/system.object.gettype(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

回答by Darin Dimitrov

If dataType = GetType(String) Then
    return 1
End If

回答by user2554744

If datatype Is GetType(String) Then
    'do something
End If

Substitute Isfor =and everything works

替代Is=一切作品

回答by David Finch

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

这可能是在 VB 中做到这一点的最佳方式。

If dataType Is String Then
    return 1
End If