开发者

generic function to return enum value

开发者 https://www.devze.com 2023-01-28 18:50 出处:网络
I have three different Enum and all of three has 开发者_如何转开发same identifier but different values.I want to access particular enum based on some condition.

I have three different Enum and all of three has 开发者_如何转开发same identifier but different values. I want to access particular enum based on some condition.

for example:

Public Enum Type1
     Font = 10
     Color = 11 
End Enum

Public Enum Type2
     Font = 20
     Color = 21
End Enum

Public Enum Type3
     Font = 30
     Color = 31
End Enum

And based on certain condition i need to access particular enum. for example,

if(somecondition = 1) 
    return Type1.Font
else if (somecondition = 2)
    return Type2.Font
else if (somecondition = 3)
    return Type3.Font

I need to repeat same logic to access other enum identifier. Is there any way I can write generic method that return me enum value?

for example,

public function GetEnumValue(enumtype, identifier) as integer

  return enumtype.identifier 

end function

Is there any way to write above generic function to return enum value?

Updated: I am looking for method like GetEnumValue(Type1,Font) that returns enum value in integer (in this case 10 for type1.font)


IMO you're misusing enums. You should have one enum and either

  • three functions with a switch
  • three dictionaries from your enum to int
  • decorate each entry in the enum with attributes for the desired values.

Your immediate problem can be solved with Enum.Parse


Your question is extremely unclear.

You may be looking for

return Enum.Parse(enumType, valueName);

Where enumType is a Type object and valueName is a string.

0

精彩评论

暂无评论...
验证码 换一张
取 消