By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,931 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,931 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!

Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <am***@hotmail.com>
Subject: Can I implicitly cast an enum variable to (int) without explicit castingDate: 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: <#Z**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: ipvpn069034.netvigator.com 203.198.202.34
Path: cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!cpmsftng xa09.phx.gbl!TK2MSFTNGP08.
phx.gbl!TK2MSFTNGP09.phx.gblXref: 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

Nov 15 '05 #2

P: n/a
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[MSFT]" <ra******@online.microsoft.com> wrote in message
news:2c*************@cpmsftngxa07.phx.gbl...
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" <am***@hotmail.com>
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: <#Z**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: ipvpn069034.netvigator.com 203.198.202.34
Path:

cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!cpmsftng xa09.phx.gbl!TK2MSFTNGP08. phx.gbl!TK2MSFTNGP09.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.languages.csharp:215468X-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

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.