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

Datatype of an enum

P: n/a
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
Share this Question
Share on Google+
4 Replies


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

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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.