Datatype of an enum

  • Thread starter Thread starter Brent Ritchie
  • Start date Start date
B

Brent Ritchie

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?
 
Brent Ritchie said:
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
 
Michael Bray said:
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

}

}
 
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 Marti
 
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
 

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

Back
Top