April 15, 2014 (Tuesday)
namespace Enumerations
{
//With Enumerations you can create your own type.
public enum BankingTransaction
{
Deposit, Withdraw, Transfer
}
//the enum values: Deposit, Withdraw, and Transfer are
//called the enum constants.
//The Banking Transaction is now considered a data type
//by c# (your own data type)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
//declare a variable of type Banking Transaction
BankingTransaction transaction1;
//assign a value to an enum variable
transaction1 = BankingTransaction.Deposit;
BankingTransaction transaction2 = BankingTransaction.Withdraw;
// BankingTransaction transaction3 = BankingTransaction.Transfer;
//convert enum constant to a string
string strTransaction1 = transaction1.ToString();
//convert enum constant to integer
//use the (int) cast
int t1 = (int)transaction1;
richTextBox1.Text = "string value: " + strTransaction1 + "\n" + "integer value: " + t1 + "\n\n" +
"string value: " + transaction2.ToString() + "\n" + "integer value: " + (int)transaction2;
}
private void Form1_Load(object sender, EventArgs e)
{
//Convert the BankingTransaction enum to a string array
string[] transactions = Enum.GetNames(typeof(BankingTransaction));
//load/ populate the combobox with this array
cboBanking.Items.AddRange(transactions);
cboBanking.SelectedIndex = 0;
string[] Days = Enum.GetNames(typeof(DayOfWeek));
cboDayOfWeek.Items.AddRange(Days);
cboDayOfWeek.SelectedIndex = 0;
string[] Color = Enum.GetNames(typeof(ConsoleColor));
comboBox1.Items.AddRange(Color);
comboBox1.SelectedIndex = 0;
}
private void btnGetSelectedT_Click(object sender, EventArgs e)
{
string selTransac = cboBanking.Text;
//you read the transaction from the combo box as a string type,
//but most likely you need to convert back to an enum type
//(BankingTransaction type)
//Parse the string read the combobox into
//the BankingTransaction type
BankingTransaction transaction = (BankingTransaction)Enum.Parse(typeof(BankingTransaction), selTransac);
richTextBox1.Text = selTransac;
}
private void button3_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
//declare a variable of type DayOfWeek
DayOfWeek Monday = DayOfWeek.Monday;
//assign a value to an enum variable
DayOfWeek Tuesday = DayOfWeek.Tuesday;
DayOfWeek Wednesday = DayOfWeek.Wednesday;
//convert enum constant to a string
string Day = Monday.ToString();
//convert enum constant to integer
//use the (int) cast
int d1 = (int)Monday;
richTextBox1.Text = "Day: " + Monday + "\n" + "Index value: " + d1 + "\n\n" +
"Day: " + Tuesday.ToString() + "\n" + "Index value: " + (int)Tuesday + "\n\n" + "Day: " + Wednesday.ToString() + "\n" + "Index Value: "
+ (int)Wednesday;
}
private void btnGetSelectedDay_Click(object sender, EventArgs e)
{
string Day = cboDayOfWeek.Text;
DayOfWeek DayOfW = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), Day);
richTextBox1.Text = Day;
}
private void btnColor_Click(object sender, EventArgs e)
{
string Color = comboBox1.Text;
ConsoleColor choosenColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), Color);
richTextBox1.Text = Color;
}
}
}
//Lab Assignment
//Complete code for the DayOfWeek enumaration
//Declare/Assign 3 DayOfWeek variables
//Display their string an int values
//and a second combobox and populate it with DayOfWeek enumeration
//Add a button to read the selected day convert it back to its original enum type
//.Net defines a ConsoleColor enumeration
//Add a third combobox and populate it with all the color values defined in the Consolecolor enum
//Add button to get the selected color and convert it to its original enum type.

No comments:
Post a Comment