Results 1 to 8 of 8

Thread: Dealing with Base classes in c#

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Dealing with Base classes in c#

    OK, VB.NET:

    VB Code:
    1. Imports System.Collections
    2.  
    3. Public Class MyCollection
    4. Inherits CollectionBase  
    5.  
    6.    Public Sub New()
    7.       MyBase.New
    8.    End Sub
    9.  
    10. End Class

    in c# I have:
    VB Code:
    1. public class MyCollection: CollectionBase
    2. {
    3.     public MyCollection()
    4.     {
    5.                  //What on earth goes here?
    6.     }
    7. }
    What should I put in the constructor sub? This.???

    Woka

  2. #2

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Dealing with Base classes in c#

    Would:
    VB Code:
    1. public class MyCollection: CollectionBase
    2.     {
    3.         public MyCollection()
    4.         {
    5.             base();
    6.         }
    be correct?

    Woka

  3. #3
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Dealing with Base classes in c#

    C# Should follow the C++ constructor logic, that when you call the constructor, base class constructors also get called. For Example:

    Code:
        class bill
        {
            public bill() { Console.WriteLine("Bill Constructor Called"); }
        }
        class Bob : bill
        {
            public Bob() { Console.WriteLine("Bob Constructor Called"); }
        }
    
    
    //In a random Button click function:
    
         Bob Bobo = new Bob();
    
    //causes this in the output window:
    
    Bill Constructor Called
    Bob Constructor Called
    
    :)
    So, the short answer is - you don't have to.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  4. #4

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Dealing with Base classes in c#

    yes, I see that. But if I am inheritting then in VB.NEt I must called Base.New from Sub New constructor.
    is this the same for c# if I use inheritance...

    I have zero exposure to any C languages...I am purely a VB developer, and never done anything else, apart from some smaller languages years ago like T Pascal and Modular 2 and basic etc.

    Woka

  5. #5
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Dealing with Base classes in c#

    Quote Originally Posted by Wokawidget
    yes, I see that. But if I am inheritting then in VB.NEt I must called Base.New from Sub New constructor.
    is this the same for c# if I use inheritance...

    In C++ or C#, when a class variable is instantiated, it calls the constructors for the class the variable is a type of, which, through inheritance (which you've got right by using ":" ), calls all inherited classes down the inheritance chain until you hit the ultimate base class. What's cool about C# and C++ is that a class can inherit from more than one base class.. and of course, all the base class constructors will be called. I believe it's only the default constructors though, and any parameterized constructors you would have to specify using the base keyword (if for example you have multiple constructors):

    Code:
    class woka
    {
       woka(){}
       woka(int var1, int var2){/*do something with var1&2*/}
    }
    
    class bill:woka
    {
       bill():base(1, 2) //Calls woka constructor overload that takes two parameters.
    }
    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  6. #6

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Dealing with Base classes in c#

    I don't undersstand that code
    Too much leet speak in c# for my liking. All on one line ()'s {}'s eh?

    Alos...c++ allows multiple Inheritance, c# does not.

    Woka

  7. #7
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Dealing with Base classes in c#

    Quote Originally Posted by Wokawidget
    Alos...c++ allows multiple Inheritance, c# does not.

    Woka
    !@#

    *deletes c# from hard drive*

    wow, I was under the impression it could, but you're right.

    http://www.c-sharpcorner.com/Languag...eritanceCB.asp

    here's a neet link on how to fake it, but screw that, I'm going back to VB/c++ :P

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  8. #8

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Dealing with Base classes in c#

    u can even fake it in VB6

    inheritance is used far too much by developers who don't know any better, and thus limitting it's use is a good thing. I see too much code where someone has used inheritance when there was no need and it just complicates things. Multiple inheritance and "novice" developers would be a nightmare

    Woka

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