Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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 …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

22 Experts available now in Live!

Get 1:1 Help Now