我可以在VB.NET中给枚举一个属性(就像我可以用Java做的那样)吗?

时间:2011-08-02 16:28:29

标签: java vb.net enums

在Java中,我可以这样做:

enum Country {
    IRELAND("Europe"),
    FRANCE("Europe"),
    NIGERIA("Africa"),
    THAILAND("Asia");

    private String continent;

    Country(String continent) {
        this.continent = continent;
    }

    public String getContinent() {
        return continent;
    }
}

允许我做类似的事情:

Country country1 = getCountryFromSomewhere();
Country country2 = Country.FRANCE;
System.out.print("country1 is in " + country1.getContinent());
System.out.print("country2 is in " + country2.getContinent());

是否可以在VB.NET中执行相同的操作,即将continent属性添加到国家/地区枚举?

5 个答案:

答案 0 :(得分:7)

(在整个过程中使用C#的道歉 - 我相信这些概念更多的是关于.NET而不是你碰巧使用的语言;希望你在阅读C#方面比我在编写VB时更好。)

不直接 - .NET中的枚举只是整数类型,其中包含某些值的名称。

.NET中最接近的是创建一组具有固定值的类型。例如,在您的情况下:

public sealed class Country
{
    public static readonly Country Ireland = new Country("Europe");
    public static readonly Country France = new Country("Europe");
    public static readonly Country Nigeria = new Country("Africa");
    public static readonly Country Thailand = new Country("Asia");

    private readonly string continent;

    public string Continent { get { return continent; } }

    private Country(string continent)
    {
        this.continent = continent;
    }
}

(我假设VB.NET非常相似。)

请注意,此不会让您打开枚举值。

如果你想要多态,你可以创建嵌套的子类,它仍然可以调用私有构造函数,这可以防止创建任何其他子类。

另一种替代方法是在普通枚举上使用 attributes

[AttributeUsageAttribute(AttributeTargets.Field)]
public class ContinentAttribute : Attribute
{
    // etc
}

public enum Country
{
    [Continent("Europe")] Ireland = 1,
    [Continent("Europe")] France = 2,
    ...
}

然后,您需要使用反射来获取ContinentAttribute并检索字符串。

请注意,这里没有确实一组固定的值 - 您可以写:

Country country = (Country) 15;

此时你无法获得它的大陆,如果你将它传递给任何期望它是真正的国家的方法,你就会遇到问题。对于早期的解决方案,情况并非如此,在这种解决方案中,您实际上只限于那些少数值(和null)。

答案 1 :(得分:5)

以下是代码:

导入System.ComponentModel

Imports System.Reflection

Public Enum enumOrderStatus
    <Description("None")>
    None
    <Description("Sent")>
    Sent
    <Description("Accepted")>
    Accepted
    <Description("Cancelled")>
    Cancelled
    <Description("Declined")>
    Declined
End Enum


Public Function GetEnumDescription(ByVal EnumConstant As [Enum]) As String
    Dim fi As FieldInfo = EnumConstant.GetType().GetField(EnumConstant.ToString())
    Dim aattr() As DescriptionAttribute = DirectCast(fi.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
    If aattr.Length > 0 Then
        Return aattr(0).Description
    Else
        Return EnumConstant.ToString()
    End If
End Function

答案 2 :(得分:1)

我改用了这个解决方案:

声明枚举:

Private Enum Country
    IRELAND
    FRANCE
    THAILAND
End Enum

声明并初始化词典(又名地图):

Dim countryContinentMap As IDictionary(Of Country, String) = New Dictionary(Of Country, String)
countryContinentMap.add(Country.IRELAND, "Europe")
countryContinentMap.add(Country.FRANCE, "Europe")
countryContinentMap.add(Country.THAILAND, "Asia")

这让我可以像这样来到大陆:

Dim franceContinent As String = countryContinentMap(Country.FRANCE)

答案 3 :(得分:0)

以下是我在申请中解决这个问题的方法。仍在寻找更容易的东西。

你怎么看?

Public Sub Init()
    Dim values() As Integer = CType([Enum].GetValues(GetType(MyEnum)), Integer())
    For i As Integer = 0 To values.Count - 1
        Me.contextMenuInGUI.Items.Add(Me.GetEnumDescription(i))
    Next
End Sub

Private Function GetEnumDescription(ByVal i As Integer) As String
    Select Case i
        Case MyEnum.Comment
            Return "Description for Comment"

        Case MyEnum.SomeEnumValueInCamelCase
            Return "Value without camel case (€)(%)(@)"
    End Select
    Return "Add a case in Class:GetEnumDescription"
End Function

答案 4 :(得分:0)

为您的枚举

创建扩展方法

用法示例:

dim description = TableTag.Important.GetDescription()

定义示例:

Imports System.ComponentModel
Imports System.Reflection
Imports System.Runtime.CompilerServices

Namespace Foo

  Public Enum TableTag

    <Description("Identifies tables that should be availible for writing as table or view to the model database")>
    Important

    <Description("Example for a table group that helps to select disctinct tables")>
    CustomGroup

  End Enum

  Public Module TableTagExtensions

    <Extension>
    Public Function GetDescription(enumValue As TableTag) As String

      Dim fieldInfo As FieldInfo = enumValue.GetType().GetField(enumValue.ToString())
      Dim attributes = DirectCast(fieldInfo.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())

      If attributes.Length > 0 Then
        Return attributes(0).Description
      Else
        Return enumValue.ToString()
      End If

    End Function

  End Module

End Namespace
相关问题