Let us see how to use switch-case statements in JavaScript/jQuery with AngularJS implementation and in .NET Applications.

Note

Here, one important point needs to be remembered about switch-case statements in JavaScript/jQuery, which is that this is also working in a similar way as we use it in C# and .NET applications.

First of all, we will see how it works in .NET applications.

Let us see in detail with the code snippets given below.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace switchCase {
  7. class myInfoTypes {
  8. public int typeValue {
  9. get;
  10. set;
  11. }
  12. public string typeDesc {
  13. get;
  14. set;
  15. }
  16. }
  17. class MyApp {
  18. static void Main(string[] args) {
  19. List < myInfoTypes > lstmyInfoTypes = new List < myInfoTypes > ();
  20. lstmyInfoTypes.Add(new myInfoTypes {
  21. typeValue = 0, typeDesc = "MyInf0-0"
  22. });
  23. lstmyInfoTypes.Add(new myInfoTypes {
  24. typeValue = 1, typeDesc = "MyInf0-1"
  25. });
  26. lstmyInfoTypes.Add(new myInfoTypes {
  27. typeValue = 2, typeDesc = "MyInf0-2"
  28. });
  29. lstmyInfoTypes.Add(new myInfoTypes {
  30. typeValue = 3, typeDesc = "MyInf0-3"
  31. });
  32. int value = lstmyInfoTypes[2].typeValue;
  33. switch (value) {
  34. case 0:
  35. Console.WriteLine(lstmyInfoTypes[2].typeDesc);
  36. break;
  37. case 1:
  38. Console.WriteLine(lstmyInfoTypes[2].typeDesc);
  39. break;
  40. case 2:
  41. Console.WriteLine(lstmyInfoTypes[2].typeDesc);
  42. break;
  43. case 3:
  44. Console.WriteLine(lstmyInfoTypes[2].typeDesc);
  45. break;
  46. }
  47. Console.Read();
  48. }
  49. }
  50. }

Explanation

Output of the above program is given below.

MyInf0-2

Now, let’s see how to use it in JavaScript/jQuery with AngularJS implementation.

Let us see in detail with the code snippets given below.

  1. var myInfoTypes = {
  2. "MyInf0-0": 0,
  3. "MyInfo-1": 1,
  4. "MyInfo-2": 2,
  5. "MyInfo-3": 3
  6. };
  7. $scope.bindMyInfoTypes = function(typeValue) {
  8. switch (parseInt(typeValue)) {
  9. case myInfoTypes.MyInfo - 0:
  10. $scope.MyInfo = "MyInfo-0";
  11. console.log("It is" + $scope.MyInfo);
  12. break;
  13. case myInfoTypes.MyInfo - 1:
  14. $scope.MyInfo = "MyInfo-1";
  15. console.log("It is" + $scope.MyInfo);
  16. break;
  17. case myInfoTypes.MyInfo - 2:
  18. $scope.MyInfo = "MyInfo-2";
  19. console.log("It is" + $scope.MyInfo);
  20. break;
  21. case myInfoTypes.MyInfo - 3:
  22. $scope.MyInfo = "MyInfo-3";
  23. console.log("It is" + $scope.MyInfo);
  24. break;
  25. }
  26. }
  27. //Invoke above function as like shown in below code
  28. $scope.bindMyInfoTypes(2);

Code explanation

  1. var myInfoTypes = { "MyInf0-0": 0, "MyInfo-1": 1, "MyInfo-2": 2, "MyInfo-3": 3 };

The line of code given above indicates that

We stored the data inside the variable with the name myInfoTypes with typeValue and its associated information format.

  • Now, in order to bind type value, we need to invoke bindMyInfoTypes() function with typeValue as a parameter.

Let us say we will invoke it with the value 2, as shown below.

  1. $scope.bindMyInfoTypes(2);

Now, observe once we invoked

$scope.BindMyInfoTypes(2) this function with typeValue ==2

The program given above will produce the output, as expected; i.e.,

  • It is MyInfo-2

It matches the case with the value 2.

It is working similarly as we use it in .NET Applications.