Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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....

 

<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

Posted

You mean like this...

Dim myString as String = myGraph.ToString()

 

EDIT:- if you need the int value to be in the string aswell then

Dim myString As String = myGraph.ToString() & " {" & Convert.ToInt16(myGraph) & "}"

Anybody looking for a graduate programmer (Midlands, England)?
Posted (edited)

He'll need to break down the bit values, first, Cags, then convert back to the enumeration using CType...

   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

Edited by mandelbrot
Posted
You mean like this...

Dim myString as String = myGraph.ToString()

 

EDIT:- if you need the int value to be in the string aswell then

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

Posted
Did you actually try the code mandlebrot? The ToString() method of a flagged enum, returns a full string of all values that are true.
Anybody looking for a graduate programmer (Midlands, England)?
Posted

I thought it was too simple to start with, but when I checked it i thought hmm...

:D

Anybody looking for a graduate programmer (Midlands, England)?
Posted

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!

Posted

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...

xPos = -(xPos < xWinMax) + xPos

...for instance.

 

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

 

 

Paul.

Posted
But' date=' I suppose what we loose in functionality we gain in structure.[/quote']

 

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...