Using lock in C#

Sometimes when we are using threading in a project and there are simultaneous threads running a piece of code but you want the threads to run the code sequentially,then we can make use of lock and create an object and place the critical section code in this lock.

private static readonly Object _lock = new Object(); lock (_lock) { // critical section }
Code language: JavaScript (javascript)

Understanding setState in React

You may be wondering why my call to setState does not mutate the state immediately, this is because setState calls are asynchronous and are usually bundled together to mutate state later.

But sometimes we need to use the state varaible after the set state this can be done by using the call back function supported in set state. In the code snippet below, i want to update the selectedFile state with file and have to print the file name. Usually the code would be like this

this.setState({selectedFile:file}); console.log(this.state.selectedFile.Name);
Code language: CSS (css)

The above code would definitely print either undefined or previous state of selected file. To avoid the above issue we can re write the above code as below and get desired result.

this.setState({selectedFile: file}, function () { console.log(this.state.selectedFile.Name);
Code language: JavaScript (javascript)

Visual Studio 2017, The breakpoint will not currently be hit. No symbols have been loaded for this document

I was working in a project where we use slow cheetah and have different configuration files for different environments and when trying to debug using a “local-dev” configuration i was having the above mentioned error.
This configuration was added recently, with other configuration the breakpoint was hitting and working normally.

With the help of stackoverflow I tried some changes and after the follwing changes were made I was able to hit the breakpoint and resolve the issue.

  • Right click on the project and go to properties and then to Debug tab. Make sure you have the above settings. “Define DEBUG constant” and “Define TRACE constant” are selected.
  • After that select Advanced and make sure you have the above Settings.
  • Save and then rebuild the solution,you will be able to resolve the issue.

For those who might be wondering what slow cheetah is ?? It is a visual studio extension that transform our config files. We were using this to use config files based on the environment like Dev,QA,Prod,Local-Dev. You can find more details below

https://marketplace.visualstudio.com/items?itemName=vscps.SlowCheetah-XMLTransforms

Windows Service Errors and solutions

Have you ever seen this error when trying to start a windows service

The Renkim File Service service is not started.

More help is available by typing NET HELPMSG 3521.

I had trouble resolving this issue. For me the issue was  the service was expecting a connection to database and in anothe case it was trying to connect to Rabbit Mq queue.

 

So please make sure the requirements for your service running conditions are satisfied before trying to start your service.

 

RabbitMQ Installation and Using Management Interface

  • To Install RabbitMQ you need to install Erlang first. Download and install the Erlang OTP latest version.
  • After that install the RabbitMQ .
  • Once RabbitMQ is installed browse to the location where it is installed in the program files like below.
  • Now enable the management interface plugin using the command
    “rabbitmq-plugins.bat enable rabbitmq_management”
  • Now restart the rabbitMQ service.
  • After that you can visit the url “http://localhost:15672” and login using credentials guest for both username and password.
  • After the login you can see all the details like below.

Adding more than one object to a list

I was adding result of a query which return a list of files but i was having error “The best overloaded method has some invalid arguments”.

I was adding elements using .Add() method and it was causing the error.  Add() method is used only to add single object, when we want to add multiple objects we should use AddRange() method, my error got resolved with this.

 

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.

 

Hosting multiple domains using Hostgator baby account

With hostagator baby plan you have the ability to host unlimited domains. There will be a primary domain name associated with the hosting account and to add new domain to host using the existing account follow the below steps.

  1. First buy the domain you require from any of the places like hostgator,godaddy,nameocheap etc.
  2. Once you bought the domain you need to add this domain to your  hosting account.
  3. Please login to the cpanel and under domains you will find add domain,click on that and you will land on a new page.
  4. In the new page fill out the domain name and choose the directory, by default it will be home/’domain name’.
  5. Also select the create ftp account and it will prompt for a password. Provide a password to use for this account.
  6. After that click on add domain. This completes the addition of domain to your account.
  7. Now under the domains section you will see the daomain but it will display “provisioning in progress”. We have to wait 24 to 36 hours till this step is done.
  8. Once done when we go to the domian name it will say configure the dns name servers. By default the name servers will point to hostgator name servers.
  9. We have the provide the dns name servers associated with our hosting account. Once this is done we will be able to use the domain name and build a websote or webapp and host it on the url.

Happy hosting !!!

What is webpack and why do we use it

Web pack is a static module bundler used in modern javascript applications. In react we use webpack alongside with babel to transpile the ES6 code to ES5 and provide the bundled js file in vanilla javascript which is understood by all browsers and works fine without any hastles.