Return Enum string by byte/integer

ExEdzy

Active member
Joined
Nov 25, 2010
Messages
37
Programming Experience
3-5
Hey guys.
I have worked with enums for some time, but i ran in to a little problem..
So i have the Enum:

VB.NET:
Public Enum MyStuff As Byte
	this_is_zero = 0
	omg_this_is_one = 1
	YourAss = 255
End Enum

and i have a function that returns 1, 0 or 255, like this:

VB.NET:
Public Shared function some_func()
	return 1 ' this actually is dinamicaly generated value, by complex code ^_^
end function

and i need the function to return NOT the integer, but the message that is in MyStuff

for example:

VB.NET:
Dim the_text_value As String = MyStuff(some_func()) ' this returns not "1", but "omg_this_is_one"

how can i do this ?
 
First up, if your function really doesn't have a return type then that's bad. Everything should always an specific type, so that function should be declared As MyStuff. It should then return a MyStuff value, not a number. As for the question, just like any other object, you call ToString on an enumeration field to get a string representation.
 
So if the function, returning integer, has the same name as enum, then it will return string from enum, if the enum has declared the the integer, the function is returning??
 
So if the function, returning integer, has the same name as enum, then it will return string from enum, if the enum has declared the the integer, the function is returning??
I have no idea what you just said. If the function is supposed to return a MyStuff value then declare it As MyStuff and return a MyStuff value. If you want a String then call ToString. It's no more complex than that.
 
Back
Top