Flag type enum to string

kejpa

Junior Contributor
Joined
Oct 10, 2003
Messages
320
Hi,
I'd like to convert a enum value to it's string representation. It's easy if it's a value within the enum, the problem I'm up against is that it's a combination of values....

Visual Basic:
	<Flags()> Public Enum eGraphType As Integer
		gtNone = 0
		gtLine = 1
		gtArea = 2
		gtIndicator = 4
		gtRadar = 8
		gtPolar = 16
	End Enum

if I have myGraph = 7 I can see in the local window
myGraph - gtLine, gtArea, gtIndication {7}

How can I get this into a string variable?!?
Dim myString as string=myGraph

TIA
/Kejpa
 
You mean like this...
Visual Basic:
Dim myString as String = myGraph.ToString()

EDIT:- if you need the int value to be in the string aswell then
Visual Basic:
Dim myString As String = myGraph.ToString() & " {" & Convert.ToInt16(myGraph) & "}"
 
He'll need to break down the bit values, first, Cags, then convert back to the enumeration using CType...
Visual Basic:
    Private Sub Test(ByVal pSender As Object, ByVal pE As EventArgs) Handles Button1.Click
        Dim flagTest As Integer
        flagTest = eGraphType.gtArea Or eGraphType.gtPolar
        TextBox1.Text = CType(flagTest And eGraphType.gtPolar, eGraphType).ToString()
    End Sub
 
Last edited:
Cags said:
You mean like this...
Visual Basic:
Dim myString as String = myGraph.ToString()

EDIT:- if you need the int value to be in the string aswell then
Visual Basic:
Dim myString As String = myGraph.ToString() & " {" & Convert.ToInt16(myGraph) & "}"
Yes,
that's what I needed, and thought that I had tried. I almost made a wise-guy remark of it but decided to give it one more shot.

Thank you, now I'm officially todays Fool-of-the-day.
/Kejpa
 
LOL! Yeah - the version I wrote simply gets each bit if it's been set, otherwise it will return gtNone...

I've never seen that done before! Still getting used to the power of VB.NET - it's quite unique!
 
Guess I'm still too used to doing things the old VB way...

I've got to say there were some useful tricks with VB6 that I haven't managed to get to work so well with VB.NET. One of them, for instance, was using Boolean values within calculations. Rather than using if to determine the result of a Boolean, I could use it directly in the calculation...
Visual Basic:
xPos = -(xPos < xWinMax) + xPos
...for instance.

But, I suppose what we loose in functionality we gain in structure.


Paul.
 
mandelbrot said:
But, I suppose what we loose in functionality we gain in structure.

What you lose in shortcuts you gain in safety perhaps. The expression you gave abuses or misuses the fact that boolean in mapped into an integer value and isn't type safe. The fact that in almost every system you'll ever use 0 if considered false and other values are true doesn't mean that you should you this fact unless the language explicity defines it. If MS had decided for some reason to suddenly change the true/false value mappings you'd find your expression failing where if you actually resolve to a boolean answer it would continue to work. Less code doesn't mean a better program, inferance may work much of the time but it only has to fail once and you're lost.
 
Back
Top