Thursday, November 29, 2007

Enumeration

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


class 
Program
{
    
//Flag enums are designed to support bitwise operations on the enum values
    
[Flags]
    
public enum LogLevels : uint
    
{
        
//Increment by powers of 2.
        //If you don't assign explicit values VS increments by 1. 
        
None        = 0,
        NotifyTrace 
= 1,
        NotifyDebug 
= 2,
        NotifyInfo  
= 4,
        NotifyWarn  
= 8,
        NotifyError 
= 16,
        NotifyAll   
=   NotifyTrace | NotifyDebug | NotifyInfo | NotifyWarn | NotifyError,
        
//The rationale for avoiding zero in a flag enumeration for an actual flag 
        //is that you can't OR it in with other flags as expected.
        
NotifyNone  = ~(NotifyTrace | NotifyDebug | NotifyInfo | NotifyWarn | NotifyError),
    }

    LogLevels options 
= LogLevels.NotifyDebug | LogLevels.NotifyWarn | LogLevels.NotifyError;

    static void 
Main(string[] args)
    {
        Program p 
= new Program();
        
p.Test1();
        
p.Test2();
        
LogLevels v = (LogLevels) 2;
        
p.Test3(v);

        
// Interesting, Compiler Catch Invalid Argument
        
p.Test4(2);
        
// Surprise, Correct
        
p.Test4(0);

        
Console.ReadLine();
    
}

    
public void Test1()
    {
        Type 
value = typeof(LogLevels);
        foreach 
(string s in Enum.GetNames(value))
        {
            Console.WriteLine(
"{0,-11} = {1}", s, Enum.Format(value, Enum.Parse(value, s), "d"));
        
}
    }

    
public void Test2()
    {
        Console.WriteLine(IsLevelsSet(LogLevels.NotifyNone))
;
        
Console.WriteLine(IsLevelsSet(LogLevels.NotifyTrace));
        
Console.WriteLine(IsLevelsSet(LogLevels.NotifyDebug));
        
Console.WriteLine(IsLevelsSet(LogLevels.NotifyInfo));
        
Console.WriteLine(IsLevelsSet(LogLevels.NotifyWarn));
        
Console.WriteLine(IsLevelsSet(LogLevels.NotifyError));
        
Console.WriteLine(IsLevelsSet(LogLevels.NotifyAll));
    
}

   
    
public void Test3(LogLevels value)
    {
        
switch (value)
        {
            
case LogLevels.NotifyTrace:
                Console.WriteLine(
value.ToString());
                break;
            case 
LogLevels.NotifyDebug:
                Console.WriteLine(
value.ToString());
                break;
            case 
LogLevels.NotifyInfo:
                Console.WriteLine(
value.ToString());
                break;
            case 
LogLevels.NotifyWarn:
                Console.WriteLine(
value.ToString());
                break;
            case 
LogLevels.NotifyError:
                Console.WriteLine(
value.ToString());
                break;
            case 
LogLevels.NotifyAll:
                Console.WriteLine(
value.ToString());
                break;
            case 
LogLevels.NotifyNone:
                Console.WriteLine(
value.ToString());
                break;
            default
:
                
break;
        
}
    }

    
public void Test4(LogLevels value)
    {
        //in C# the literal constant 0 implicitly converts to any enum type             
        
if (value == LogLevels.None)
            Console.WriteLine(
value.ToString());
    
}

    
private bool IsLevelsSet(LogLevels values)
    {
        
return (options & values) == values;
    
}

}

Friday, November 23, 2007

Create Objects From Their Names

//you can use the Activator.Create instance to do so.

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

namespace 
Labs1
{
    
class Program
    {
        
static void Main(string[] args)
        {

            
string TypeName = "DerivedTest";
            string 
NmSpace  = "Labs2";

            
//gets the System.Type of the control name (must be fully qualified)
            
Type t = Type.GetType(NmSpace + "." + TypeName);

            
//Uses the 'Activator' to create a new instance of the specified type
            
Labs2.Test obj1 = (Labs2.Test)Activator.CreateInstance(t);

            
//Now, "DerivedTest" is an instance of that type
            
obj1.Method();


            
NmSpace  = "Labs3";

            
t = Type.GetType(NmSpace + "." + TypeName);

            
Labs3.ITest obj2 = (Labs3.ITest) Activator.CreateInstance(t);
            
obj2.Method();

            
Console.ReadLine();

        
}
    }

}

namespace Labs2
{
    
public abstract class Test
    {
        
public virtual void Method()
        {
            Console.WriteLine(
"BasedTest2");
        
}
    }

    
public class DerivedTest : Test
    {
        
public override void Method()
        {
            Console.WriteLine(
"DerivedTest2");
        
}
    }
}

namespace Labs3
{
    
interface ITest
    {
        
void Method();
    
}
    
    
public class Test : ITest
    {
        
public virtual void Method()
        {
            Console.WriteLine(
"BasedTest3");
        
}
    }
   
    
public class DerivedTest : Test
    {
        
public override void Method()
        {
            Console.WriteLine(
"DerivedTest3");
        
}
    }
}

Wednesday, November 21, 2007

What is the use of 'virtual new' keyword ?

using System;
using 
System.Text;

namespace 
Polymorphism
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Derived1 dv1 
= new Derived1();
            
//Do you know which Class Method is called here ?
            
dv1.Method(dv1);

            
Derived2 dv2 = new Derived2();
            
//Do you know which Class Method is called here ?
            
dv2.Method(dv2);

            
DerivedDerived3 dv3 = new DerivedDerived3();
            
//Do you know which Class Method is called here ?
            
dv3.Method(dv3);

            
DerivedDerived4 dv4 = new DerivedDerived4();
            
//Do you know which Class Method is called here ?
            
dv4.Method(dv4);

            
Console.ReadLine();
        
}
    }

    
class Based1
    {
           
public string Display()
           {
               
return "based";
           
}
     }
     
    
class Derived1:Based1
    {
        
//What is the use of new keyword ?
           
public new string Display() 
           {
               
return "derived";
           
}
    
        
public void Method(Based1 t)
        {
              Console.WriteLine(t.Display())
;
        
}
    }

    
class Based2
    {
        
//What is the use of virtual keyword ?
        
public virtual string Display()
           {
               
return "based";
           
}
     }

    
class Derived2 : Based2
    {
        
//What is the use of override keyword ?
        
public override string Display()
        {
            
return "derived";
        
}

        
public void Method(Based2 t)
        {
            Console.WriteLine(t.Display())
;
        
}

    }

    
class Based3
    {
        
public string Display()
        {
            
return "based";
        
}
    }
    
    
class Derived3 : Based3
    {
        
//What is the use of 'virtual new' keyword ?
        //Methods of a derived class can both be virtual and at the same time hide the derived method. 
        //In order to declare such a method, both keywords virtual and new have to be used in the method declaration
        
public virtual new string Display()
        {
            
return "derived";
        
}
    }

    
class DerivedDerived3 : Derived3
    {
        
public override string Display()
        {
            
return "derivedderived";
        
}

        
public void Method(Derived3 t)
        {
            Console.WriteLine(t.Display())
;
        
}
    }

    
class DerivedDerived4 : Derived3
    {
        
public new string Display()
        {
            
return "derivedderived";
        
}

        
public void Method(Derived3 t)
        {
            Console.WriteLine(t.Display())
;
        
}
    }

}

 
Indonesia To Blog -Top Site
SEO - search engine submission and optimisation