Wednesday, July 22, 2015

Types of ERROR!

1.   Syntactical Error: Wrong Syntax, like Missing double quotes, wrong spelling of Keyword, missing “;” (terminator) etc… this error can be identified by the programmer.

2.   Compilation Error: Happens in Compile time like, trying to create object for abstract class or interface class, assigning wrong data in to the variable, this error can be identified by the programmer.


3.   Run-Time Error: this error occurs when User trying to connect with wrong user id or password OR trying to access a file which have no Open permission. And this error can’t be handle by programmer, because this happens by User End.

Enumerations - Facts

    Used to allow the user to assign any of the value from a specific list of value, into a data member/variable.
  • ·   It’s a collection of Constant Values.
  • ·         By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. For example, in the following enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth.
  • ·         enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
  • ·         Variables cannot be assigned to enum elements.
  • ·         Float CANNOT be used as an underlying datatype for an enum
  • ·         Enum is Value Type.

using System;
namespace Enum
{
   public  enum AgeGroupEnum
    {
        Child, Tennager, Adult, Senior
    }
    class Person
    {
        public string Name { get; set; }
       public  AgeGroupEnum AgeGroup { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person();
            p1.Name = "Soumik";
            p1.AgeGroup = AgeGroupEnum.Adult;

            Person p2 = new Person();
            p2.Name = "John";
            p2.AgeGroup = AgeGroupEnum.Senior;

            Console.WriteLine(p1.Name);
            Console.WriteLine(p2.Name);
            Console.WriteLine(p2.AgeGroup);
            Console.ReadKey();
        }
    }
}




Dictionary- with a simple program

 It’s used to create a collection with pair of “Key” & “Value

Ex:
  • Key             Values
  • Maths         70
  • Phy             80
  • Chm            65


Path: System.Collections.Generic.Dictionary;


Program:
using System;
using System.Collections.Generic;

namespace Dictionary
{
    class Program
    {

        static void Main(string[] args)
        {
            Dictionary<string, int> Marks = new Dictionary<string, int>();
          
            Marks.Add("Maths",70);
            Marks.Add("Phy",79);
            Marks.Add("Chem",90);

            int m = Marks["Chem"];
            Console.WriteLine(m);   //OUTPUT: 90

            Console.ReadKey();
        }
    }
}

Play with String

Eg:    string s=”jasd%^$#32143”;
Program:
using System;
namespace String
{
    class PlayWithString
    {
        static void Main(string[] args)
        {
            string str = "atsd %$&2143";// Print str's Value
            Console.WriteLine(str);
            // Showing 4th Position of String--
            string str2 = "India";
            int Pos = 4;
            char ch = str2[Pos];
            Console.WriteLine(ch);

            //string substr = str2.substring(pos);
            ////console.writeline(substr);
            //char ch2 = 'i';
            //char ch3 = str2.IndexOf(ch2, ch2+1);
            //Console.WriteLine(ch3);

            //Substring--


            // Trim- Remove White Spaces from String  L-R  --
            string lrgstr = "   Soumik Mukherjee      ";
            string smlstr = lrgstr.Trim();
            Console.WriteLine(smlstr);

            //String fomat-
            string  Sentence = "{0} Is Metro City of  {1}";
            string City = "Kolkata";
            string Country = "India";
            string ResultString = string.Format(Sentence, City, Country);
            Console.WriteLine(ResultString);

            Console.ReadKey();
        }
    }

}

Story of 'static'

Now I will tell you the story of 'static' Keyword:


static Keyword can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes

Static Data member:
  • -      Static data members accessible without creating any Object for the Class.
  • -      Create by ‘static’ keyword.
  • -      Static data member’s memory will be allocated when the class is used for the  1st tme in the main().
  • -      Static data members memory will not be allocated as part of the object.
  • -      Accessible in Static() & Non-Static ().

Static not Fix the Value, its Fix the Memory for 1st Time
Static data members are used to store the ‘common data’ that belong to all the object.
Syntax:
            AccessModifier static DataType VariableName;

Static Method():
  • -      Static Method() Accessible without creating any Object for the Class
  • -      Static Method() are accessible with Class Name.
  • -      Static Method() can access static data members Only; Can’t access non-      Static Method() or Data Members.
  • -      Static Method() are used to manipulations on static data members only.
  • -      Static Method() are declared as ‘static ’ keyword.


Syntax::
AccessModifier static ReturnType MethodName (arg1, arg2…)
{
            --------------
}

Static Constructor: 2 Types – Static & Non-Static constructor.
Used to initialize only static data members.
Static constructor is automatically call when the class name is used in main() for the 1st time.
Static constructor can initialise only static data members; can’t initials non-static data members.
Maximum can only one static constructor can be written in a class.
No Argument; No Return Value.
Syntax::
Static ClassName()
{
Static data members here………….
}

Static Class:
Static Class contain Only static-member; they can’t contain any non-static data member.
I can create Object for Static Class.
Static Classes avoided the confusion for the other programmer that there are not-static data member present on it.

Syntax::
Static class    ClassName
{
            ONLY staic data member;
}

Syntax to call Static Data Member:
ClassName.DataMember;
Syntax: to call static method:
ClassName.MethodName();
Syntax: to Call non-static data member:
ObjectName.DataMemberName;
Syntax: to call Non-static Constructor:
New ClassName();
Syntax: to Call non-static method:
Objectname.MethodName();

Program: -
using System;
namespace StaticDataMember  // Static not Fix the Vallue, its Fix the Memory for 1st Time
{
    //
    //If the static keyword is applied to a class, all the members of the class 'must be static' --
    static class Class1
    {
        // Static Data Member   -
       public  static int Var1=999;
       //public char ch = 's'; ----Instance member can't exist in Static Class

        //Static Method -
       public  static int Increment()
       {
           return Var1+1;
       }

        // Static Constructor   -
        static Class1()
        {
           // Var1 = Var1 + 1;
            Console.WriteLine("Woooooooooh. I am STATIC in Constructor,                                              I Called First");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {  
           // Console.WriteLine(Class1.Var1);
           Class1.Increment();
            int temp = Class1.Increment();
            Console.WriteLine(temp);

            Console.ReadKey();
        }
    }
}


Compile Time Polymorphism/Early Binding/Static Binding/Overloading

Same Method Name But, Difference Signature/Parameter:


namespace MethodOverloading
{
    public class Class1
    {
        public void Method(int x)
        {
            Console.WriteLine("Integer : " + x);
        }
        public void Method(char ch)
        {
            Console.WriteLine("character: " + ch);
        }
        public void Method(string str)
        {
            Console.WriteLine("String : " + str);
        }
        class Program
        {
            static void Main(string[] args)
            {
                Class1 obj1 = new Class1();
                obj1.Method(001);
                obj1.Method('S');
                obj1.Method("Soumik");

                Console.ReadKey();
            }
        }

    }

Value Type & Reference type

Value Type
Reference type
Struct, Int, double, long, float, bool, enum
Class, object, string
Memory is allocated at Compile Time
Memory is allocated at Run Time
Memory allocation stored in Stack. i.e. Contiguous
Memory Allocation Store in Heap, i.e. Random
Store the Value
Store Memory Address