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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace switchCase { class myInfoTypes { public int typeValue { get; set; } public string typeDesc { get; set; } } class MyApp { static void Main(string[] args) { List < myInfoTypes > lstmyInfoTypes = new List < myInfoTypes > (); lstmyInfoTypes.Add(new myInfoTypes { typeValue = 0, typeDesc = "MyInf0-0" }); lstmyInfoTypes.Add(new myInfoTypes { typeValue = 1, typeDesc = "MyInf0-1" }); lstmyInfoTypes.Add(new myInfoTypes { typeValue = 2, typeDesc = "MyInf0-2" }); lstmyInfoTypes.Add(new myInfoTypes { typeValue = 3, typeDesc = "MyInf0-3" }); int value = lstmyInfoTypes[2].typeValue; switch (value) { case 0: Console.WriteLine(lstmyInfoTypes[2].typeDesc); break; case 1: Console.WriteLine(lstmyInfoTypes[2].typeDesc); break; case 2: Console.WriteLine(lstmyInfoTypes[2].typeDesc); break; case 3: Console.WriteLine(lstmyInfoTypes[2].typeDesc); break; } Console.Read(); } } } |
Explanation
Output of the above program is given below.
1 |
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
var myInfoTypes = { "MyInf0-0": 0, "MyInfo-1": 1, "MyInfo-2": 2, "MyInfo-3": 3 }; $scope.bindMyInfoTypes = function(typeValue) { switch (parseInt(typeValue)) { case myInfoTypes.MyInfo - 0: $scope.MyInfo = "MyInfo-0"; console.log("It is" + $scope.MyInfo); break; case myInfoTypes.MyInfo - 1: $scope.MyInfo = "MyInfo-1"; console.log("It is" + $scope.MyInfo); break; case myInfoTypes.MyInfo - 2: $scope.MyInfo = "MyInfo-2"; console.log("It is" + $scope.MyInfo); break; case myInfoTypes.MyInfo - 3: $scope.MyInfo = "MyInfo-3"; console.log("It is" + $scope.MyInfo); break; } } //Invoke above function as like shown in below code $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
1 |
$scope.BindMyInfoTypes(2) this function with typeValue ==2 |
The program given above will produce the output, as expected; i.e.,
1 |
It is MyInfo-2 |
It matches the case with the value 2.
It is working similarly as we use it in .NET Applications.