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.
Related
I have like 20 objects with the same script. And the problem that when I want to set a value for every each object only one respond to the script. here is the objects main script
[SerializeField]
private int Id = 0;
public void SetId(int num)
{
Id = num;
}
public int ReturnId()
{
return Id;
}
And here where they are called:
private void DeterminedWallColor()
{
for (int i = 0; i < _colorType.Length; i++)
{
switch (_colorType[i])
{
case 1:
_wId.SetId(_colorType[i]);
break;
case 2:
_wId.SetId(_colorType[i]);
break;
case 3:
_wId.SetId(_colorType[i]);
break;
case 4:
_wId.SetId(_colorType[i]);
break;
}
}
}
The problem is you shouldn't be using a switch statement. The code you have right now will always match the first case because all the cases below it are the same as the first case and will always be the same as the firsts case because it is the same code/expression in all the cases you are checking for.
Also since you want to set a value for each gameObject(assuming you want to set the Id), you would need to loop thru all the gameObjects first, and then set each _colorType per gameObject, according to what you want/need. (I cannot provide code example because your question was vague, and you did not provide context for your code examples such as "_colorType". I cannot tell where that variable came from since you did not provide the variable implementation)
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
}
I'm trying to make a test which is faster in code executing.
Situation 1
int a=2;
if(a==1)
{
//code here
}
if(a==2)
{
//code here
}
if(a==3)
{
//code here
}
Situation 2
int a=2;
if(a==1)
{
//code here
}
else if(a==2)
{
//code here
}
else if(a==3)
{
//code here
}
In situation 1, 'int a' is always different value inside if statements
If you have a lot of if or if else statements I would recommend a switch statement like this:
int a = 2;
switch (a)
{
case 1:
break;
case 2:
break;
case 3:
break;
}
Reference: http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx
The second code block can be faster because the first one always tests every condition. The second one stops testing after it finds a match.
I am creating an Automation Testing framework, and I came up with this function:
public void DoClick(string selectType, string selector)
{
switch (selectType)
{
case "ClassName":
driver.FindElement(By.ClassName(selector)).Click();
break;
case "CssSelector":
driver.FindElement(By.CssSelector(selector)).Click();
break;
case "Id":
driver.FindElement(By.Id(selector)).Click();
break;
}
}
I want to replace the "By..." with something like By[selectType], in order to write a single line of code, instead of the whole switch case. How can I achieve that?
It's pretty simple. There's really no need to do this. Why wrap a one-liner inside a function?
public void DoClick(By locator)
{
driver.FindElement(locator).Click();
}
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;
}
}