Constructors
-
Hi, In an inherited class is it possible to stop the base class constructor from being called? Thanks
No. But u can play some tricks and call different base class constructors. That mite work for your case. top secret xacc-ide 0.0.1
-
Hi, In an inherited class is it possible to stop the base class constructor from being called? Thanks
IIRC, the base constructor must be called in order to initialise things like private member variables that cannot be initialised from a derived class because it will be invisible to the derived class. Probably your best solution would be to create a protected constructor in the base that does nothing and have your derived class call it instead of the default constructor.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way! My Blog
-
Hi, In an inherited class is it possible to stop the base class constructor from being called? Thanks
Don't define a default constructor in your inheritted class. Also make the default constructor private in the base class.
Microsoft MVP, Visual C# My Articles
-
Don't define a default constructor in your inheritted class. Also make the default constructor private in the base class.
Microsoft MVP, Visual C# My Articles
Tried that with the base classes constructor being private but then I can't inherit from it ( compile error ). eg. public class inheritance { public String strName; private inheritance() { showMessage(); } public virtual void showMessage() { MessageBox.Show("Base Class"); } } public class inheritance2: inheritance { public String strTest; public override void showMessage() { MessageBox.Show("inheritance2"); } }
-
Don't define a default constructor in your inheritted class. Also make the default constructor private in the base class.
Microsoft MVP, Visual C# My Articles
Heath Stewart wrote: Also make the default constructor private in the base class. No, the default constructor would always be called, regardless. Heath Stewart wrote: Also make the default constructor private in the base class. If it's private in the base class, the derived class will not be able to call it automatically and the compiler will complain. The only solution is to add a protected constructor to the base class (assuming you can) and have the derived constructor call that explicitly. Regards, Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
-
Don't define a default constructor in your inheritted class. Also make the default constructor private in the base class.
Microsoft MVP, Visual C# My Articles
Which idiot does vote your comments down to "1"? :mad: Im now voting "5" against when reading them :-) -- - Free Windows-based CMS: www.zeta-software.de/enu/producer/freeware/download.html - See me: www.magerquark.de - MSN Messenger: uwe_keim@hotmail.com
-
Heath Stewart wrote: Also make the default constructor private in the base class. No, the default constructor would always be called, regardless. Heath Stewart wrote: Also make the default constructor private in the base class. If it's private in the base class, the derived class will not be able to call it automatically and the compiler will complain. The only solution is to add a protected constructor to the base class (assuming you can) and have the derived constructor call that explicitly. Regards, Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
Really? Try compiling this and examining the IL with ildasm.exe or something:
using System;
public abstract class Test
{
private Test()
{
}protected Test(string name) { }
}
public class Test2 : Test
{
protected Test2(string name) : base(name)
{
}
}You're right - the default ctor of the base class won't be called, but if it isn't private it will be called. There was no requirement in the original post for the default ctor of the base class to be called.
Microsoft MVP, Visual C# My Articles
-
Which idiot does vote your comments down to "1"? :mad: Im now voting "5" against when reading them :-) -- - Free Windows-based CMS: www.zeta-software.de/enu/producer/freeware/download.html - See me: www.magerquark.de - MSN Messenger: uwe_keim@hotmail.com
Thanks. :) I don't mind if people don't always agree with me, but when I'm right, I'm right - and in this case I'm right. See my reply to Alvaro.
Microsoft MVP, Visual C# My Articles
-
Tried that with the base classes constructor being private but then I can't inherit from it ( compile error ). eg. public class inheritance { public String strName; private inheritance() { showMessage(); } public virtual void showMessage() { MessageBox.Show("Base Class"); } } public class inheritance2: inheritance { public String strTest; public override void showMessage() { MessageBox.Show("inheritance2"); } }
You can inherit, but only if you don't have a default constructor in your derivative class. If the base class's default ctor isn't private, it will be called. See my reply to Alvaro below for an example.
Microsoft MVP, Visual C# My Articles