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.
- 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.
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.
- 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;
- }
- }
-
- $scope.bindMyInfoTypes(2);
Code explanation
- 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.
- $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 matches the case with the value 2.
It is working similarly as we use it in .NET Applications.