PDA

View Full Version : Enum ToString's



ChloeRadshaw
10-28-2008, 05:44 AM
I am not sure if this is possible but after creating a enum, how can you write a toString() method for that enum?

ie:

Public Enum Currency
USD = 1
GBP = 2
End Enum

How do you get a human readable format of this enum for debugging purposes? At the moment I get the number printed which is quite useless

Any ideas please?

Bob Phillips
10-28-2008, 05:53 AM
I don't think you can get the reverse value, the id from the value.

What exactly are you trying to get when debugging, I have never had such a need?

ChloeRadshaw
10-28-2008, 06:25 AM
I don't think you can get the reverse value, the id from the value.

What exactly are you trying to get when debugging, I have never had such a need?

Given an enum I am trying to get a human readable format so I can write this out to a log file - At the moment it is saying:
Currency : 1

Is there any way to get this to do something like :
Currency : USD without writing an enumResolver?

Finally am I correct in thinking that enums are defined in normal modules.

Thanks

Bob Phillips
10-28-2008, 06:35 AM
Given an enum I am trying to get a human readable format so I can write this out to a log file - At the moment it is saying:
Currency : 1

Is there any way to get this to do something like :
Currency : USD without writing an enumResolver?

Yes, I understood that much, but can you give an example of the code and where you want to get it, I just cannot envisage a need for it.


Finally am I correct in thinking that enums are defined in normal modules.

They can be defined in any module, the scope of the enum will determine its accessibility.

ChloeRadshaw
10-28-2008, 08:13 AM
Yes, I understood that much, but can you give an example of the code and where you want to get it, I just cannot envisage a need for it.



They can be defined in any module, the scope of the enum will determine its accessibility.

Dim usdCurr As CurrencyEnum
usdCurr = CurrencyEnum.USD

Call Debug.print ("Currency : " & usdCurr.???????????)

Bob Phillips
10-28-2008, 10:57 AM
See, there you perfectly illustrate why I cannot see the point of what you are trying to do.

You know you were getting the USD value, you had to know to get it from the Enum, so why do you now need to know that the value associated with the number that you extracted via that value in the first place?

ChloeRadshaw
10-28-2008, 01:13 PM
Yes - But suppose the enum was passed around multiple functions and you have a currEnu variable.

How do you print this out?

Hers the example

Sub PV(curr As CurrencyEnum)
Log.debug(curr.?????????)

Bob Phillips
10-28-2008, 02:40 PM
Then if you have to know, pass a currency string variable around in tandem.

Chabu
12-07-2010, 01:46 PM
I don't know if it is still relevant but I was looking for the same thing, and figured it out my self. Here it goes...

The enumeration goes in a class module. It can't have the same name as the enumeration which is annoying, I called it "enumerationResourceType"

Public Enum enumResourceType
Internal
Contractor
End Enum

in my resource class module I have an attribute

Private fType As enumResourceType

Now you can use it but as you mentioned you get the integer values (0 or 1 in this case) when you want to display the fType attribute instead of the strings ("Internal","Contractor").
I added the following code to the enumeration class module


Public Function toString(x As enumResourceType) As String
Select Case x
Case enumResourceType.Internal
toString = "Internal"
Case enumResourceType.Contractor
toString = "Contractor"
End Select
End Function

The whole purpose is to use it as follows (here in a debug line in the resource collection class)

Public Function Add(ByVal p As clsResource)
Call FResources.Add(p, p.ID)
Debug.Print resourceType.toString(p.resourceType) & " " & p.Name & " added to pool"
End Function

For this to work you just need the following line in a code module of your project

Public resourceType As New enumerationResourceType

I know it is not a real "toString" override like would be available in a oo language, but it works, it allows you to keep that tostring code together with the enum definition. (I even have a "toResourceType" function in the same module that translates from an input string to the enum


Public Function toResourceType(x As String) As enumResourceType
Select Case x
Case "Internal"
toResourceType = enumResourceType.Internal
Case "Contractor"
toResourceType = enumResourceType.Contractor
End Select
End Function


in a piece of code that reads in values from users (or a spreadsheet in my case) I do

aResource.resourceType = resourceType.toResourceType(astring)


(in my code "astring" points to a location in a spreadsheet in which the user can select values from a dropdown...)

I made some simplifications to the code to keep it focussed and short so don't shoot me it I cut away too much...

If anyone has a better way let me know.

Greetings

Paul_Hossler
12-07-2010, 06:01 PM
I think the easiest way (still manual tho) would be to just init an array


Option Explicit

Enum eSample
eInternal = 1
eExternal = 2
eSubcontractor = 3
End Enum

Public eArray As Variant

Sub drv()
eArray = Array(vbNullString, "Internal", "External", "Subcontractor")

MsgBox eInternal & " -- " & eArray(eInternal)
MsgBox eExternal & " -- " & eArray(eExternal)
MsgBox eSubcontractor & " -- " & eArray(eSubcontractor)

End Sub


Paul

Chabu
12-08-2010, 01:49 PM
A lot simpler true, but I like my solution for the following reasons
- enumerations tend to be reused, mine are completely contained in one class module (the enum, toString and toEnum functions). I can just import that class module and use the functionality immediately.

- more importantly, although the select statement is much more verbose that the array, I can put other code in there, e.g. to capture errors like someone supplying new values in the data without adapting the enumeration (I handle it in the else part of the select case of the toEnum (toResourceType in the example))

- also how would you map a stored code back to the enum without repeating the same array lookup over and over?

Greetings