I have to use the same switch case repeatedly in different functions. In different functions, the switch case definition will be different. For example:      int groupID = somenumber;     private void FunctionA()     {         switch (groupID)         {             case 0:                 // Do Action A                 break;             case 1:                 // Do Action B                 break;             case 2:                 // Do Action C                 break;         }     }      private void FunctionB()     {         switch (groupID)         {             case 0:                 // Do Action Z                 break;             case 1:                 // Do Action X                 break;             case 2:                 // Do Action Y                 break;         }     }   Is that any method to use the same switch case once time but the definition can be different?
42580
What are the ways to eliminate the use of switch in code?
114921
I have some code like this:  If key.Equals("search", StringComparison.OrdinalIgnoreCase) Then     DoSomething() End If   I don't care about the case. Should I use OrdinalIgnoreCase, InvariantCultureIgnoreCase, or CurrentCultureIgnoreCase?
119341
I'm pretty new to programming and made a few programs so far. I recently made a Calculator program and I hope you guys could tell me what I could do to improve. int firstNumber; string sign; int secondNumber; int answer;  Console.WriteLine(&quot;Input Number:&quot;); firstNumber = Convert.ToInt32(Console.ReadLine()); Console.Clear();  Console.WriteLine(&quot;Input Sign:&quot;); sign = Console.ReadLine(); Console.Clear();  Console.WriteLine(&quot;Input Second Number:&quot;); secondNumber = Convert.ToInt32(Console.ReadLine()); Console.Clear();  switch (sign) {     case &quot;+&quot;:         answer = firstNumber + secondNumber;         Console.WriteLine(answer);         Console.ReadKey();     break;     case &quot;-&quot;:         answer = firstNumber - secondNumber;         Console.WriteLine(answer);         Console.ReadKey();     break;     case &quot;*&quot;:         answer = firstNumber * secondNumber;         Console.WriteLine(answer);         Console.ReadKey();     break;     case &quot;/&quot;:         answer = firstNumber / secondNumber;         Console.WriteLine(answer);         Console.ReadKey();     break;     default:         Console.WriteLine(&quot;Invalid Input&quot;);     break; }
78505
I'm asking with regards to c#, but I assume its the same in most other languages.  Does anyone have a good definition of expressions and statements and what the differences are?
56898
It is a bit odd we can't evaluate a  statement directly on SObjectType, so I am trying to determine conclusively which workaround is the most efficient.  Just to verify, I tried to compile this code:  void demo(SObjectType input) {     switch on input     {         when Account.sObjectType { }         when else { }     } }   And this code generates the error:     Schema.SObjectType is not a valid switch expression type   Bummer. There are workarounds however. The two most obvious that I have come up with:  void demoRecordInstantiation(SObjectType input) {     switch on input.newSObject()     {         when Account a { }         when else { }     } } void demoStringTyping(SObjectType input) {     switch on String.valueOf(input)     {         when 'Account' { }         when else { }     } }   Maybe there are others, so if there is an obvious approach I have missed which performs better or is more readable/maintainable, let me know.  My question is, what approach offers the best performance? If I am calling this method many times in a loop, is the SObjectType.newSObject method resource intensive? Or calling String.valueOf(SObjectType)? Should I consider caching these values?
27507
What is the benefit/downside to using a switch statement vs. an if/else in C#.  I can't imagine there being that big of a difference, other than maybe the look of your code. Is there any reason why the resulting IL or associated runtime performance would be radically different? Related:
11617
I've recently decided that I just have to finally learn C/C++, and there is one thing I do not really understand about pointers or more precisely, their definition.  How about these examples:   int* test; int *test; int * test; int* test,test2; int *test,test2; int * test,test2;   Now, to my understanding, the first three cases are all doing the same: Test is not an int, but a pointer to one.  The second set of examples is a bit more tricky. In case 4, both test and test2 will be pointers to an int, whereas in case 5, only test is a pointer, whereas test2 is a "real" int. What about case 6? Same as case 5?
60850
I often use this code pattern:  while(true) {      //do something      if(&lt;some condition&gt;) {         break;     }  }      Another programmer told me that this was bad practice and that I should replace it with the more standard:  while(!&lt;some condition&gt;) {      //do something  }      His reasoning was that you could "forget the break" too easily and have an endless loop. I told him that in the second example you could just as easily put in a condition which never returned true and so just as easily have an endless loop, so both are equally valid practices.  Further, I often prefer the former as it makes the code easier to read when you have multiple break points, i.e. multiple conditions which get out of the loop.  Can anyone enrichen this argument by adding evidence for one side or the other?
107627
Is it the same convention as those used in known programming languages such as Java and C? I'm trying to create a lot of variables and I want it to start with a number, but I'm not sure if it's okay.
40845
I wrote the two methods below to automatically select N distinct colors. It works by defining a piecewise linear function on the RGB cube. The benefit of this is you can also get a progressive scale if that's what you want, but when N gets large the colors can start to look similar. I can also imagine evenly subdividing the RGB cube into a lattice and then drawing points. Does anyone know any other methods? I'm ruling out defining a list and then just cycling through it. I should also say I don't generally care if they clash or don't look nice, they just have to be visually distinct.  public static List&lt;Color&gt; pick(int num) {     List&lt;Color&gt; colors = new ArrayList&lt;Color&gt;();     if (num &lt; 2)         return colors;     float dx = 1.0f / (float) (num - 1);     for (int i = 0; i &lt; num; i++) {         colors.add(get(i * dx));     }     return colors; }  public static Color get(float x) {     float r = 0.0f;     float g = 0.0f;     float b = 1.0f;     if (x &gt;= 0.0f &amp;&amp; x &lt; 0.2f) {         x = x / 0.2f;         r = 0.0f;         g = x;         b = 1.0f;     } else if (x &gt;= 0.2f &amp;&amp; x &lt; 0.4f) {         x = (x - 0.2f) / 0.2f;         r = 0.0f;         g = 1.0f;         b = 1.0f - x;     } else if (x &gt;= 0.4f &amp;&amp; x &lt; 0.6f) {         x = (x - 0.4f) / 0.2f;         r = x;         g = 1.0f;         b = 0.0f;     } else if (x &gt;= 0.6f &amp;&amp; x &lt; 0.8f) {         x = (x - 0.6f) / 0.2f;         r = 1.0f;         g = 1.0f - x;         b = 0.0f;     } else if (x &gt;= 0.8f &amp;&amp; x &lt;= 1.0f) {         x = (x - 0.8f) / 0.2f;         r = 1.0f;         g = 0.0f;         b = x;     }     return new Color(r, g, b); }
19310
I have some code that I only want to run once, even though the circumstances that trigger that code could happen multiple times.  For example, when the user clicks the mouse, I want to click the thing:  void Update() {     if(mousebuttonpressed) {         ClickTheThing(); // I only want this to happen on the first click,                          // then never again.     } }   However, with this code, every time I click the mouse, the thing gets clicked. How can I make it happen only once?
88238
I have problem in some JavaScript that I am writing where the Switch statement does not seem to be working as expected.  switch (msg.ResultType) {   case 0:     $('#txtConsole').val("Some Val 0");     break;   case 1:     $('#txtConsole').val("Some Val 1");     break;   case 2:     $('#txtConsole').text("Some Val 2");     break; }   The ResultType is an integer value 0-2 and I can see that in FireBug.  In all cases, the switch transfers control to the final break statement which means all the logic is completely skipped.  What am I missing?
13334
What is a closure? Do we have them in .NET?  If they do exist in .NET, could you please provide a code snippet (preferably in C#) explaining it?
71027
The following code fails to compile stating  "A local variable named 'st' cannot be declared in this scope because it would give a different meaning to 'st', which is already used in a 'child' scope to denote something else":           var l = new List&lt;string&gt;();         l.Find(st =&gt; st.EndsWith("12"));         string st = "why this fails?";   I understand why this won't work:          string preParent = "";         {             string preParent = "Should fail cause we change the meaning";         }   When we do the following we get "CS0103: The name 'postParent' does not exist in the current context":          {             string postParent=string.Empty;         }         postParent = "Should this work?";   What I don't get is why is the compiler smart enough to see that postParent is not within scope, but won't let me define a new variable that has the same name as a variable used within a child scope (which is obviously out of scope at this point).   Is the compiler simple enforcing scope by refusing to let me use the variable? If so this makes sense.  ===========  Edited:   I guess what I also find interesting is how you can have the same variable within two child scopes in a single method, so this is valid:          {             string thisWorks= string.Empty;         }         {             string thisWorks= "Should this work?";         }   I'm just a little curious that you can have two variables with the same name as long as they are at the same level (if you look at scope as a tree). This makes sense because you can have local variables in two methods of the same class with the same name.   I'm just surprised that the compiler is able to differentiate and allow this, while it wouldn't allow the postParent variable. And is this a technical limitation or was this a design decision? That's what I'm really trying to get at;-)
11
I've always been one to simply use:  List&lt;String&gt; names = new ArrayList&lt;&gt;();   I use the interface as the type name for portability, so that when I ask questions such as these I can rework my code.    When should  be used over  and vice-versa?
64609
I have taken three fields from case object called AccountName, IntegrationTo__c and SfType__c (checkbox).  I have created one case record with a specific combination of three fields.  For the second time I create a record with that combination, then  display error message only if SfType__c is true in first record otherwise allow  for record creation.  trigger AvoidDuplicate on Case(before insert)  {          set&lt;string&gt; newNameSet = new set&lt;string&gt;();     newNameSet = new set&lt;string&gt;();      set&lt;string&gt; newEmailSet = new set&lt;string&gt;();      set&lt;string&gt; dbNameSet = new set&lt;string&gt;();      set&lt;string&gt; dbEmailSet = new set&lt;string&gt;();      for(Case newLead : trigger.new){          newNameSet.add(newLead.Account.Name);          newEmailSet.add(newLead.IntegrationTo__c);      }      for(Case dbLead : [select id, Case.Account.Name, IntegrationTo__c from Case where IntegrationTo__c IN: newEmailSet OR Case.Account.Name IN: newNameSet]){          dbNameSet.add(dbLead.Account.Name);          dbEmailSet.add(dbLead.IntegrationTo__c);      }      for(Case newLead : trigger.new){          if(dbNameSet.contains(newLead.Account.Name) &amp;&amp; dbEmailSet.Contains(newLead.IntegrationTo__c))              newLead.addError('You are inserting Duplicate Case');      }  }   i wrote this this not checking the account field name and integration__c values only with combination of account and integration__c shows error
17714
I have a function with the same name, but with different signature in a base and derived classes. When I am trying to use the base class's function in another class that inherits from the derived, I receive an error. See the following code:  class A {     public:     void foo(string s){}; };  class B : public A {     public:     int foo(int i){}; };  class C : public B {     public:     void bar()     {         string s;         foo(s);     } };   I receive the following error from the gcc compiler:  In member function `void C::bar()': no matching function for call to `C::foo(std::string&amp;)' candidates are: int B::foo(int)   If I remove int foo(int i){}; from class B, or if I rename it from foo1, everything works fine.   What's the problem with this?