VB.NET 中的类型比较 [英] Types comparison in VB.NET

查看:38
本文介绍了VB.NET 中的类型比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何比较 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 Function

有什么想法吗?

推荐答案

接受的答案存在语法错误.这是正确的解决方案:

The accepted answer has a syntax error. Here is the correct solution:

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

 If dataType.Equals(GetType(String)) Then
      Return 1
 End If

 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.

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

这篇关于VB.NET 中的类型比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆