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

Cod*_*ber 5 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这让我可以这样做:

Country country1 = getCountryFromSomewhere();
Country country2 = Country.FRANCE;
System.out.print("country1 is in " + country1.getContinent());
System.out.print("country2 is in " + country2.getContinent());
Run Code Online (Sandbox Code Playgroud)

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

Jon*_*eet 8

(在整个过程中使用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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

请注意,这不允许您打开枚举值.

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

一种替代方法是在普通枚举上使用属性:

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

public enum Country
{
    [Continent("Europe")] Ireland = 1,
    [Continent("Europe")] France = 2,
    ...
}
Run Code Online (Sandbox Code Playgroud)

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

请注意,这里没有真正的一个固定的值-你可以写:

Country country = (Country) 15;
Run Code Online (Sandbox Code Playgroud)

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

  • @Tim Schmelter:他不仅仅是某个人. (5认同)

小智 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
Run Code Online (Sandbox Code Playgroud)


Cod*_*ber 2

我改用这个解决方案:

声明枚举:

Private Enum Country
    IRELAND
    FRANCE
    THAILAND
End Enum
Run Code Online (Sandbox Code Playgroud)

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

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")
Run Code Online (Sandbox Code Playgroud)

这让我可以得到这样的大陆:

Dim franceContinent As String = countryContinentMap(Country.FRANCE)
Run Code Online (Sandbox Code Playgroud)