kejpa Posted February 14, 2006 Posted February 14, 2006 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 Quote
mandelbrot Posted February 14, 2006 Posted February 14, 2006 Have you tried breaking the value into constituent parts then performing a CType on each part, then .ToString? Quote
Cags Posted February 14, 2006 Posted February 14, 2006 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) & "}" Quote Anybody looking for a graduate programmer (Midlands, England)?
mandelbrot Posted February 14, 2006 Posted February 14, 2006 (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 February 14, 2006 by mandelbrot Quote
kejpa Posted February 14, 2006 Author Posted February 14, 2006 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 Quote
Cags Posted February 14, 2006 Posted February 14, 2006 Did you actually try the code mandlebrot? The ToString() method of a flagged enum, returns a full string of all values that are true. Quote Anybody looking for a graduate programmer (Midlands, England)?
Cags Posted February 14, 2006 Posted February 14, 2006 I thought it was too simple to start with, but when I checked it i thought hmm... :D Quote Anybody looking for a graduate programmer (Midlands, England)?
mandelbrot Posted February 14, 2006 Posted February 14, 2006 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! Quote
Administrators PlausiblyDamp Posted February 14, 2006 Administrators Posted February 14, 2006 You may also want to look at Sytstem.Enum.Parse(...) - allows you to go the other direction and take the string and convert it back into an enum! Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mandelbrot Posted February 15, 2006 Posted February 15, 2006 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. Quote
Wraith Posted February 15, 2006 Posted February 15, 2006 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.