Inheritance and constructors
-
Hi all, I'm having a hard time getting my head around
this()
andbase()
, hoping someone can help. Let's say I haveclassB
which inheritsclassA
both have a constructor and an overloaded constructor.class classA
{
public classA()
{
//some code
}
public classA(int i)
{
//some code
}
}class classB
{
public classB()
{
//some code
}
public classB(int i)
{
//some code
}
}How do I get the overloaded constructor of
classB
to first call the default constructor ofclassB
and then the similar overloaded constructor of the base class,classA
? I seem to be able to have it call either the default constructor ofclassB
OR the overloaded constructor ofclassA
by declaring the overloaded constructor ofclassB
aspublic classB(int i) : this()
orpublic classB(int i) : base(i)
but how do I get it to call both? -
Hi all, I'm having a hard time getting my head around
this()
andbase()
, hoping someone can help. Let's say I haveclassB
which inheritsclassA
both have a constructor and an overloaded constructor.class classA
{
public classA()
{
//some code
}
public classA(int i)
{
//some code
}
}class classB
{
public classB()
{
//some code
}
public classB(int i)
{
//some code
}
}How do I get the overloaded constructor of
classB
to first call the default constructor ofclassB
and then the similar overloaded constructor of the base class,classA
? I seem to be able to have it call either the default constructor ofclassB
OR the overloaded constructor ofclassA
by declaring the overloaded constructor ofclassB
aspublic classB(int i) : this()
orpublic classB(int i) : base(i)
but how do I get it to call both?First of all the proper way to inherit is : public ClassB:ClassA { } Secondly, when you are instianting ClassB it calls the parent constructor first and then instiante the derived class. For instance ClassB cb = new ClassB()---> it calls the Parent no value(default constructor) first and then public classB() { } Thirdly, ClassB cb = new Classb(2)-->it calls the Parent value constructor in ur case is public classA(int i) { } and then the dervied class constructor public classB(int i) { //some code } hope it may help
-
Hi all, I'm having a hard time getting my head around
this()
andbase()
, hoping someone can help. Let's say I haveclassB
which inheritsclassA
both have a constructor and an overloaded constructor.class classA
{
public classA()
{
//some code
}
public classA(int i)
{
//some code
}
}class classB
{
public classB()
{
//some code
}
public classB(int i)
{
//some code
}
}How do I get the overloaded constructor of
classB
to first call the default constructor ofclassB
and then the similar overloaded constructor of the base class,classA
? I seem to be able to have it call either the default constructor ofclassB
OR the overloaded constructor ofclassA
by declaring the overloaded constructor ofclassB
aspublic classB(int i) : this()
orpublic classB(int i) : base(i)
but how do I get it to call both?Try the following:
class classA
{
public classA() : this(0)
{
//some code
}
public classA(int i)
{
//some code
}
}
class classB : classA
{
public classB() : base()
{
//some code
}
public classB(int i) : base(i)
{
//some code
}
}With this, calling the classB constructors will result in the appropriate classA constructors being called. If however, you want to explicitly perform some code in response to the constructors, I would create an OnInit method, and call it from
public classA(int i)
. This means that all of the paths through to it will be handled.Deja View - the feeling that you've seen this post before.
-
Hi all, I'm having a hard time getting my head around
this()
andbase()
, hoping someone can help. Let's say I haveclassB
which inheritsclassA
both have a constructor and an overloaded constructor.class classA
{
public classA()
{
//some code
}
public classA(int i)
{
//some code
}
}class classB
{
public classB()
{
//some code
}
public classB(int i)
{
//some code
}
}How do I get the overloaded constructor of
classB
to first call the default constructor ofclassB
and then the similar overloaded constructor of the base class,classA
? I seem to be able to have it call either the default constructor ofclassB
OR the overloaded constructor ofclassA
by declaring the overloaded constructor ofclassB
aspublic classB(int i) : this()
orpublic classB(int i) : base(i)
but how do I get it to call both?but how do I get it to call both? You don't, cause you can only have on base instance, so you can't call two constructors. You'll have to put the code from both base class constructors into two protected methods and call each from the respective base class constructor, and call both from the derived class constructor.
-
Hi all, I'm having a hard time getting my head around
this()
andbase()
, hoping someone can help. Let's say I haveclassB
which inheritsclassA
both have a constructor and an overloaded constructor.class classA
{
public classA()
{
//some code
}
public classA(int i)
{
//some code
}
}class classB
{
public classB()
{
//some code
}
public classB(int i)
{
//some code
}
}How do I get the overloaded constructor of
classB
to first call the default constructor ofclassB
and then the similar overloaded constructor of the base class,classA
? I seem to be able to have it call either the default constructor ofclassB
OR the overloaded constructor ofclassA
by declaring the overloaded constructor ofclassB
aspublic classB(int i) : this()
orpublic classB(int i) : base(i)
but how do I get it to call both?class classB : classA
{
public classB() : this(5)
{
//some code
}
public classB(int i) : base("laser")
{
//some code
}
}calling classB() will run both
betonglasermur.FeedDwarf(pur_is, 17);
ProcessStartupInfo.AintNotCreateNoWindow = (false && !true) != (true || false) ? false == true ? true : false : (true != false && false);Morgonen är tröttmans mecka
-
Try the following:
class classA
{
public classA() : this(0)
{
//some code
}
public classA(int i)
{
//some code
}
}
class classB : classA
{
public classB() : base()
{
//some code
}
public classB(int i) : base(i)
{
//some code
}
}With this, calling the classB constructors will result in the appropriate classA constructors being called. If however, you want to explicitly perform some code in response to the constructors, I would create an OnInit method, and call it from
public classA(int i)
. This means that all of the paths through to it will be handled.Deja View - the feeling that you've seen this post before.
Pete O'Hanlon wrote:
public classB() : base() { //some code }
I think the call to
base()
is not needed here. WhenclassB
is instantiated, parameterless constructor of base class will be called.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
First of all the proper way to inherit is : public ClassB:ClassA { } Secondly, when you are instianting ClassB it calls the parent constructor first and then instiante the derived class. For instance ClassB cb = new ClassB()---> it calls the Parent no value(default constructor) first and then public classB() { } Thirdly, ClassB cb = new Classb(2)-->it calls the Parent value constructor in ur case is public classA(int i) { } and then the dervied class constructor public classB(int i) { //some code } hope it may help
Are you sure about that second case? As far as I understand (and can reproduce), if you instantiate
ClassB cb = new ClassB(2)
it calls the default constructor ofClassA
first and then the overloaded constructor ofClassB
. It never calls the overloaded constructor of ClassA as you say. This is of course if you don't specify for the constructors whether they should be calling other constructors. If I wanted the overloaded constructor of ClassB to first call the overloaded constructor of ClassA as opposed to the default constructor of ClassA, I would have had to specify it as:public ClassB(int i) : base(i)
{
//some code
}Am I missing something?
-
Try the following:
class classA
{
public classA() : this(0)
{
//some code
}
public classA(int i)
{
//some code
}
}
class classB : classA
{
public classB() : base()
{
//some code
}
public classB(int i) : base(i)
{
//some code
}
}With this, calling the classB constructors will result in the appropriate classA constructors being called. If however, you want to explicitly perform some code in response to the constructors, I would create an OnInit method, and call it from
public classA(int i)
. This means that all of the paths through to it will be handled.Deja View - the feeling that you've seen this post before.
Thanks, but this won't exactly do what I'm looking for. I'm looking for a way to have an instantiation of
ClassB cb = new ClassB(1)
make a call to both constructors of ClassB as well as the default constructor of ClassA. The way you suggest would result in an instantiation as above to only call the overloaded constructors of both classes, but not the default onstructor of ClassA. Not to worry though. The more I think about it, the more I realise that I'm trying to use OOP in a way it's not meant to be used. I've found another way to achieve what I want anyway. Also, I think your suggestion of an OnInit method makes better sense as well. Thanks again. -
class classB : classA
{
public classB() : this(5)
{
//some code
}
public classB(int i) : base("laser")
{
//some code
}
}calling classB() will run both
betonglasermur.FeedDwarf(pur_is, 17);
ProcessStartupInfo.AintNotCreateNoWindow = (false && !true) != (true || false) ? false == true ? true : false : (true != false && false);Morgonen är tröttmans mecka
-
but how do I get it to call both? You don't, cause you can only have on base instance, so you can't call two constructors. You'll have to put the code from both base class constructors into two protected methods and call each from the respective base class constructor, and call both from the derived class constructor.