I searched for how to use enumeration and wrote the below code.
But I'm not sure whether this is a right way or not.
If someone find another ways or good websites, please let me know:D
Misa
Form
private void btnDisplay1_Click(object sender, EventArgs e)
{
SimpleCardEnum card = new SimpleCardEnum(int.Parse(txtCardValue.Text));
lblResult.Text = card.getInfo();
}
private void btnDisplay2_Click(object sender, EventArgs e)
{
SimpleCardEnum card = new SimpleCardEnum(txtCardName.Text.ToUpper());
lblResult.Text = card.getInfo();
}
private void btnDisplayAll_Click(object sender, EventArgs e)
{
SimpleCardEnum card = new SimpleCardEnum();
lblResult.Text = card.getAllCardName();
}
Class
public class SimpleCardEnum
{
//instance variables
private int cardValue;
//static fields
public static int CardInASet = 13;
//enumerations
public enum CardName
{
ACE = 1,
TWO = 2,
THREE = 3,
FOUR = 4,
FIVE = 5,
SIX = 6,
SEVEN = 7,
EIGTH = 8,
NINE = 9,
TEN = 10,
JACK = 11,
QUEEN = 12,
KING = 13
}
//properties
public int CardValue
{
get { return cardValue; }
set { cardValue = value; }
}
//constructors
public SimpleCardEnum()
{
cardValue = 1;
}
public SimpleCardEnum(int cv)
{
cardValue = cv;
}
public SimpleCardEnum(string cn)
{
cardValue = (int)Enum.Parse(typeof(SimpleCardEnum.CardName), cn);
}
//methods
public string getCardName()
{
return Enum.GetName(typeof(SimpleCardEnum.CardName), cardValue);
}
public string getInfo()
{
return getCardName() + " " + cardValue;
}
public string getAllCardName()
{
string names = "";
foreach (string name in Enum.GetNames(typeof(SimpleCardEnum.CardName)))
{
names += name + ", ";
}
return names;
}
}
Thanks for the post. I was about to start this exercise :)
ReplyDeleteMisa, could you send me the homework for Allan and Shanti concerning C#? I have only the Javascript homework. Thanks :)