CodeClimber CodeClimber - 4 months ago 11
Vb.net Question

Can I give an enum an attribute in VB.NET (like I can do in Java)?

In Java I can do something like this:

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

private String continent;

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

public String getContinent() {
return continent;
}
}


which allows me to do something like:

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


Is it possible to do the same thing in VB.NET i.e. add the continent attribute to the country enum?

Answer

I used this solution instead:

Declare enum:

Private Enum Country
    IRELAND
    FRANCE
    THAILAND
End Enum

Declare and initialise Dictionary (aka a map):

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")

which allows me to get the continent like this:

Dim franceContinent As String = countryContinentMap(Country.FRANCE)
Comments