472,805 Members | 1,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

Datatype of an enum

Hello all,

I have been wondering is it possible to get the datatype of an
enumeration without using reflection. To clearify if I do:

enum Genders
{
Male,
Female
}

What I want is to return the value Genders not System.Int32. Is there a
way of doing this without reflection or should I add reflection to my
program to handle this. If I use reflection what should I be looking for to
help with this?
Jan 20 '06 #1
4 1704
"Brent Ritchie" <br**********@personainternet.com> wrote in
news:XZ********************@news20.bellglobal.com:
I have been wondering is it possible to get the datatype of an
enumeration without using reflection. To clearify if I do:

enum Genders
{
Male,
Female
}

What I want is to return the value Genders not System.Int32. Is
there a
way of doing this without reflection or should I add reflection to my
program to handle this. If I use reflection what should I be looking
for to help with this?


Why would you want to do this? Couldn't you just use a hard-coded string?
Enum's aren't like classes. If you have an enum variable, you always know
what type of enum it is. I can't imagine any situation in which you would
treat it as a generic enum (or if its even possible).

-mdb
Jan 20 '06 #2
> Hello all,

I have been wondering is it possible to get the datatype of an
enumeration without using reflection. To clearify if I do:

enum Genders
{
Male,
Female
}
What I want is to return the value Genders not System.Int32. Is
there a way of doing this without reflection or should I add
reflection to my program to handle this. If I use reflection what
should I be looking for to help with this?


I'm not understanding your question at all.

Genders male = Genders.Male

public Genders GetGender()
{
return Genders.Male;
}

What is the issue you're having?

Chris Martin
Jan 20 '06 #3

"Michael Bray" <mbray@makeDIntoDot_ctiusaDcom> wrote in message
news:Xn****************************@207.46.248.16. ..
"Brent Ritchie" <br**********@personainternet.com> wrote in
news:XZ********************@news20.bellglobal.com:
I have been wondering is it possible to get the datatype of an
enumeration without using reflection. To clearify if I do:

enum Genders
{
Male,
Female
}

What I want is to return the value Genders not System.Int32. Is
there a
way of doing this without reflection or should I add reflection to my
program to handle this. If I use reflection what should I be looking
for to help with this?


Why would you want to do this? Couldn't you just use a hard-coded
string?
Enum's aren't like classes. If you have an enum variable, you always know
what type of enum it is. I can't imagine any situation in which you would
treat it as a generic enum (or if its even possible).

-mdb


The thing is I'm trying to create a groupbox that acts much like the
groupbox in access 2000 with the Value property. But now that I have done
that I want to automate loading the groupbox with an enumeration. I have my
radio buttons being populated properly but I want to automate loading the
text property of the Groupbox. Here is the code for my new groupbox maybe
someone can help point out a way of doing this that won't be terribly taxing
for the user.

using System;

using System.Drawing;

using System.Windows.Forms;

namespace OptionSelectBox

{

/// <summary>

/// OptionSelectBox.

/// </summary>

public class OptionSelectBox : System.Windows.Forms.GroupBox

{
public OptionSelectBox()

{

//

// The InitializeComponent() call is required for Windows Forms designer
support.

//

InitializeComponent();

}
public Enum DataSource

{

set

{

if (this.Controls.Count < 1)

{

System.Array values = Enum.GetValues(value.GetType());

for (int i = 0; i < values.Length; i++)

{

System.Windows.Forms.RadioButton rdoButton = new
System.Windows.Forms.RadioButton();

rdoButton.Left = 5;

if (this.Controls.Count > 0)

{

rdoButton.Top = this.Controls[this.Controls.Count - 1].Top +
this.Controls[this.Controls.Count - 1].Height + 5;

}

else

{

rdoButton.Top = 20;

rdoButton.Checked = true;

// I want to set the groupbox name here.

}

rdoButton.Tag = values.GetValue(i);

rdoButton.Text = rdoButton.Tag.ToString();

this.Controls.Add(rdoButton);

}

}

this.Width = this.Controls[0].Width + 10;

this.Height = this.Controls.Count * (this.Controls[0].Height + 5) + 20;

}

}
public object Value

{

get

{

foreach (System.Windows.Forms.RadioButton rdoButton in this.Controls)

{

if (rdoButton.Checked == true)

{

return rdoButton.Tag;

}

}
return null;
}

}
#region Windows Forms Designer generated code

/// <summary>

/// This method is required for Windows Forms designer support.

/// Do not change the method contents inside the source code editor. The
Forms designer might

/// not be able to load this method if it was changed manually.

/// </summary>

private void InitializeComponent() {

//

// UserControl1

//

this.Name = "OptionSelectBox";

this.Size = new System.Drawing.Size(120, 80);

}

#endregion

}

}
Jan 20 '06 #4
I think what you might be looking for is one of these:

[untested code]
string[] names = Enum.GetNames(typeof(Gender));
Array values = Enum.GetValues(typeof(Gender));

Scott

Brent Ritchie wrote:
Hello all,

I have been wondering is it possible to get the datatype of an
enumeration without using reflection. To clearify if I do:

enum Genders
{
Male,
Female
}

What I want is to return the value Genders not System.Int32. Is there a
way of doing this without reflection or should I add reflection to my
program to handle this. If I use reflection what should I be looking for to
help with this?

Jan 23 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
1
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
2
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
4
by: UJ | last post by:
I have a datatype that I want to use as a type for a column (It's an enumeration.) but I can't get the syntax right. Can you even do it? Or do I need to do it as a string and convert it back and...
1
by: Bryan | last post by:
I have a class called "Prop". I want that class to have a property called "DataType" where the user can select and store a datatype. How can I store a DataType value in a class property. how...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
3
by: Johnny J. | last post by:
Does anybody know if VS 2005 contains an enum of the datatypes available in an SQL Server database? Cheers Johnny J.
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.