CodeClimber CodeClimber - 1 year ago 79 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 {

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 Source

I used this solution instead:

Declare enum:

Private Enum Country
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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download