|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Base Class constructor problemand am having a problem. Original Class *********************** using System; using System.IO; namespace FtsData { abstract class testClass { protected object _objInitial; public testClass(object initial) { _objInitial = initial; } public object First { get { return _objInitial; } } } class BoolType2 : testClass { public BoolType2() { } public BoolType2(bool initial) { _objInitial = initial; } } } *********************** This works fine. but I wanted to move the Constructors from the Child to the Base since each type I build (boolean, string, integer, etc) will have as little code to write. If I moved the constructor (with a value passed) to the base, it caused an error with the void constructor: ******************************** using System; using System.IO; namespace FtsData { abstract class testClass { protected object _objInitial; public testClass(object initial) { _objInitial = initial; } public object First { get { return _objInitial; } } } class BoolType2 : testClass { public BoolType2() { } public BoolType2(bool initial):base(initial) { //_objInitial = initial; } } } ******************************** Now I get an error saying: No overload for method 'testClass' takes '0' arguments This is because I have a void constructor in my child class but not in my base class. But I didn't need this constructor in my initial example above. Only after I added a constructor. It seems at that point any constructors that are in my child class are necessary in my base class. I found that I don't have to set it so that the child void class references the child class (as I did with my child class with one parameter). But the void class did have to be there. Also, can I have my constructors in my base class but not in my child classes? ******************************************* using System; using System.IO; namespace FtsData { abstract class testClass { protected object _objInitial; public testClass() { } public testClass(object initial) { _objInitial = initial; } public object First { get { return _objInitial; } } } class BoolType2 : testClass { } } ******************************************* Thanks, Tom tshad wrote:
> No overload for method 'testClass' takes '0' arguments Actually it's because you are not specifying which constructor in the > > This is because I have a void constructor in my child class but not in my > base class. base class to call, and there is no parameterless constructor that can be called by default. Every constructor has to call a constructor in the base class. If you don't specify one, the parameterless constructor is called by default. As there is no parameterless constructor in the base class, you get the error message above. > But I didn't need this constructor in my initial example above. Only after No, there is no such relation between the constructors.> I added a constructor. It seems at that point any constructors that are in > my child class are necessary in my base class. > Also, can I have my constructors in my base class but not in my child If you don't specify any constructor, a parameterless constructor is > classes? created by default. That means that you can create instances of the child class, but you can't specify the initial value. A constructor in a class can only create instances of that class, not of any other class. Constructors are not inherited.
Show quote
Hide quote
"Göran Andersson" <gu***@guffa.com> wrote in message But then the question still stands. Why did I get an error in this news:OY2VlpkXJHA.5312@TK2MSFTNGP02.phx.gbl... > tshad wrote: >> No overload for method 'testClass' takes '0' arguments >> >> This is because I have a void constructor in my child class but not in my >> base class. > > Actually it's because you are not specifying which constructor in the base > class to call, and there is no parameterless constructor that can be > called by default. > > Every constructor has to call a constructor in the base class. If you > don't specify one, the parameterless constructor is called by default. As > there is no parameterless constructor in the base class, you get the error > message above. > >> But I didn't need this constructor in my initial example above. Only >> after I added a constructor. It seems at that point any constructors >> that are in my child class are necessary in my base class. > > No, there is no such relation between the constructors. > situation? ******************************** using System; using System.IO; namespace FtsData { abstract class testClass { protected object _objInitial; public testClass(object initial) { _objInitial = initial; } public object First { get { return _objInitial; } } } class BoolType2 : testClass { public BoolType2() { } public BoolType2(bool initial):base(initial) { //_objInitial = initial; } } } ******************************** This gave me an error: No overload for method 'testClass' takes '0' arguments There is default constructor in my child class, so why did it give an error? As I mentioned before there was no error, if I didn't have the constructor with the parameter. Thanks, Tom Show quoteHide quote >> Also, can I have my constructors in my base class but not in my child >> classes? > > If you don't specify any constructor, a parameterless constructor is > created by default. That means that you can create instances of the child > class, but you can't specify the initial value. > > A constructor in a class can only create instances of that class, not of > any other class. Constructors are not inherited. > > -- > Göran Andersson > _____ > http://www.guffa.com tshad wrote:
Show quoteHide quote > "Göran Andersson" <gu***@guffa.com> wrote in message 8< snip> news:OY2VlpkXJHA.5312@TK2MSFTNGP02.phx.gbl... >> tshad wrote: >>> No overload for method 'testClass' takes '0' arguments >>> >>> This is because I have a void constructor in my child class but not in my >>> base class. >> Actually it's because you are not specifying which constructor in the base >> class to call, and there is no parameterless constructor that can be >> called by default. >> >> Every constructor has to call a constructor in the base class. If you >> don't specify one, the parameterless constructor is called by default. As >> there is no parameterless constructor in the base class, you get the error >> message above. >> >>> But I didn't need this constructor in my initial example above. Only >>> after I added a constructor. It seems at that point any constructors >>> that are in my child class are necessary in my base class. >> No, there is no such relation between the constructors. >> > > But then the question still stands. Why did I get an error in this > situation? > > You mean a parameterless constructor.> This gave me an error: > > No overload for method 'testClass' takes '0' arguments > > There is default constructor in my child class, > so why did it give an error? Because the parameterless constructor in the child class doesn't specify which constructor to call in the base class. Therefore it by default calls the parameterless constructor in the base class, but there is no parameterless constructor on the base class. > As I mentioned before there was no error, if I didn't have the constructor Then the constructor in the base class didn't have a parameter, so that > with the parameter. it could be called by default. Tom... If you declare any constructor in the base class then the
compiler will not auto generate a parameterless base class constructor. The following will not compile since there is no auto generated base class constructor for public testClass(). using System; using System.Collections.Generic; using System.Text; namespace TestConstructor1 { class Program { static void Main(string[] args) { } } abstract class testClass { protected object _objInitial; public testClass(object initial) { _objInitial = initial; } } class BoolType2 : testClass { public BoolType2() { } } } Regards, Jeff *** Sent via Developersdex http://www.developersdex.com *** I think I see now, sort of. The child class makes the difference. If there
is no child class, it doesn't matter what you do with the class. It can have a parameterless constructor or not, even if it has another constructor. That doesn't matter. If I don't have a child class inheriting the base class, I don't need a default class. This compiles fine. Not sure why this is - if it isn't with a child defined (even if the child doesn't have a parameterless constructor). This works fine (whether or not the class is abstract). ********************* using System; using System.IO; namespace FtsData { abstract class testClass { protected object _objInitial; public testClass(object initial) { _objInitial = initial; } public object First { get { return _objInitial; } } } } ******************** If I have a child class, I HAVE to have a parameterless constructor in the base class. It can be defined or no constructor at all and it will create a default one for me. This works: *********************** using System; using System.IO; namespace FtsData { abstract class testClass { protected object _objInitial; public object First { get { return _objInitial; } } } class BoolType2 : testClass { } } *********************** And this works: *************************** using System; using System.IO; namespace FtsData { abstract class testClass { protected object _objInitial; public testClass() { } public testClass(object initial) { _objInitial = initial; } public object First { get { return _objInitial; } } } class BoolType2 : testClass { } } *************************** But this doesn't: ***************************** using System; using System.IO; namespace FtsData { abstract class testClass { protected object _objInitial; public testClass(object initial) { _objInitial = initial; } public object First { get { return _objInitial; } } } class BoolType2 : testClass { } } ***************************** I assume that that is because Show quoteHide quote "Jeff Louie" <anonym***@devdex.com> wrote in message news:OeyKzqlXJHA.2440@TK2MSFTNGP06.phx.gbl... > Tom... If you declare any constructor in the base class then the > compiler will > not auto generate a parameterless base class constructor. The following > will > not compile since there is no auto generated base class constructor for > public > testClass(). > > using System; > using System.Collections.Generic; > using System.Text; > > namespace TestConstructor1 > { > class Program > { > static void Main(string[] args) > { > } > } > > abstract class testClass > { > protected object _objInitial; > > public testClass(object initial) > { > _objInitial = initial; > } > } > > class BoolType2 : testClass > { > public BoolType2() > { > } > > } > > } > > Regards, > Jeff > > *** Sent via Developersdex http://www.developersdex.com *** tshad wrote:
> If I have a child class, I HAVE to have a parameterless constructor in the Not at all.> base class. You just have to specify which constructor to call from the child constructor if there is no parameterless constructor in the base class: abstract class testClass { protected object _objInitial; public testClass(object initial) { _objInitial = initial; } public object First { get { return _objInitial; } } } class BoolType2 : testClass { public BoolType2() : base (false) {} } "Göran Andersson" <gu***@guffa.com> wrote in message So in the case where I have no constructor in the child class, it will try news:ueSqqYoXJHA.256@TK2MSFTNGP06.phx.gbl... > tshad wrote: >> If I have a child class, I HAVE to have a parameterless constructor in >> the base class. > > Not at all. > > You just have to specify which constructor to call from the child > constructor if there is no parameterless constructor in the base class: to call the default constructor of the child class as well as the default constructor of the base class (since I didn't tell it which constructor to use)? A constructor for a base class will always be called for a child class? And will call the following constructors in the base: 1) the constructor specified by the child class first if there. 2) the parameterless constructor of the base if there and nothing specified by the child constructor 3) will call the default if 1 and 2 are not handled. Thanks, Tom Show quoteHide quote > > abstract class testClass { > > protected object _objInitial; > > public testClass(object initial) { > _objInitial = initial; > } > > public object First { > get { return _objInitial; } > } > > } > > class BoolType2 : testClass { > > public BoolType2() : base (false) {} > > } > > -- > Göran Andersson > _____ > http://www.guffa.com tshad wrote:
Show quoteHide quote > "Göran Andersson" <gu***@guffa.com> wrote in message Yes, a parameterless constructor is created for the child class, just as > news:ueSqqYoXJHA.256@TK2MSFTNGP06.phx.gbl... >> tshad wrote: >>> If I have a child class, I HAVE to have a parameterless constructor in >>> the base class. >> Not at all. >> >> You just have to specify which constructor to call from the child >> constructor if there is no parameterless constructor in the base class: > > So in the case where I have no constructor in the child class, it will try > to call the default constructor of the child class as well as the default > constructor of the base class (since I didn't tell it which constructor to > use)? if you had put this code in it: public BoolType2() : base() {} > A constructor for a base class will always be called for a child class? Yes.> And will call the following constructors in the base: It's simpler than that:> 1) the constructor specified by the child class first if there. > 2) the parameterless constructor of the base if there and nothing specified > by the child constructor > 3) will call the default if 1 and 2 are not handled. 1) If the constructor in the child class specifies a constructor to call, that will be called. 2) Otherwise the parameterless constructor in the base class will be called. A constructor may call a constructor in the same class instead, but somewhere along the line it ends in calling a constructor in the base class. Example: public class BoolType2 { public BoolType2(bool value) : base(value) {} public BoolType2() : this(false) {} }
Show quote
Hide quote
"Göran Andersson" <gu***@guffa.com> wrote in message Got it.news:uullCwpXJHA.5272@TK2MSFTNGP04.phx.gbl... > tshad wrote: >> "Göran Andersson" <gu***@guffa.com> wrote in message >> news:ueSqqYoXJHA.256@TK2MSFTNGP06.phx.gbl... >>> tshad wrote: >>>> If I have a child class, I HAVE to have a parameterless constructor in >>>> the base class. >>> Not at all. >>> >>> You just have to specify which constructor to call from the child >>> constructor if there is no parameterless constructor in the base class: >> >> So in the case where I have no constructor in the child class, it will >> try to call the default constructor of the child class as well as the >> default constructor of the base class (since I didn't tell it which >> constructor to use)? > > Yes, a parameterless constructor is created for the child class, just as > if you had put this code in it: > > public BoolType2() : base() {} > >> A constructor for a base class will always be called for a child class? > > Yes. > >> And will call the following constructors in the base: >> 1) the constructor specified by the child class first if there. >> 2) the parameterless constructor of the base if there and nothing >> specified by the child constructor >> 3) will call the default if 1 and 2 are not handled. > > It's simpler than that: > > 1) If the constructor in the child class specifies a constructor to call, > that will be called. > 2) Otherwise the parameterless constructor in the base class will be > called. > And if there is no constructor in either, then the default constructor will be called for both the child and the base (base gets executed first then the child). Thanks, Tom Show quoteHide quote > A constructor may call a constructor in the same class instead, but > somewhere along the line it ends in calling a constructor in the base > class. Example: > > public class BoolType2 { > > public BoolType2(bool value) : base(value) {} > > public BoolType2() : this(false) {} > > } > > -- > Göran Andersson > _____ > http://www.guffa.com
Other interesting topics
Override the property
Compare and Null FolderBrowserDialog won't open sometimes Bitmpa.Save & GZipStream - error Value equality ProvideProperty attribute linq --> changing a value? Extract Text (string) from doc, xls, pdf ... I will pay for Null dynamic data in .net 3.5 SP1 -- performance, multi-tier |
|||||||||||||||||||||||