Check Digit

Check digit or some times known as Check Sum is a character used to verify the correctness of the information, most commonly used places are barcode,credit card numbers, bank account numbers.

General usage is when all the information is numbers but sometimes we have alpha numeric characters in the information and we need to generate the check digit.

First we will go through calculation of check digit with all numbers based on Luhn Algorithm. Consider “14358”.

  1. In step 1 we multiply every alternate number with “2” going from right to left.
    1   4   3   5   8
    2   1   2    1   2
    2   4   6    5   16
  2. After that add up all numbers if number is greater than 9 add the digits and take the value. In the above case for “16” we get 1+6=7. So the sum would be 2+4+6+5+7=24.
  3. Now divide this by 10. 24/10 we get quoitent of 2 and remainder 4. We need the remainder. Check digit would be 10-4=6.

C# program to calculate check digit with numbers.

private static int CheckDigit(string number)
        {
            List<int> ans = new List<int>();
            foreach (var x in number.Reverse().ToList())
            {
                if (int.TryParse(x.ToString(), out var num))
                {
                    ans.Add(num);
                }
               
            }
            var sum = 0;
            for (int i = 0; i < ans.Count; i++)
            {
                
                if (i % 2 == 0)
                {
                    ans[i] = ans[i] * 2;
                    if (ans[i] > 9)
                    {
                        ans[i] = ans[i] % 10 + 1;
                    }

                }

                sum += ans[i];
                
            }
            sum = 10 - (sum % 10);
            if (sum == 10) sum = 0;
            return sum;


        }

Now when when we have alphanumeric characters in the information all the steps would be same but we have to consider a mapping to the values.We can make use of the alpha numeric character ascii value or we can assign different values like A=10,B=11,C=12 and so on.

C# program for check digit with alphanumeric characters.

private static int CheckDigit(string number)
        {
            List<int> ans = new List<int>();
            foreach (var x in number.Reverse().ToList())
            {
                if (int.TryParse(x.ToString(), out var num))
                {
                    ans.Add(num);
                }
                else
                {
                    int temp = (int) x - 48;
                    ans.Add(temp);
                }
               
            }
            var sum = 0;
            for (int i = 0; i < ans.Count; i++)
            {
                
                if (i % 2 == 0)
                {
                    ans[i] = ans[i] * 2;
                    if (ans[i] > 9)
                    {
                        ans[i] = ans[i] % 10 + ans[i]/10;
                    }

                }

                sum += ans[i];
                
            }
            sum = 10 - (sum % 10);
            if (sum == 10) sum = 0;
            return sum;


        }

Here i am making use of values for alpha numeric value is “ASCII value -48” and generating the check digit.

 

Building your C# application

I am going to describe how to create a basic C# console application in which we generate a random number and then guess it.Before diving in I am just giving an introduction on C# and .NET and also which tools we will be using in building this application.

C# is an object oriented programming language created by Microsoft and it is one of the many languages used along with .NET framework. There is generally a confusion between C# and .NET, I will try to clear this. C# is basically a programming language and we use it along with .NET framework to build applications for Windows and we can also use C# to build applications for other platforms like Linux  by making use of Mono, which is a open source implementation of Microsoft .NET Framework.
.NET is a framework for building windows applications and web applications and this is not limited to one language. We can use C#, VB.Net, F#, C++, J# and many more like around sixty languages. It has two important components say Common Language Run-time and Class Library. CLR is a virtual machine component and manages execution of .NET programs. Common Language Runtime or CLR is the run-time execution environment of .Net Framework. Converting MS-IL into platform or OS specific code is done by the CLR. CLR is responsible for bringing application to life and its also the CLR’s job to tear down application when its finished executing or if it has an unrecoverable error. CLR actively tracks all the memory a program uses and it knows when the program is finished with memory so it will clean things up and allows program to have enough memory as it runs. 

We make use of an IDE(Integrated Development Environment) to build our application. We use Visual Studio (Community edition) as it is free.

First we should decide the algorithm on how we are writing our program. The algorithm is as follows:

1.Generate a random number .
2.Take input from user.
3.Check whether both are equal.
4.Make user know whether value he entered is greater or lesser than the secret number.
5.Continue steps 2 ,3,4 until guess is correct.
6.If user quits close or else continue from step 1 again.

Get Microsoft Visual Studio installed and Create a new project. In creating new project select Console Application and select location where you want

to save the project.

Once you click on OK you have a new screen with defaults loaded. We have a program.cs file created for us with an empty main method which is our entry point.


Now we can start writing our application. First we need to generate a random number for this we make use of Random class .


After generating this number we should prompt the user to guess a value.


Now we should check whether the guess and secret number are equal or not. We should do this until user enters the correct value.


We should allow user to play the game until he wants to quit.


now the application is done and we can start using it to play with your friends.

Nancy

Nancy is a light-weight ,low ceremony framework for building HTTP-based services on .NET. It provides a super-duper-happy-path to all interactions.

You might be wondering what the super-duper-path is. Here I will explain things clearly.

 

  • It just works
  • Easily Customizable
  • Low ceremony
  • Low friction

how to pass a javascript object to be written to a database

I have an Address object from my user interface like below

Address:{address1:”Franklin”,address2:”Warrensburg”,adress3:”Missouri}

so as i am sending an object i should be able to capture all the fields.

so i define a class as

public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
}

and now when we get an object ,we get values as follows

[“Address”] = new JObject
{
[“Address1”] = _customer.Address.Address1, ===>Franklin
[“Address2”]= _customer.Address.Address2, ===>Warrensburg
[“Address3”] = _customer.Address.Address3 ===> Missouri
}

Partial Classes

Partial classes concept was introduced in .NET 2.0 which allows us to split business logic in multiple files with same class name but with a “partial” keyword.

constant and read only difference in C#

constant needs to be initialized when it is declared.

read only variable can be assigned dynamically but need to be done before constructor exists as the the value is not changeable later.

Choosing between  read only or constant depends on our requirement. When we are confident that our constant value does not change we can use “const”. The issue with constant is when we define a constant “a” variable with value 20 in an assembly X and use this assembly in another assembly Y, any update of variable “a” is not reflected until we recompile it.

But when we use a read only value here it is a reference to memory location and when we have new “a” value and on building assembly “X” all the assemblies using this X assembly the value of “a” is updated without being building them again.