Wednesday, 15 February 2017

Constructor in C#


A constructor is a special method of a class or structure in object-oriented programming that initializes an object of that type. A constructor is an instance method that usually has the same name as the class, and can be used to set the values of the members of an object, either to default or to user-defined values.

Some key point of constructor
·         Constructor is member of a class and structure.
·         Constructor is a special type of function which will be fire at time of object is creating or class is load.
·         Constructor name is always same name as class name.
·         Constructor not contains any return type, because it not returns any value.
·         Constructor can contain a parameter or not.

Syntax of constructor
<Access modifier><class name (type><parameter>)>
{
Code initialization
}

Instance constructor:-While define a constructor without using static key word is known as instance constructor.
Key point
·        Only instance constructor can hold parameter
·        It initialize at the time of object is created.
·        In a single class multiple instance constructors can be created but condition is each constructor should be unique to other by using different signature and number of parameter should vary.

Static constructor
When a constructor is created as static, it will be invoked only once for all of instances of the class and it are invoked during the creation of the first instance of the class or the first reference to a static member in the class. A static constructor is used to initialize static fields of the class and to write the code that needs to be executed only once.

Key Point constructor
Ø A static constructor does not take access modifiers or have parameters.
Ø A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
Ø A static constructor cannot be called directly.


A default constructor is a constructor which can be called with no arguments /with any parameter. 
 Key point  
Ø Definition of the constructor outside the class body.
Ø Inhibiting the automatic generation of a default constructor by the compiler.
Ø Explicitly forcing the automatic generation of a default constructor by the compiler.

using System;
namespace
 DefaultConstractor
 {
  
  class  demo
    {
       
 int a, b;  // class variable declartion
        public addition()   //without  any parameter  or argument
        {
            a = 100;
            b = 175;
        }

        public static void Main()
        {
            addition obj = new addition(); 
            Console.WriteLine(obj.a);
            Console.WriteLine(obj.b);
            Console.Read();
        }
      }
    }


Parameterized Constructor:- 
A constructor with at least one parameter is called parameterized constructor. The advantage of a parameterized constructor is that you can initialize each instance of the class to different values.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace parameterizedConstuctor
{
    class Course
    {
        ulong cid;
        string cname;
        double cfees;
        internal Course(ulong CourseID, string courseName, double courseFees)
        {
            cid = CourseID;
            cname = courseName;
            cfees = courseFees;
                    }
        internal void DisplaycourseDetails()
        {
            Console.WriteLine("Course Code : " + cid);
            Console.WriteLine("Couse Name : " + cname);
            //Console.WriteLine("Couse Name : " + string.Format("{0:0.00}"+ cfees));
            // string s = string.Format("Course Fees is {0:0.00}", +cfees);
            // Console.WriteLine(s);
            Console.WriteLine( string.Format("Course Fees is {0:0.00}", +cfees));
        }
        static void Main(string[] args)
        {
            Course obj1 = new Course(101, "C# .net", 1100.00);
            obj1.DisplaycourseDetails();
            Course obj2 = new Course(102, "Angular", 7000.00);
            obj2.DisplaycourseDetails();
            Course obj3 = new Course(103, "MVC", 1100.00);
            obj3.DisplaycourseDetails();
            Console.ReadLine();
        }
    }
}


Tuesday, 7 February 2017

Jump statement using C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jumpstatement
{
    class Program
    {
        static void Main(string[] args)
        {
            start:
            //This is a label
            Console.WriteLine("Enter The First Number");
            // Here User Eneter firt number
            int num1 = Convert.ToInt32(Console.ReadLine());
            //Here num1 store value enter by user first time
            Console.WriteLine("Enter the Second Number");
           // Here user Eneter Second number
            int num2 = Convert.ToInt32(Console.ReadLine());
            //it store under num2
            choice:
            Console.WriteLine("Enter A=Addition S=Subtraction M= multiplication D=Division");
            //here We providig an option.
            string choice = Console.ReadLine().ToUpper();
            //Returns a copy of this string converted to uppercase.
            int result = 0;
            // can not convert null to int becoz it is a non nullable value type;
            switch (choice)
                //Swich case statement:-Switch case is faster because ,it not check all the condition
                //it check only those condition which is meet within  the body(in the block)
                //or which satsify the condition
                             
            {
                case "A":
                    result = num1 + num2;
                    break;
                case "S":
                    result = num1 - num2;
                    break;
                case "M":
                    result = num1 * num2;
                    break;
                case "D":
                    break;
                default:
                    Console.WriteLine("Please Enter correct choice");
                    //If any of the expression passed to switch case does not match
                    //with case statement the control will go to default:statement.
                    break;
            }
            Console.WriteLine("Result is{0}"  + result);
            wantToContiue:
            //This is Label
            Console.WriteLine("Do you want  to Continue ? press Y to yes and N  to No.");
            string wantToContiue = Console.ReadLine().ToUpper();
            switch (wantToContiue)
            {
                case "Y":
                    goto start;
                //A common use of goto is to transfer control to a specific switch-case label or the default label in a switch statement.
                //The goto statement is also useful to get out of deeply nested loops.
                case "N":
                   break;
                default:
                    Console.WriteLine("Please enter correct choice..");
                    goto wantToContiue;
                    //Goto logic
            }
            Console.ReadLine();

         
                    }
       
         
        }
    }


Friday, 27 January 2017

How to send SMS using C# and ASP.NET

We can send SMS to any phone no and anywhere in the world using Twilio.
what is Twilio?

Twilio allows to make and receive phone calls and send and receive text messages using its APIs. Programmers can implement Twilio APIs in their code and create all sort of applications involving phone calls and SMS. It’s services are accessed over HTTP and are billed based on usage. It can be used easily in our application for creating features like SMS authentication, OTP, SMS marketing and more.

STEP 1:- Create your account in Twilio to get API credentials.
STEP 2: – Reference the Twilio.Api.dll file in your website.
For adding the reference .We need to install nuget Package in Visual Studio.
After install nuget package.
click on Tool-->Nuget Package Manager-->package Manager Console.->Type ‘Install-Package Twilio’ and press enter key..
-------------------Default.aspx---------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function msg() {
            alert("Thank you for Showing Your interest");
        }

    </script>
    <style type="text/css">
        #text {
            width: 300px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td>
                    <label>First Name</label></td>
                <td>
                    <input type="text" id="txtfName" required="required" class="text" runat="server" /></td>
            </tr>
            <tr>
                <td>
                    <label>Last Name</label></td>
                <td>
                    <input type="text" id="txlName" class="text" required="required" runat="server" /></td>
            </tr>
            <tr>
                <td>
                    <label>Email ID</label></td>
                <td>
                    <input type="email" id="txt" required="required" class="text" runat="server" /></td>
            </tr>
            <tr>
                <td>
                    <label>Phone</label></td>
                <td>
                    <input type="tel" id="txtphone" required="required" class="text" runat="server" /></td>
            </tr>
            <tr>
                <td>
                    <label>Your Requirement </label>
                </td>
                <td>
                    <textarea rows="2" id="txtReq" class="text" runat="server">

</textarea></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="btnsend" runat="server" Text="Send" OnClick="btnsend_Click" />
                </td>
            </tr>

        </table>

    </form>
</body>
</html>
---------------------------------------------------Default.aspx.cs----------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

using Twilio;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnsend_Click(object sender, EventArgs e)
    {
        string number = txtphone.Value.Trim();
        string msg = txtReq.Value.Trim().ToString();
        string AccountSid = "******";
        string AuthToken = "***********";
        var twilio = new TwilioRestClient(AccountSid, AuthToken);
        var message = twilio.SendMessage("From number", number, msg, ""); 
       
        ClientScript.RegisterStartupScript(this.GetType(), "Alert", "msg()", true);

    }
}

Note:- 1)Add using Twilio in code behind .
2)For Account Sid,AuthToken and From number we need to register your self on https://www.twilio.com/.


Saturday, 14 January 2017

Abstract in c#

ABSTRACT :-The purpose of abstract class is to provide a common definition of based class that multiple derived classes can share, and can be used only as a based class and never want to create the object of this class.Any class can be convert in abstract class by adding abstract modifier to to.

Some key point of  an abstract class and method are:-

  1. An abstract class can not be instantiate.
  2.  An Abstract class can have access modifiers like public, protected, internal with class members. But abstract members cannot have private access modifier.
  3. An abstract class must have an abstract method.
  4. An abstract method is implicitly a virtual method.
  5. The class containing at least one abstract method must be declared abstract.
The purpose of an abstract class is to provide basic functionalities that multiple derived classes can share and override.
  
Example:-
       class Program
    {
        public abstract class university
        {
            public abstract void MCA();
            public abstract void MBA();
            public void Durination()
            {
                Console.WriteLine("Post Graduate fees Structure");
            }

        }
        public class WBTU:university 
        {
            public override void MCA()
            {
                Console.WriteLine("Fees is 250000");
            }
            public override void MBA()
            {
                Console.WriteLine("Fees is 5000000");
            }

        }
        public class BHU : university
        {
            public override void MCA()
            {
                Console.WriteLine("Fees is 350000");
            }
            public override void MBA()
            {
                Console.WriteLine("Fees is 450000");
            }
        }
            static void Main(string[] args)
        {
            university objUni = new WBTU();
            objUni.Durination();
            objUni.MBA();
            objUni.MCA();
            objUni = new BHU();
            objUni.MBA();
            objUni.MCA();
            Console.ReadLine();
            

        }
    }

Note:-From above example we have make one abstract class university and two abstract methods MCA and MBA. WBTU and BHU both are override university course fee.
University course common for both WBTU and BHU so university method MCA and MBA is abstract.
WBTU and BHU inherited abstract method so university method must be override here.

Secure and Unsecure configuration in Dynamics CRM 365.

Overview : In Dynamics CRM, when we create a plugin, we sometimes need to pass data to that plugin, instead of writing this data inside the ...