Can I implicitly cast an enum variable to (int) without explicit casting

B

babylon

I have an enum
public enum MyEnum : int
{
X,
Y
}

I have to do
int a = (int) MyEnum.X;
can i overload the operator or other means to do something like
int a = MyEnum.X;
??

thank you!
 
R

Rakesh Namineni[MSFT]

Hi,
Search for "TypeConverter" in MSDN and you will find examples for
converting types of values to other types. You can achieve the conversion
you need by doing something like

[TypeConverter(typeof(EnumToIntConverter))]
public enum MyEnum : int
{
X,
Y
}

public class EnumToIntConverter : EnumConverter
{
// Override the appropriate methods.
}

Hope this helps
--------------------
From: "babylon" <[email protected]>
Subject: Can I implicitly cast an enum variable to (int) without explicit casting
Date: Tue, 27 Jan 2004 09:50:28 +0800
Lines: 17
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: ipvpn069034.netvigator.com 203.198.202.34
Path: cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.
phx.gbl!TK2MSFTNGP09.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.languages.csharp:215468
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

I have an enum
public enum MyEnum : int
{
X,
Y
}

I have to do
int a = (int) MyEnum.X;
can i overload the operator or other means to do something like
int a = MyEnum.X;
??

thank you!


Rakesh, EFT.

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
B

babylon

from MSDN
Remarks
This converter can only convert an enumeration object to and from a string.
The EnumConverter provides the Comparer property to get an IComparer
interface that can be used to sort the values of the enumeration. By
default, the enumeration values are sorted in the order they appear in the
file.



"This converter can only convert an enumeration object to and from a string"

I would like an implicit conversion from enum to int...is that possible?

thank you

Rakesh Namineni said:
Hi,
Search for "TypeConverter" in MSDN and you will find examples for
converting types of values to other types. You can achieve the conversion
you need by doing something like

[TypeConverter(typeof(EnumToIntConverter))]
public enum MyEnum : int
{
X,
Y
}

public class EnumToIntConverter : EnumConverter
{
// Override the appropriate methods.
}

Hope this helps
--------------------
From: "babylon" <[email protected]>
Subject: Can I implicitly cast an enum variable to (int) without explicit casting
Date: Tue, 27 Jan 2004 09:50:28 +0800
Lines: 17
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: ipvpn069034.netvigator.com 203.198.202.34
Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.
phx.gbl!TK2MSFTNGP09.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.languages.csharp:215468
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

I have an enum
public enum MyEnum : int
{
X,
Y
}

I have to do
int a = (int) MyEnum.X;
can i overload the operator or other means to do something like
int a = MyEnum.X;
??

thank you!


Rakesh, EFT.

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top