about dynamic change a expressions in methods
-
hello, for example, i have three Textbox and a button int the Form(textBox1,textBox2,textBox3, button2). private void button2_Click(object sender, EventArgs e) { int temp; temp = int.Parse(textBox2.Text) + int.Parse(textBox1.Text); textBox3.Text = temp.ToString(); } A question is : i have two roles (system administrator and commmon user) in this software. if i have written this button2_Click(), can i let system administrator change button2_Click() when software is running. eg,temp = int.Parse(textBox2.Text) * int.Parse(textBox1.Text); instead of temp = int.Parse(textBox2.Text) + int.Parse(textBox1.Text); thank you
-
hello, for example, i have three Textbox and a button int the Form(textBox1,textBox2,textBox3, button2). private void button2_Click(object sender, EventArgs e) { int temp; temp = int.Parse(textBox2.Text) + int.Parse(textBox1.Text); textBox3.Text = temp.ToString(); } A question is : i have two roles (system administrator and commmon user) in this software. if i have written this button2_Click(), can i let system administrator change button2_Click() when software is running. eg,temp = int.Parse(textBox2.Text) * int.Parse(textBox1.Text); instead of temp = int.Parse(textBox2.Text) + int.Parse(textBox1.Text); thank you
Sure you can. The simplest way is to use a boolean (see below) but there are more complex and elaborate methods if there is a lot of role dependent behavior in your system.
bool isAdmin = false; // TODO: set this to an actual value on startup
private void button2_Click(object sender, EventArgs e)
{
int temp;
if (isAdmin)
{
temp = int.Parse(textBox2.Text) * int.Parse(textBox1.Text);
}
else
{
temp = int.Parse(textBox2.Text) + int.Parse(textBox1.Text);
}
textBox3.Text = temp.ToString();
}