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:
Related
destroyComponentPacket.Component is an IComponent (an interface). I want to determine the specific class under it automatically, without having to do it manually using switch(). What are the options for doing so?
switch (destroyComponentPacket.Component)
{
case Tile t:
destroyComponentEntity.Detach<Tile>();
break;
case CollisionActor ca:
destroyComponentEntity.Detach<CollisionActor >();
break;
case Grass g:
destroyComponentEntity.Detach<Grass>();
break;
case Tree t:
destroyComponentEntity.Detach<Tree>();
break;
case Wall w:
destroyComponentEntity.Detach<Wall>();
break;
default:
break;
}
Im creating a Hotkey funtion atm so users can adjust Keybindings to their own needs, before i set the keys and they were not adjustable. This is what i used before.
public void gHook_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.D:
// stuff
Salvagebtn.PerformClick();
break;
case Keys.F12:
// stuff
pausebtn.PerformClick();
break;
case Keys.K:
//stuff
Geardropbtn.PerformClick();
break;
case Keys.F:
//stuff
Gamblebtn.PerformClick();
break;
case Keys.A:
//stuff
LeftClickSpambtn.PerformClick();
break;
case Keys.H:
// stuff
openGRbtn.PerformClick();
break;
case Keys.B:
//stuff
gemupbtn.PerformClick();
break;
}
}
Now i made a setting Tab where ppl can change Keybindings which looks like this.
How can i get it going that instead of the "case Keys.D:" it will react to the Keybinding in the Textbox ?
A few ways to do it. One way you could go would be to assign the keybindings to a Dictionary where UserActions is an enum defining the possible action:
public enum UserAction
{
Salvage, Pause, Drop // etc...
}
Then when the user presses a key, something like:
public void gHook_KeyDown(object sender, KeyEventArgs e)
{
string k = e.KeyCode.ToString();
if (KeyBindings.Contains(k)) //KeyBindings is your field or variable which is a Dictionary<string, UserAction>
{
var action = KeyBindings[k]
switch (action)
{
case UserAction.Salvage:
// stuff
Salvagebtn.PerformClick();
break;
case UserAction.Pause:
// stuff
pausebtn.PerformClick();
break;
// and so on...
Assigning values to the dictionary:
var KeyBindings = new Dictionary<string, UserAction>;
KeyBindings["D"] = UserAction.Salvage;
KeyBindings["A"] = UserAction.Pause;
If you need to get a key from the user's input, you can use Enum.TryParse(input):
Keys key;
if (Enum.TryParse<Keys>(textBoxSalvage.Text, out key))
{
KeyBinding[textBoxSalvage.Text] = UserAction.Salvage; // note that 'UserAction.something' comes from the context
}
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.
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;
}
}