返回所选结构常量的属性

时间:2015-03-11 11:22:03

标签: vb.net

我正在尝试从结构中获取所选值,因此如果用户选择“自定义”或“AD”身份验证,我将调用一个函数来返回所选的选项。我正在使用一个试图返回所选常量的附加属性。基本上我想要的是设置txtAuthentication.text = LoginDetails.Authentication。当我这样做时,我得到一个LoginDetails.Authentication无法转换为字符串的错误。我的房产应该如何形成?

Public Class LoginDetails
Structure Authentication
    Const cCustom = "Custom"
    Const cActiveDirectory = "AD"

    Public ReadOnly Property Custom() As String
        Get
            Return cCustom
        End Get
    End Property
    Public ReadOnly Property AD() As String
        Get
            Return cActiveDirectory
        End Get
    End Property
End Structure

Private Shared sAuthentionUsed As Authentication
Public Shared Property AuthentionUsed() As Authentication
    Get
        Return sAuthentionUsed
    End Get
    Set(value As Authentication)
        sAuthentionUsed = value
    End Set
End Property

结束班

1 个答案:

答案 0 :(得分:1)

您正尝试将类型身份验证分配给字符串属性。这是我将如何做到的。

Public Class LoginDetails
    Enum AuthenticationType
        Custom = 0
        AD = 1
    End Enum

    Private Shared sAuthentionUsed As AuthenticationType
    Public Shared Property AuthentionUsed() As AuthenticationType
        Get
            Return sAuthentionUsed
        End Get
        Set(value As AuthenticationType)
            sAuthentionUsed = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Select Case sAuthentionUsed
            Case AuthenticationType.AD
                Return "AD"
            Case AuthenticationType.Custom
                Return "Custom"
            Case Else
                Throw New NotImplementedException("Enum has no string representation: " & sAuthentionUsed.ToString())
        End Select
    End Function
End Class

然后使用

txtAuthentication.text = LoginDetails.ToString()

我相信有人会指出你可以将枚举转换为字符串来获取名称。