Problem:
Create an enumeration named Month that holds values for the
months of the year, starting with JANUARY equal to 1. (Recall
that an enumeration must be placed within a class but outside
of any method.) Using a GUI interface, prompt the user for a
month integer. Convert the user's entry to a Month value and
display it. Save the project as MonthNamesGUI.
Thats what I have found in google, and i just copy pate it, but I dont know where go to next....
int input, output;
enum month : byte { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
input = Convert.ToInt32(inPutTextBox);
Loading
VulpesPosted Dec 13, 2011, 1:57 PM
Jignesh TrivediPosted Jan 5, 2012, 3:24 AM
try folliwing code.
public static string GetEnumDescription(Enum value)
{
if (value == null)
return string.Empty;
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null &&
attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
public enum MonthName
{
[Description("None")]
None =0 ,
[Description("January")]
Jan = 1,
[Description("February")]
Feb,
[Description("March")]
March,
[Description("April")]
April,
[Description("May")]
May,
[Description("June")]
June,
[Description("July")]
July,
[Description("August")]
Aug,
[Description("September")]
Sept,
[Description("October")]
Oct,
[Description("November")]
Nov,
[Description("December")]
Dec
}
And finally
in your application
string monthName = GetEnumDescription(MonthName.Jan);
hope this help.
VulpesPosted Dec 13, 2011, 2:27 PM
string monthName = Enum.GetName(typeof(Month), monthNumber);
Prime bPosted Dec 13, 2011, 2:07 PM
Prime bPosted Dec 13, 2011, 12:37 PM
enum month { Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
private void button1_Click(object sender, EventArgs e)
{
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int o = 5;
int f = 6;
int g = 7;
int h = 8;
int i = 9;
int j = 10;
int k = 11;
int l = 12;
int input;
a = (int)month.Jan;
b = (int)month.Feb;
c = (int)month.Mar;
d = (int)month.Apr;
o = (int)month.May;
f = (int)month.Jun;
g = (int)month.Jul;
h = (int)month.Aug;
i = (int)month.Sep;
j = (int)month.Oct;
k = (int)month.Nov;
l = (int)month.Dec;
input = Convert.ToInt32(inPutTextBox.Text);
my problem: How do I convert the input to the letter that represents the month?
Prime bPosted Dec 12, 2011, 10:00 PM
Prime bPosted Dec 12, 2011, 9:57 PM
Sam HobbsPosted Dec 12, 2011, 9:30 PM
This obviously is for a class assignment. So this is a great way for you to learn how to use the documentation. Note that you need to do the "starting with JANUARY equal to 1" part. Please look at enum (C# Reference). The documentation makes it extremely easy to figure out how to specify that January is 1.
Next, note that inPutTextBox is the TextBox. You need the text that is in the TextBox. Look at the documentation to determine how to do that.
Next, note that month is a type; it is not an instance of something. It is not a place that holds a value; it just describes what a month is. You need to create an instance of a month. Try to figure out how to do that. It will really help you a lot if you can figure it out yourself. When you have an instance of a month, then you can put the converted number (converted using Convert.ToInt32) in that.
I am not sure I understand the part about "display it".
Prime bPosted Dec 12, 2011, 9:11 PM
Prime bPosted Dec 12, 2011, 9:11 PM