VB.NET通用函数 [英] VB.NET Generic Function

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

问题描述

我想要做的是,根据T的类型做不同的操作。下面是我的问题的简单示例。

 公共共享函数示例(Of T)()As T 
Dim retval As T
如果TypeOf retval是String然后
Dim myString As String =Hello
retval = myString
ElseIf TypeOf retval是整数然后
Dim myInt As Integer = 101
retval = myInt
End If
返回retval
End Function

出现错误类型'字符串'的值不能转换为'T'与整数部分相同。如果我把它们投向一个物体,然后将它们重新定位,它就会起作用,但我认为这会打败我的目的,效率会降低。有任何想法吗?感谢!

解决方案

这可能有点晚,但试试这个:

 公共共享函数CAnyType(Of T)(ByRef UTO As Object)作为T 
返回CType(UTO,T)
End Function


公共共享函数Exec​​uteSQLstmtScalar(Of T)(ByVal strSQL As String)As T
Dim T_ReturnValue As T

'这里我们得到了一个DB查询'
Dim obj As Object =来自数据库查询cmd.ExecuteScalar的值
Dim strReturnValue As Object = obj.ToString();



尝试
Dim tReturnType As Type = GetType(T)

如果tReturnType是GetType(String)则
返回CAnyType(Of T)(strReturnValue)
ElseIf tReturnType GetType(布尔)然后
Dim bReturnValue As Boolean = Boolean.Parse(strReturnValue)
返回CAnyType(Of T)(bReturnValue)
ElseIf tReturnType GetType(Integer)Then
Dim iReturnValue As Integer = Integer.Parse(strReturnValue)
返回CAnyType(Of T)(iReturnValue)
ElseIf tReturnType GetType(Long)然后
Dim lngReturnValue As Long = Long.Parse(strReturnValue)
返回CAnyType(Of T)(lngReturnValue)
Else
MsgBox(ExecuteSQLstmtScalar(Of T):This type is尚未定义。)
结束如果

赶上例外

结束尝试

返回Nothing
End Function

将您的通用结果转换为对象,然后从Object类型转换为模板类型T)。

PS:

您有责任确保您的代码能够正确地使用可为空的类型和不可为空的类型,以及System.DbNull.Value。例如,当字符串为NULL且返回值类型为布尔值(不可为空)时。在旁注中,请注意VB Nothing不等于NULL,它等于C#的 default(T)(例如Guid的System.Guid.Empty)


What I want to do is, based on the type of T do different opperations. Below is a simple example of my problem.

Public Shared Function Example(Of T)() As T
    Dim retval As T
    If TypeOf retval Is String Then
        Dim myString As String = "Hello"
        retval = myString
    ElseIf TypeOf retval Is Integer Then
        Dim myInt As Integer = 101
        retval = myInt
    End If
    Return retval
End Function

I get the error "Value of Type 'String' Cannot be converted to 'T'" Same with the integer part. If I cast either to an object before asigning them to retval it works but I think that would defeat my purpose and be less efficient. Any Ideas? Thanks!

解决方案

It's probably a bit late, but try this:

    Public Shared Function CAnyType(Of T)(ByRef UTO As Object) As T
        Return CType(UTO, T)
    End Function


    Public Shared Function ExecuteSQLstmtScalar(Of T)(ByVal strSQL As String) As T
        Dim T_ReturnValue As T

        ' Here we have the result of a DB query ' 
        Dim obj As Object = "Value from DB query cmd.ExecuteScalar"
        Dim strReturnValue As Object = obj.ToString();



        Try
            Dim tReturnType As Type = GetType(T)

            If tReturnType Is GetType(String) Then
                Return CAnyType(Of T)(strReturnValue)
            ElseIf tReturnType Is GetType(Boolean) Then
                Dim bReturnValue As Boolean = Boolean.Parse(strReturnValue)
                Return CAnyType(Of T)(bReturnValue)
            ElseIf tReturnType Is GetType(Integer) Then
                Dim iReturnValue As Integer = Integer.Parse(strReturnValue)
                Return CAnyType(Of T)(iReturnValue)
            ElseIf tReturnType Is GetType(Long) Then
                Dim lngReturnValue As Long = Long.Parse(strReturnValue)
                Return CAnyType(Of T)(lngReturnValue)
            Else
                MsgBox("ExecuteSQLstmtScalar(Of T): This type is not yet defined.")
            End If

        Catch ex As Exception

        End Try

        Return Nothing
    End Function

(the secrect is casting your generic result to object, then casting from type Object to template type T).

PS:
You are responsible to ensure that your code works correctly with nullable types and NOT nullable types, as well as System.DbNull.Value. For example when string is NULL and return value type is Boolean (not nullable). On a sidenote, please also note that VB Nothing is NOT equal NULL, it's equal to C#'s default(T) (e.g. System.Guid.Empty for Guid)

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

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