Why does the following work (compile):
public void SaveCurrentTab(string currentTabIndex)
{
if (currentTabIndex == MainInfoPnl.ClientID)
PartialSave1();
else if (currentTabIndex == ContactInfoPnl.ClientID)
PartialSave2();
else if (currentTabIndex == BankInfoPnl.ClientID)
PartialSave3();
else if (currentTabIndex == ServicesPnl.ClientID)
PartialSave4();
else if (currentTabIndex == AttachmentsPnl.ClientID)
PartialSave5();
}
But, the following does not?
public void SaveCurrentTab(string currentTabIndex)
{
switch (currentTabIndex)
{
case MainInfoPnl.ClientID:
PartialSave1();
break;
case ContactInfoPnl.ClientID:
PartialSave2();
break;
case BankInfoPnl.ClientID:
PartialSave3();
break;
case ServicesPnl.ClientID:
PartialSave4();
break;
case AttachmentsPnl.ClientID:
PartialSave5();
break;
}
}
Presumably because MainInfoPnl.ClientID isn't a compile-time constant. It may be public static readonly for example, which isn't the same as const. If that doesn't help, show us more about the code and the error message you're getting.
The case statements must be constant strings for a switch - an if condition has no such restriction.
A switch in C# only works with constant values. For values determined at runtime, use a if-else construction.
Try this:
public void SaveCurrentTab(string currentTabIndex)
{
switch (Convert.ToInt32(currentTabIndex))
{
case (int)MainInfoPnl.ClientID:
PartialSave1();
break;
case (int)ContactInfoPnl.ClientID:
PartialSave2();
break;
case (int)BankInfoPnl.ClientID:
PartialSave3();
break;
case (int)ServicesPnl.ClientID:
PartialSave4();
break;
case (int)AttachmentsPnl.ClientID:
PartialSave5();
break;
}
}
Related
I work on csharp i face issue i can't access element inside switch case statement as
case RoleNames.REQ:
so How to do that ?
I create enum as below :
public enum RoleNames
{
REQ,
HRED,
PLNG,
REC,
CEO
}
2- I call enum to get first element by switch case on Main function console .
static void Main(string[] args)
{
RoleNames rn=new RoleNames();
switch (rn)
{
case RoleNames.REQ:
break;
case RoleNames.HRED:
break;
case RoleNames.PLNG:
break;
case RoleNames.REC:
break;
case RoleNames.CEO:
break;
default:
break;
}
}
I need to access first element on switch as
RoleNames.REQ:
when debug code above switch go direct to default break
so what change i can do on my code to access first element case RoleNames.REQ:
If I have an Enum as follows:
private object myEnumValLock = new object();
private MyEnum _myEnumVal;
public MyEnum MyEnumVal
{
get
{
lock(this.myEnumValLock)
{
return this._myEnumVal;
}
}
set
{
lock(this.myEnumValLock)
{
if (value != this._myEnumVal)
{
this.HandleNewMyEnumVal(this._myEnumVal, value);
this._myEnumVal = value;
}
}
}
}
When using switch case, can I directly use the property like this:
private void MyFunc()
{
switch (this.MyEnumVal)
{
case MyEnum.First:
// Do Something
break;
case MyEnum.Second:
// Do Something
break;
}
}
Or should I read it first and then use switch on the read value like this:
private void MyFunc()
{
var myEnumVal = this.MyEnumVal;
switch (myEnumVal)
{
case MyEnum.First:
// Do Something
break;
case MyEnum.Second:
// Do Something
break;
}
}
If using if ... else as in this question, I'd need to read the value first. Is it the same case with switch statement? What is the behaviour of the switch statement? Does it read the value at every case statement or reads only once at the beginning?
As #mjwills suggested, I put a breakpoint in the getter and it got hit only once at the beginning of the switch statement. I can't find any specific reference where it says the switch statement reads the value only once (please comment if you find the reference). But the breakpoint does prove it.
So, although you need to have read the value beforehand in case of if ... else, in case of the switch statement, you don't need to.
Can someone please tell me why my switch statement is not working? In my program I have a text box with the choices of 'Billing', 'Tech Support', 'Retention', 'Customer Service'. My switch statement is saying if that text box is = xxx then show a form I build and hide everything else.
Currently only the billing and other option works?!
<
private void cbDept_SelectedIndexChanged(object sender, EventArgs e)
{
string reason = cbDept.Text;
switch (reason)
{
case "Billing":
gbBilling.Show();
gbTechSupport.Hide();
gbCS.Hide();
gbRetention.Hide();
lbOtherReason.Hide();
txtOther.Hide();
break;
case "Tech Support":
gbTechSupport.Show();
gbBilling.Hide();
gbCS.Hide();
gbRetention.Hide();
lbOtherReason.Hide();
txtOther.Hide();
break;
case "Retention":
gbRetention.Show();
gbBilling.Hide();
gbTechSupport.Hide();
gbCS.Hide();
lbOtherReason.Hide();
txtOther.Hide();
break;
case "Customer Service":
gbCS.Show();
gbBilling.Hide();
gbTechSupport.Hide();
gbRetention.Hide();
lbOtherReason.Hide();
txtOther.Hide();
break;
case "Other":
txtOther.Show();
lbOtherReason.Show();
gbCS.Hide();
gbBilling.Hide();
gbTechSupport.Hide();
gbRetention.Hide();
break;
default:
gbCS.Hide();
gbBilling.Hide();
gbTechSupport.Hide();
gbRetention.Hide();
break;
}
}
>
It seems to me that there is some misspelling in the other options. Check the values of reason before reaching the switch.
apologies for the vague title, I'm having a few issues with if statements in my program, this is not the exact code but should still outline my issues, in my program I have this:
if(entry.Contains("a"))
{
Console.WriteLine("a");
}
if(entry.Contains("b"))
{
Console.WriteLine("b");
}
if(entry.Contains("c"))
{
Console.WriteLine("c");
}
else
{
Console.WriteLine("d");
}
So my issue is, if I was to type a or b(the first two if statements), the else statement would still occur but if I was to type c, the else statement would not occur but I want it to act so that if i type a, b or c the else statement would not occur.
Any help would be much appreciated.
You want
if(entry.Contains("a"))
{
Console.WriteLine("a");
}
else if(entry.Contains("b"))
{
Console.WriteLine("b");
}
else if(entry.Contains("c"))
{
Console.WriteLine("c");
}
else
{
Console.WriteLine("d");
}
Additionally, if you are using all the same variable types (i.e. string or int) you can also use a case statement. It does the exact same thing, it's just a bit neater:
switch(stringName)
{
case "a":
Console.Writeline("a");
break;
case "b":
Console.Writeline("b");
break;
case "c":
Console.Writeline("c");
break;
case "d":
Console.Writeline("d");
break;
default:
Console.Writeline("none of the above");
break;
}
I don't think it'll work with entry.Contains() though.
And I'm doing some exercises about switch. I just did it from console application and I would like to do it in window forms applications. I'm looking for syntax on how to do switch in window forms.
In console it's usually like this:
switch (wordValue)
{
case 1:
Console.WriteLine("You have entered numbered two");
break;
default:
break;
how can I do this in my window forms, if I would like to display this cases in listbox1?
Thanks
=======
Thank you. I tried this one but I'm getting an error. This is what I've tried:
public static void WriteNumber(int wordValue)
{
switch (wordValue)
{
case 1:
listbox.Items.Add("You have entered number one");
break;
}
}
========
This is the code I'm trying to do:
private void btnOk_Click(object sender, EventArgs e)
{
string strUserInputNumber;
strUserInputNumber = textBox1.Text.Trim();
Int32 intNumber;
if (Int32.TryParse(textBox1.Text, out intNumber))
{
listBox1.Items.Add(intNumber.ToString());
}
}
public static void WriteNumber(int wordValue)
{
switch (wordValue)
{
case 1:
this.listBox1.Items.Add("You have entered numbered one");
break;
}
}
====
This is the new code:
private void btnOk_Click(object sender, EventArgs e)
{
string strUserInputNumber;
strUserInputNumber = textBox1.Text.Trim();
Int32 intNumber;
if (Int32.TryParse(textBox1.Text, out intNumber))
{
listBox1.Items.Add(intNumber.ToString());
WriteNumber(intNumber);
}
else
{
MessageBox.Show("Please enter an integer not a character");
}
}
public void WriteNumber(int wordValue)
{
switch (wordValue)
{
case 1:
listBox2.Items.Add("You have entered numbered one");
break;
case 2:
listBox2.Items.Add("You have entered numbered two");
break;
case 3:
listBox2.Items.Add("You have entered numbered three");
break;
default:
listBox2.Items.Add("You have exceeded the range of 1-3. Please enter the number between 1-3");
break;
}
The switch/case syntax is identical between WinForms and a console app (or any other type of application or class library), the only difference is how you display the data. If you want to add a string to a listbox (which is apparently what you're asking), it's as simple as
listBox1.Items.Add("Here is the text of the list box item");
This should work:
public void WriteNumber(int wordValue)
{
switch (wordValue)
{
case 1:
listbox.Items.Add("You have entered number one"); break;
}
}
You need to remove the static keyword to get access to the listbox, which is an instance variable.
This works fine:
switch (wordValue)
{
case 1:
this.listBox1.Items.Add("You have entered numbered two");
break;
default:
break;
}