Click to See Complete Forum and Search --> : Dealing with Base classes in c#
Wokawidget
Feb 2nd, 2006, 09:38 AM
OK, VB.NET:
Imports System.Collections
Public Class MyCollection
Inherits CollectionBase
Public Sub New()
MyBase.New
End Sub
End Class
in c# I have:
public class MyCollection: CollectionBase
{
public MyCollection()
{
//What on earth goes here?
}
}
What should I put in the constructor sub? This.???
Woka
Wokawidget
Feb 2nd, 2006, 09:42 AM
Would:
public class MyCollection: CollectionBase
{
public MyCollection()
{
base();
}
be correct?
Woka
conipto
Feb 2nd, 2006, 10:20 AM
C# Should follow the C++ constructor logic, that when you call the constructor, base class constructors also get called. For Example:
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
Wokawidget
Feb 2nd, 2006, 10:25 AM
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
conipto
Feb 2nd, 2006, 10:36 AM
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):
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
Wokawidget
Feb 2nd, 2006, 10:41 AM
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
conipto
Feb 2nd, 2006, 10:51 AM
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/Language/MultipleInheritanceCB.asp
here's a neet link on how to fake it, but screw that, I'm going back to VB/c++ :P
Bill
Wokawidget
Feb 2nd, 2006, 11:58 AM
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 :D
Woka
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.