Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Tuesday, 24 January 2017

Change the protocol before calling any URL using HttpWebRequest


Now-a-days most of secure websites has transferred their security protocol to TLS 1.2 protocol. In C#, default protocol is taken as SSL3 or TLS. You must need to change this protocol before accessing secure website with TLS 1.2 protocol. Just write below code before you call targeted website.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

This code will change the calling request protocol to TLS 1.2. That’s it

Tuesday, 22 December 2015

Object count in binary image


You are given a binary image. Binary image is basically represented by a matrix of 0-1 values. The pixel whose value is 1 is said to be bright pixel and the pixel whose value is 0 is said to be dark pixel. The consecutive bright pixels (here consecutive means upper, lower, left side or right side) form an object.

For example, in the following binary image there are 6 objects 



You will be given an input binary image in matrix form each element of which will be 0 or 1. You have to detect number of objects in the image.


0      0      0      0      0      0      0      0      0 
0      0      1      1      0      0      1      1      1 
0      0      0      0      0      0      0      1      1 
0      0      0      0      0      0      0      0      0 
0      1      1      1      1      1      1      1      1 
1      1      1      1      1      1      1      1      1 
0      0      0      1      1      1      1      0      0 
1      1      1      0      0      0      0      0      0 

Matrix is 8×9 (i.e. number of rows are 8 and number of columns are 9). Count number of consecutive zeros from left to right (i.e. row wise) and then downwards (i.e. column wise), then count number of consecutive ones and so on.

Above matrix can be represented using this idea by 11, 0, 2, 1, 2, 0, 3, 1, 7, 0, 2, 1, 10, 0, 17, 1, 3, 0, 4, 1, 2, 0, 3, 1, 6, 0 (11 zeros then 2 ones then 2 zeros and so on).

So there are two inputs in this case

(i)  An integer array of 2 elements depicting number of rows and number of columns of matrix
     For e.g. {8, 9}.

(ii) An integer array defining the matrix
     For e.g. {11,0,2,1,2,0,3,1,7,0,2,1,10,0,17,1,3,0,4,1,2,0,3,1,6,0}.


Output Specification:
Output will be the number of objects in the image.


Solution:

            var input1 = new int[] { 51, 101 };
            var input2 = new int[]{21, 1, 170, 0, 2, 1, 97, 0, 4, 1, 4, 0, 2, 1, 92, 0, 3, 1, 98, 0,
3, 1, 3, 0, 3, 1, 92, 0, 3, 1, 3, 0, 3, 1, 78, 0, 8, 1, 11, 0, 4,
1, 77, 0, 9, 1, 13, 0, 1, 1, 78, 0, 9, 1, 7, 0, 4, 1, 82, 0, 8,
1, 7, 0, 5, 1, 81, 0, 8, 1, 7, 0, 2, 1, 1, 0, 2, 1, 80, 0, 8, 1,
8, 0, 5, 1, 81, 0, 7, 1, 7, 0, 5, 1, 85, 0, 1, 1, 1, 0, 2, 1, 99,
0, 3, 1, 91, 0, 9, 1, 91, 0, 10, 1, 93, 0, 5, 1, 1, 0, 2, 1, 93,
0, 8, 1, 93, 0, 8, 1, 92, 0, 9, 1, 95, 0, 7, 1, 94, 0, 6, 1, 94,
0, 7, 1, 91, 0, 10, 1, 92, 0, 8, 1, 94, 0, 1, 1, 2, 0, 3, 1, 94,
0, 1, 1, 2350, 0};

            string matrix = "";
            string prevRow = string.Empty;
            var row = 0;
            var column = 0;
            var objCount = 0;

            for (var k = 0; k < input2.Length; k = k + 2)
            {
                var BinaryChar = input2[k + 1];
                if (BinaryChar == 1)
                {
                    objCount += 1; // increase count by 1 when bright pixel come
                }
                bool currentloop = true;
                // Create matrix
                for (var l = 0; l < input2[k]; l++)
                {
                    column += 1;
                    if (row >= input1[0])
                    {
                        break;
                    }

                    // check for objects
                    if (BinaryChar == 1)
                    {
                        // Compare top
                        string topChar = string.Empty;                       
                        if (!string.IsNullOrWhiteSpace(prevRow))
                        {
                            topChar = prevRow.Substring(column - 1, 1);
                        }

                        if (currentloop && topChar == BinaryChar.ToString())
                        {
                            objCount -= 1;
                            currentloop = false;
                        }
                    }

                    matrix += BinaryChar;
                    if (column > 0 && (column % input1[1]) == 0)
                    {
                        row += 1;
                        prevRow = matrix;
                        matrix = "";
                        column = 0;
                    }
                }
            }

            int result = objCount;

Thanks to techgig

Wednesday, 19 August 2015

Call method in Circular reference in C#

We have two DLLs. One is Test1.dll and Second is Test2.dll. I have added reference of Test2.dll in Test1.dll. Now  I want to call method of Test1.dll from Test2.dll. For that I have to add reference of Test1.dll in test2.dll. but .Net doesn’t allow to add circular reference. Now it is confusion how to call method of Test1.dll. but we have way to achieve it. Follow the code snippet.
 
Create Interface in Test2.dll
   public interface ICallBack
    {
        void Log(string Query);
    }
 
Class in Test2.dll.  Pass Interface object as a parameter so You can access object of Test1.dll’s class
   public class Transactions
    {
        private ICallBack callback;
        public Transactions(ICallBack objcallback)
        {
            callback = objcallback;
        }
        public void CallLog()
        {
if (callback != null)
{
     callback.Log("Ajay Patel");
}
         }
    }
 
Call Interface method from class in Test2.dll
if (callback != null)
{
     callback.Log("Ajay Patel");
}
We have completed steps in Test2.dll. Now do some trick in Test1.dll. Inherit interface to class in which we want to call method. And pass instace of class as a parameter in Test2.dll’s class constructor.
 
public class TestClass : ICallBack
{
        public TestClass()
        {
Transactions objTran = new Transactions(this)
        }
        public void Log(string Query)
        {
            // Query = “Ajay Patel” This method will called from Test2.dll
        }
}

Wednesday, 8 July 2015

Generic class with optional parameter

public class Generic<T>
      where T : new()
    {
        private T objClass;
        public T ClassInstance
        {
            get
            {
                if (objClass == null)
                {
                    objClass = new T();
                }
 
                return objClass;
            }
        }
    }
 
    public class Generic<T, T1> : Generic<T>
        where T1 : new()
        where T : new()
    {
        private T1 objClass1;
        public T1 ClassInstance1
        {
            get
            {
                if (objClass1 == null)
                {
                    objClass1 = new T1();
                }
 
                return objClass1;
            }
        }
    }