Results 1 to 20 of 20

Thread: constructors

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    constructors

    PHP Code:
    class Class1
    {
        static 
    void Main(string[] args)
        {
            
    MyDerivedClass derived = new MyDerivedClass(4);
        }
    }

    public class 
    MyBaseClass
    {
        public 
    MyBaseClass()
        {
            
    Console.WriteLine("MyBaseClass()");
        }

        public 
    MyBaseClass(int i)
        {
            
    Console.WriteLine("MyBaseClass(int i)");
        }
    }

    public class 
    MyDerivedClass MyBaseClass
    {
        public 
    MyDerivedClass()
        {
            
    Console.WriteLine("MyDerivedClass()");
        }

        public 
    MyDerivedClass(int i)
        {
            
    Console.WriteLine("MyDerivedClass(int i)");
        }

        public 
    MyDerivedClass(int iint j)
        {
            
    Console.WriteLine("MyDerivedClass(int i,int j)");
        }


    im reading a C# book who says that if i have a derived class from a inheritable class then when i use the derived class constructor it will try to use the base class constructor with the same parameters as the derived class..i tested..but it didnt work the way the book says!

    the way i did returned:

    MyBaseClass()
    MyDerivedClass(int i)


    when it should have returned:

    MyBaseClass(int i)
    MyDerivedClass(int i)


    hmm...what is wrong?

  2. #2

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    btw the book name is Beginning Visual C# its a very good book from Karli Watson..

  3. #3
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    When you derive a class from a base class, if you do not explicity call the base class constructor, the runtime will make a call up the chain to the default New() constructor, which takes zero arguments. You could have done this:

    Code:
    public MyDerivedClass(int i) : base(i)
    {
        Console.WriteLine("MyDerivedClass(int i)");
    }

  4. #4

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    also i am not seeing the advantage of using the base(i) here:

    PHP Code:
    class Class1
    {
        static 
    void Main(string[] args)
        {
            
    MyDerivedClass derived = new MyDerivedClass(4,8);
        }
    }

    public class 
    MyBaseClass
    {
        public 
    MyBaseClass()
        {
            
    Console.WriteLine("MyBaseClass()");
        }

        public 
    MyBaseClass(int i)
        {
            
    Console.WriteLine("MyBaseClass(int i)");
        }
    }

    public class 
    MyDerivedClass MyBaseClass
    {
        public 
    MyDerivedClass()
        {
            
    Console.WriteLine("MyDerivedClass()");
        }

        public 
    MyDerivedClass(int i)
        {
            
    Console.WriteLine("MyDerivedClass(int i)");
        }

        public 
    MyDerivedClass(int iint j) : base(i)
        {
            
    Console.WriteLine("MyDerivedClass(int i,int j) -> {0},{1}",i,j);
        }


    as the final result will be the same with or without the base..im confused...

  5. #5

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i know to use constructors but this is getting me really really confused damn i want to understand thiss!!! lol!

  6. #6

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ah sorry lethal didnt see ur post..hm...but what the : base(i) will do? i am not getting that at all...it forces the constructor(int i) to be used?

  7. #7
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    The base contructor will be called by default ,which is the one without the parameters. If you want to call a special contructor in the base class, thats when you would do base(i).
    Dont gain the world and lose your soul

  8. #8
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327
    Basically, you can call any of the base constructors from your subclass constructors.

    The : base(i) syntax allows you to specify which constructor to call, passing the appropriate parameter down to the base. This syntax is just as if you were creating a new instance of the base class.

    If you don't do this, then the base constructor, base(), will always be called.

    Does that help?
    -scott
    he he he

  9. #9
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327

    oops too slow...
    -scott
    he he he

  10. #10
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    DejaVu...

  11. #11

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    yeah much tks but i tryed base(i) and didnt work lol..

  12. #12

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i mean...what is the advantage of doin that here:

    PHP Code:
        public MyDerivedClass(int iint j)
        {
            
    Console.WriteLine("MyDerivedClass(int i,int j) -> {0},{1}",i,j);
        } 

  13. #13

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    PHP Code:
    class Class1
    {
        static 
    void Main(string[] args)
        {
            
    MyDerivedClass derived = new MyDerivedClass(3);
        }
    }

    public class 
    MyBaseClass
    {
        public 
    MyBaseClass()
        {
            
    Console.WriteLine("MyBaseClass()");
        }

        public 
    MyBaseClass(int i)
        {
            
    Console.WriteLine("MyBaseClass(int i)");
        }
    }

    public class 
    MyDerivedClass MyBaseClass
    {
        public 
    MyDerivedClass()
        {
            
    Console.WriteLine("MyDerivedClass() base(5) -> {0}");
        }

    //    public MyDerivedClass(int i)
    //    {
    //        Console.WriteLine("MyDerivedClass(int i)");
    //    }

        
    public MyDerivedClass(int iint j)
        {
            
    Console.WriteLine("MyDerivedClass(int i,int j) -> {0},{1}",i,j);
        }

        public 
    MyDerivedClass(int j) : base(i)
        {
            
    Console.WriteLine("MyDerivedClass(int j) : base(i) -> i={0} j={0}",i,j);
        }


    doesnt work bcoz he says base(i) doesnt exist . . .

  14. #14
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    B/c you named the variable 'j' in your constructor. All you are doing is passing the variable up the chain to the constructor you are explicitly invoking.

  15. #15

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    if i have up in the baseClass a constructor(int i) and a derivedClass constructor constructor(int i, string s) : base(j) (or base i?)what is the advantage of using base if the only thing im doin is writting more chars in code? lol

  16. #16
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Ok..You are calling the base class constructor that takes a parameter. Constructors are not inherited when you derive a class. So, your derived class constructor has 'int i' as one of its arguments. You need to pass the i paramater up to the base constructor, for initializtaion. Let's say for instance your base class didn't have a constructor that takes no arguments. If you didn't explicitly make this call from your derived class, the CLR would raise an exception.

  17. #17

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    . . . bah! i am not getting a ****! ill go sleep tomorrow i see this again

  18. #18
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327
    Like Lethal said, you had some bugs in your last constructor. Try the following and let us know if it's working (it should work). Also note, because of what you're doing, i will equal j...

    PHP Code:
    public MyDerivedClass(int j) : base(j)
    {
       
    Console.WriteLine("MyDerivedClass(int j) : base(i) -> i={0} j={1}",i,j);

    -scott
    he he he

  19. #19

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    but what is the advatage of making (int j) : base(j) ??? i am only writting extra code as the performance will be the same!

  20. #20
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327
    This is the only way that you can execute the base constructor.
    In this example, there's not really a good reason to call the base constructor because it doesn't do anything significant.

    But, imagine you are now inheriting the functionality from an array list. If you had a new constructor that took the size of the list and a new property of your class, name, then you could (should) call the base constructor to initialize the array list:

    PHP Code:
    public class NamedList ArrayList
    {
       private 
    String listName;

       public 
    NamedList(int SizeString Name) : base(Size)
      {
        
    listName Name;
      }

    This then becomes favorable to accessing the base class directly to intialize the size etc.
    -scott
    he he he

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width