I am trying to convert a string or int variable to an Enum that I have set
up in my C# application. I have tried many options with no success. Any
advice would be appreciated.
Regards
Ozzy 3 11455
Jon, Note how here, z *is* a Foo, but it's not one of the "predefined" members of the enumeration. Just wanted to point out that enums aren't as type-safe as you might hope.
PMFJI: I totally agree!
However, they are much more typesafe than VB6's implementation. ;-)
I also prefer them over the constant integers in Java classes.
Just a thought
Jay
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP***********************@news.microsoft.com. .. Ozzy Knox <oz******@freemail.absa.co.za> wrote: I am trying to convert a string or int variable to an Enum that I have
set up in my C# application. I have tried many options with no success.
Any advice would be appreciated.
Sure. For ints, you just need to cast. For strings, you need Enum.Parse. For example:
using System;
public class Test { enum Foo { Bar=2, Baz=3 }
static void Main() { Foo x = (Foo)2; Foo y = (Foo) Enum.Parse(typeof(Foo), "Baz"); Foo z = (Foo)4;
Console.WriteLine ("{0} {1} {2}", x, y, z); } }
Note how here, z *is* a Foo, but it's not one of the "predefined" members of the enumeration. Just wanted to point out that enums aren't as type-safe as you might hope.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet/ If replying to the group, please do not mail me too
Jay B. Harlow [MVP - Outlook] <Ja********@email.msn.com> wrote: Jon, Note how here, z *is* a Foo, but it's not one of the "predefined" members of the enumeration. Just wanted to point out that enums aren't as type-safe as you might hope. PMFJI: I totally agree!
However, they are much more typesafe than VB6's implementation. ;-)
I don't know VB6, but I wouldn't be surprised :)
I also prefer them over the constant integers in Java classes.
Note that Java is getting enums in Java 1.5, too... I believe they're
going to be more typesafe than these, but I haven't looked at the
details yet.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jon, However, they are much more typesafe than VB6's implementation. ;-) I don't know VB6, but I wouldn't be surprised :)
VB6 allows (in C# like syntax):
Foo x = 2; // no requirement to cast the enum
Foo z = Baz; // no requirement to prefix the enum
Neither of which I care for.
I've been researching Code Critics. I think identifying casts on enums
should be flagged as a warning, especially when the number is outside the
range of values.
Code Critics should at the very least flag the following line:
Foo z = (Foo)4;
It should be interesting to see how typesafe they make Java's. And if that
could be adopted to .NET. :-)
Jay
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ... Jay B. Harlow [MVP - Outlook] <Ja********@email.msn.com> wrote: Jon, Note how here, z *is* a Foo, but it's not one of the "predefined" members of the enumeration. Just wanted to point out that enums aren't as type-safe as you might hope. PMFJI: I totally agree!
However, they are much more typesafe than VB6's implementation. ;-)
I don't know VB6, but I wouldn't be surprised :)
I also prefer them over the constant integers in Java classes.
Note that Java is getting enums in Java 1.5, too... I believe they're going to be more typesafe than these, but I haven't looked at the details yet.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet/ If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Asbjørn Ulsberg |
last post by:
Hi.
I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0
module to C#. I've managed to convert the VB code to VB.NET,
which gave me the following code:
Option Strict On
Option...
|
by: Pete Davis |
last post by:
I've never done this in C# so I don't know what the appropriate way of doing
it is.
I've got an array of bytes and I need to convert the array into "usable"
data. For example, the first 4 bytes...
|
by: gmccallum |
last post by:
I am trying to convert the value of a string to a defined enum value such as
follows.
public enum MyEnum { One, Two };
string MyString = "One";
// or even this is fine
string MyString2 =...
|
by: Robert W. |
last post by:
In a posting earlier this year I found a simple approach to convert a string
to a particular Enum value. The one line solution looked like this:
MyEnum ConvertedString = (MyEnum)...
|
by: rsine |
last post by:
I have developed a program that sends a command through the serial port
to our business system and then reads from the buffer looking for a
number. Everything worked great on my WinXP system, but...
|
by: Richy |
last post by:
Hi,
I have a class that exposes the Microsoft.DirectX.Direct3D.Compare
property, which allows a drop-down list of values (such as Never,
Always etc) to be selected via the propertygrid. I write...
|
by: MikalE |
last post by:
Hi,
I'll try and explain my problem.
I'm simulating an incoming event that in my application I get from a COM object. My problem is that I can't seem to figure out how to convert an object back...
|
by: Doug |
last post by:
I have an enum that will look kind of like this:
private enum MyEnum
{
SomeValue = 0,
AnotherValue = 1,
ThirdValue = 2
}
|
by: =?Utf-8?B?cmF5ZnVzaW9u?= |
last post by:
I am storing Keys.Right in a string and I need to convert it to back to the
Keys Enumeration so I can handle it correctly. How do I do this?
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |