menu

Sunday, January 19, 2014

Is Keyword in C#

Is keyword is used to check variable's type ...

using System;
using System.Collections;

namespace IsKeyword
{
    class A
    {
        public string str = "A";
    }
    class B
    {
        public string str = "B";
    }
    class Program
    {
        static void Main()
        {
            ArrayList al = new ArrayList();
            al.Add(new B());
            al.Add(new A());
            al.Add(new B());
            al.Add(new B());
            al.Add(new A());
            al.Add(new A());
            al.Add(new B());
            foreach (var item in al)
            {
                if (item is A)
                {
                    Console.WriteLine(((A)item).str);
                }
            }
            Console.ReadKey();
        }
    }
}


JavaScript to Allow only Numbers and Dot

<html>
<head>
    <title></title>
    <script type="text/javascript">
        function ChkKeyPressed(e, str) {
            var keyNum;
            // IE
            if (window.event) { keyNum = e.keyCode; }
                // Netscape/Firefox/Opera/Chrome
            else if (e.which) { keyNum = e.which; }
            // Chk whether dot is available
            var isDot = str.value.indexOf(".");
            // Chk for second dot
            if (isDot > -1 && keyNum == 46) { return false; }
                // Allow first dot
            else if (keyNum == 46) { return true; }
                // Allow numeric char 0 to 9
            else if (keyNum <= 57 && keyNum >= 48) { return true; }
                // Block all other characters
            else { return false; }
        }
    </script>
</head>
<body>
    Enter your number:
    <input type="text" onkeypress="return ChkKeyPressed(event, this)" />
</body>

</html>

Sunday, September 1, 2013

Constructor in C#

using System;
namespace Constructor
{
    public abstract class A
    {
        //Static cunstructor con't have access modifire and argument
        //so only one static constructor can be created
        //public static A(int a)
        static A()
        {
            Console.WriteLine("A:A()");
        }
       
    }
    public class C : A
    {
        public C(int a)
        {
            Console.WriteLine("A:C(int a)");
        }
        static C()
        {
            Console.WriteLine("A:C()");
        }
    }
    //A class with both static and non static constructor, can't be inharited
    //public class D : C
    //{

    //}
    public static class C1 // : C // static class can't derive from any class
    {
        public static void Fun()
        {
            Console.WriteLine("A:Fun()");
        }
    }
    public class B
    {
        //private and protected constructor is not accessable
        public B()
        {
            Console.WriteLine("B:B()");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //A o = new A();//Abstract class can't be initialize
            C a = new C(1);
            B b = new B();
           
            Console.ReadKey();
        }
    }
}


Wednesday, August 28, 2013

Inheritance in C#

using System;
namespace Inheritance
{
    interface I1
    {
        void Fun();
    }
    public abstract class A
    {
        // Implemented function can also be marked as virtual
        public virtual void Fun()
        {
            Console.WriteLine("I1::Fun()");
        }
        public abstract void Fun1();
        public virtual void Fun2()
        {
            Console.WriteLine("C1::Fun()");
        }
    }
    public class C1 : A
    {
        // Overriding abstract function of class A
        public override void Fun1()
        {
            Console.WriteLine("C1::Fun1()");
        }
        // Overriding virtual function of class A
        public override void Fun2()
        {
            Console.WriteLine("C1::Fun2()");
        }
    }
    public class C2 : C1
    {
        // Overriding virtual function of class A
        public override void Fun()
        {
            Console.WriteLine("C2::Fun()");
        }
        // Overriding abstract function of class A
        public override void Fun1()
        {
            Console.WriteLine("C2::Fun1()");
        }
        // Overriding virtual function of class A
        public override void Fun2()
        {
            Console.WriteLine("C2::Fun2()");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            Console.ReadKey();
        }
    }
}


Implement Two Interfaces With Same Method Name in C#

using System;
interface I1
{
    void Fun();
}
interface I2
{
    void Fun();
    void Fun1();
}
public class C : I1, I2
{
    public void Fun()  
    {
        Console.WriteLine("I1::Fun()");
    }
    /* This function will be implemented as private
     * because one more implementation with same name
     * is already from another base type(I1)*/
    void I2.Fun()
    {
        Console.WriteLine("I2::Fun()");
    }
    public void Fun1()
    {
        Console.WriteLine("I2::Fun1()");
    }
}
public class A
{
    public static void Main()
    {
        I1 o1 = new C();
        o1.Fun();            // Of I1

        I2 o2 = new C();
        o2.Fun();            // Of I2
        o2.Fun1();           // Of I2
       
        C o = new C();
        o.Fun();             // Of I1
        o.Fun1();            // Of I1

        Console.ReadKey();
    }

}