Solved

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

Posted on 2008-06-21
5
403 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21839555
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
ID: 21839594
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
ID: 21839637
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
ID: 21839660
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
ID: 21839666
That's done the trick, thanks a million, actually 500 points ;-))

/Michael
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

728 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