Solved

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

Posted on 2008-06-21
5
402 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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