1

I need to return a default enum from a function if it can't find the right one by description.

If I assume that the default enum is numbered 0, how can I do this?

VB won't let me convert 0 to T or even to GetType(T).

I have tried:

Return CType(0, T)
Return CType(0, GetType(T))
Return DirectConvert(0, T)

etc...

Thanks for any help!

4

1 回答 1

3

相信NothingVB 中的工作方式类似于default(T)在 C# 中,所以你应该能够使用:

Return Nothing

这当然适用于我的示例代码:

Option Strict On

Imports System

Public Class Test
    Public Shared Sub Main()        
        Console.WriteLine(Foo(Of DayOfWeek)().ToString)
    End Sub

    Public Shared Function Foo(Of T) () As T
        Return Nothing
    End Function
End Class
于 2013-09-12T15:11:31.190 回答