Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Override ToString Method for my Enum

Status
Not open for further replies.

Norris68

IS-IT--Management
Jun 19, 2002
769
GB
I have a Public Enum defined in one of my classes. I would like to override the ToString method so I can return a more sensible string than the internal name (as it currently does). The help seems to want me to run round in circles. How do I go about it?

Public Class clsMyClass
Public Enum eState
eStateOK
eStateFailed
eStateUnknown
End Enum

'Then a whole pile of stuff...
End Class
 
You should be able to use the default override of ToString (the one without any parameters), as the doc shows it:
Code:
<Serializable>
Overrides Overloads Public Function ToString() As String
But my advice would be to name your enum elements what the user will expect to see.

If you need to localize this to several languages, then I'd create a class with static/shared methods rather than use an enum, where the static/shared method returns the appropriate language.

Chip H.
 
I am doing a similar thing and I don't think it is sensible to necessarily show the user what the name of each Enum is. For instance, I have
Enum enMaritalStatus
Single_Filing_Single as Integer
Maried_Filing_Separate as Integer
Maried_Filing_Jointly as Integer
Head_Of_Household as Integer
End Enum

Problem is, I want to be able to change Single_Filing_Single to a sensible string for the user such as "Your filing status has been recorded as Filing Single" but still be able to use the Single_Filing_Single for comparisons and various other things.

The difference with me and Norris68 is that I have a property
Public property MaritalStatus () as enMaritalStatus
Get....
Set....
End Property

I want to do something in the code of the program like this:

dim strStats as string
strStats = obj.MaritalStatus.ToString

I want to overload the ToString only for that property. How do you do that? Or for that matter, how do you create anything that you can add after a . after a property that you create? so far I have only been able to get it to go one layer deep such as:

dim obj as new myclass
obj.prop1
obj.prop2

I can't seem to figure out how to do:
obj.prop1.anotherprop

Any ideas?
 
Just thought of what it could be called:
.ToFriendlyString

Ooh, that's gooooood!

"But that's not the problem. It's how to do it. These things must be done delllllicately, or you'll hurt the spell." (Wicked Witch of the West, Wizard of Oz, 1939)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top