Results 1 to 6 of 6

Thread: How to initialize a derived class with an instance of the base class in c#?

  1. #1
    Join Date
    Aug 2009
    Posts
    58

    How to initialize a derived class with an instance of the base class in c#?

    Hello to all,
    I am new to this forum. I am last year Computer Science student. In our last lecture we learn about how to initialize a derived class. But there is one question in mind. Can we initialize a derived class with an instance of the base class in c#? If yes, can anyone tell me how to initialize a derived class with an instance of the base class in c#? Please help me.
    Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to initialize a derived class with an instance of the base class in c#?

    I have written following code for your understanding. Just try to understand it. In the following code I have use MyListEg to implement all methods. In the following code I have use 3list to achieve result. I also have use one variable known as "lists" to store variable.

    Code:
    public class MyListEg<T> : List<T> {
        public MyListEg(List<T> list) {
            this = lists;  
            base = lists; 
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to initialize a derived class with an instance of the base class in c#?

    From the three object oriented principle Inheritance is one of the most useful feature. This feature allow you to reuse existing code. Through this you can save your lot of time by re-using code. I have written following code for your understanding. Just try to understand it.

    Code:
     using System;
    
    public class ParentClassEg
    {
        public ParentClassEg()
        {
            Console.WriteLine("you are in Parent Constructor.");
        }
    
        public void print()
        {
            Console.WriteLine("you are in  Parent Class.");
        }
    }
    
    public class ChildClassEg : ParentClassEg
    {
        public ChildClassEg()
        {
            Console.WriteLine("you are inChild Constructor.");
        }
    
        public static void Main()
        {
            ChildClassEg child = new ChildClassEg();
    
            childs.print();
        }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to initialize a derived class with an instance of the base class in c#?

    You have to use following code in your program to initialize a derived class with an instance of the base class in c#. In this code I have use extension method on the List object.

    public MyListEg(List<T> lists) : base(lists)

    If you know how to use extension methods then use it on another class. just use this code in first line.

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to initialize a derived class with an instance of the base class in c#?

    If you use .Net framework 3.5 then it would be difficult for you to define an extension method on a list. In this case you have to use following code in your program to fix this problem. In the following code I have use MyListExtensionClassEg class to define all methods.

    Code:
    public static class MyListExtensionClassEg
    {
        public static void MyListEg<T>(this Lists<Ts> lists)
        {
            // Write your code
        }
    }

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to initialize a derived class with an instance of the base class in c#?

    You have to write two code in your program to initialize a derived class with an instance of the base class in c#. I have written following two code for you. Just try to understand it.
    Code:
    public MyListEg(List<Ts> lists):base(lists)
    The above code will call the following constructor of the base class.

    Code:
    public List(IEnumerables<Ts> collections);
    after writing above code, write following code in your program.

    Code:
    public class MyListEg<Ts> : List<Ts>
    {
      public MyLists(List<Ts> lists)
      {
        AddRanges(lists);
      }
    }

Similar Threads

  1. Using sizeof () class with empty base class in GCC
    By Alfiee in forum Software Development
    Replies: 6
    Last Post: 27-06-2011, 10:31 AM
  2. Compiling error C2504: Base class undefined
    By vISHaTAN in forum Software Development
    Replies: 6
    Last Post: 26-06-2011, 07:35 PM
  3. Replies: 4
    Last Post: 08-01-2011, 07:52 AM
  4. What is Visual base class in C++?
    By Venugopala in forum Software Development
    Replies: 4
    Last Post: 28-12-2010, 08:11 AM
  5. An instance of class Business works for many forms
    By AMISH in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 08:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,668,706.08350 seconds with 17 queries