?
Solved

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

Posted on 2008-06-21
5
Medium Priority
?
404 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 2000 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

Industry Leaders: 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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 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