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!

Enum and ToString in VB6? 2

Status
Not open for further replies.

jtseltmann

Programmer
Jan 15, 2002
149
US
Well second question in as many days...but I had to. I have googled and read all morning and can't find a good answer that works in VB6.

I have some basic Enums that I'm trying to return the String value associated with them. I cannot seem to get anything to work. I am trying to NOT have to use a big Case statement to translate them but I have no options left.

Here is a simple snippet...

'---
Public Enum RS_FIELDNAMES
KeyField
Field1
Field2
End num
Public Enum RS_SORT
Asc
Dsc
End Enum

Public Sub RS_Sort(strField as RS_FIELDNAMES, strSort as RS_SORT)
Dim strSortString as string

strSortString = strSort.ToString()

Exit Sub
End Sub
'---

I can't get the strSortString var to show the "Asc" instead of 0. What could I be doing wrong? I have read a bunch of pages where it was outlined to just ust the .ToString function but I keep getting an error?

Thanks to anyone with some help.
 
See if the thread mentioned in this thread helps:

thread222-1257842

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Public Enum RS_SORT
Asc=0
Dsc=1
End Enum
Public mSortOrder(1) as String

Private Sub Form_Load()
mSortOrder(0)="ASC"
mSortOrder(1)="DESC"
End Sub


'Use as:
strSortString = mSortOrder(strSort)

 
Ehh...I had assumed that the app was VB and the poster just copied some code not realizing the code was VB.Net, but if the app really is VB.Net then, yeah...what strongm said.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Well, reading the name of the post, I'm getting that the OP is looking to implement the Enum and ToString functions in a VB6 context. I'll assume this is the case. Enum translates pretty much directly, see SB's code. There is no ToString method in VB6. Furthermore, as you are finding, there isn't a way to directly capture the enum name rather than the underlying value that represents it. You can do this in .Net's Enum, becuase the Enum object overrides the base ToString method provided by System.Object to specifically do this. There is no direct equivalent to all this in VB6.

So, obviously, you can do a conditional statement to evaluate the enum and determine the sort order therefrom. However, you can also set up a string array, with the same values as your enum, and get something similar to what you are looking for. Thus:
Code:
Public Enum RS_SORT
   Asc = 0
   Desc = 1
End Enum

Private SortVals(1) as String

Sub Form_Load() ' or whatever
SortVals(0) = "Asc"
SortVals(1) = "Desc"
End Sub

Function GetSortVal(SortIn as RS_SORT) as String
GetSortVal = SortVals(SortIn)
End Function

'To test it:
Private Sub Command1_Click()
Dim x As RS_SORT
x = Desc
MsgBox GetSortVal(x)
End Sub
'works for me!  :)

You might want to give that a look.

HTH

Bob
 
And also, you might want to read the thread link that EBGreen provided, as I ought to have done before answering. My suggestion is a simple way to go about it, however, it doesn't scale too well. strongm's (in EBGreen's link), while more complicated, also scales a lot better.
 
Thanks to all. Yes, its a VB6 app. I got the answer I was looking for basically and that is there is no way to do what I want to do easily. I had read numerous posts where folks had said that it was avail in VB but that was obviously wrong.

I have a way of equating out the Enum values but I was truly looking for a way of 'not' doing it. It just makes no sense to create the Enum and then have to equate the descriptions again.

Thanks all. This forum always gives me great info.
Jeff
 
>It just makes no sense to create the Enum and then have to equate the descriptions again

Not quite sure I'm with you

> I had read numerous posts where folks had said that it was avail in VB but that was obviously wrong

Theoretically it isn't and they were indeed wrong ... however, does not my code in the link in the thread pointed to by EBGreen get close?
 
strongm,
It does. I was just looking for something much easier. Thank you.
 
Stars for both of you for equally viable solutions!
thanks guys!
Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top