Solved

How to convert enumeration string to an actual enumertion (Int16) value

Posted on 2008-06-21
5
398 Views
Last Modified: 2009-12-16
Hi guys,
I need to convert a string representation of an enumeration to an actual (Int16) value.
I tried the following but with 'Option Strict On' I got an error so my question is this:
- is there an inbuilt function to do that or so I need to implement a function to do that?

/Michael
 

Friend Enum ShowPageImage As Short
     None = 0
     Header = 1
     Footer = 2
     Both = 3
End Enum

Dim MyVal as ShowPageImage = CType("Both", ShowPageImage)
0
Comment
Question by:gem56
  • 3
  • 2
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
you can use directcast:

Dim MyVal as ShowPageImage= DirectCast(.InnerText, ShowPageImage)

to an actual Int16 should be:

Dim MyVal as Int16 = DirectCast(.InnerText, Int16)
0
 

Author Comment

by:gem56
Comment Utility
I've tried your suggestion and I get the following error message 'Value of type 'String' cannot be converted to '...ShowPageImage'.

What you're saying makes sense but what's happening here doesn't. Yesterday I adjusted my code to comply with  'Option Strict On' requirements and today I started having this problem but now even if I try running old code (Me.ShowOtherPagesImage = .InnerText) I also get the same error.

It's like as if I did something with switches to cause that problem. I'm not getting any other problems and if I use '= ShowPageImage.Both' then all is OK.

It's really weird!!!!!

/Michael


     
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
Comment Utility
hmm, if DirectCast does not do the trick, you can use Parse:

Dim MyVal as ShowPageImage= CType([Enum].Parse(GetType(ShowPageImage), .InnerText), ShowPageImage)
0
 

Author Comment

by:gem56
Comment Utility
Actually I just found why the old code doesn't work, it's to do with my adjustments for 'Option Strict On' compliance. As you may have gathered, the data being used is part of information being saved to an XML file and later read back in.

Previously I saved that (ShowMainPageImage) data using the following statement:
    myXMLWriter.WriteElementString("ShowMainPageImage", Me.ShowMainPageImage)  

After the 'Option Strict On' adjustment the same statement became this:
    myXMLWriter.WriteElementString("ShowMainPageImage", Me.ShowMainPageImage.ToString)  

So obviously in the past I was saving the actual Int16 value whereas now I save a string value. When I read an old file using new app it worked OK but as soon as I did a save, saving a string instead of Int16 using the new app, I could no longer read the file properly.

I would prefer to save a string instead of a value so I sill welcome any comments on how to make the conversion to work.

/Michael
0
 

Author Comment

by:gem56
Comment Utility
That's done the trick, thanks a million, actually 500 points ;-))

/Michael
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now