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>