Saturday, 23 June 2012

How to Initialize class to another class as Parameter to Constructor?

Yes, we can make class intialization to another class as a parameter.
means:  Class1 obj=new Class1{ new Class2()};  ----yes;

 class Book
    {            
        public Book(Publications obj)               // one
        {
            this.Publish = obj;
        }
        public Book(Book obj, Publications pobj)       //  two
        {
            this.Author = obj.Author;
            this.Publish = pobj;
        }
        public string Author { get; set; }
        public Publications Publish { get; set; }
    }
    class Publications
    {
        public string Publish { get; set; }
    }
 class InitailizataionofTwoClasses
    {
        static void Main()
        {                                      
          // 1st constructor 
            Book bo = new Book(new Publications { Publish = "Dream Tech" });         // one
            Console.Write(bo.Publish.Publish);
        
             // 2nd constructor
            Book bobj = new Book(new Publications { Publish = "BPB Publications" });  // two
            Console.Write(bobj.Publish.Publish);
            bobj.Author = "Propercode";
            Console.Write(bobj.Author);  // No author Here
    
        }
    }

No comments:

Post a Comment