473,586 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructor firing Inheritance in C#

I'm having a problem, and here is a simplified example of code that
demonstrates it:
public class BizObj
{
public string TableName="";
private DataSet oData;

public BizObj()
{
LoadData();
}

public void LoadData()
{
SqlConnection oConn = new SqlConnection(" ...");
SqlDataAdapter oAdapter = new SqlDataAdapter( "SELECT * FROM
"+this.TableNam e,oConn);
oAdapter.Fill(o Data);

}
}

public class AuthorBizObj: BizObj
{
public AuthorBizObj()
{
this.TableName= "Authors";
}
}

Now, because the base class constructor fires first, LoadData is called
before the TableName can be set to "Authors".

So I try to override the TableName field in the following manner, hoping
that it will be set before the constructor methods fire:
public class AuthorBizObj: BizObj
{
public string TableName="Auth ors";

public AuthorBizObj()
{
}
}

But I get a compile warning: "The keyword new is required on
AuthorBizObj.Ta bleName because it hides inherited member BizObj.TableNam e."

I'm not trying to hide the member, I'm trying to override it.

How can I make sure that the TableName field has the value "Authors" before
the BizObj constructor fires?

<b>This question is strictly about inheritance.</b>

You might say "Pass a value for TableName to the constructor" , but I think
that the Authors bizobj should know its table, not have its name passed into
it.

You might ask "Why do you want to call LoadData in the constructor?" or say
"Call LoadData after you have instantiated AuthorBizObj, not in the
constructor" - I'm actually not creating bizobjs, this is just some sample
code that demonstrates the problem, I'm trying to override a member field,
not figure out where LoadData should be called.

Thanks,
--
Rick Hodder
Jan 20 '06 #1
11 2396
What about something like this? Here name gets set before the WriteLine is called.

class BaseObject
{
protected string name = string.Empty;

protected BaseObject()
{
Init();
}

protected virtual void Init()
{
System.Diagnost ics.Debug.Write Line(name);
}
}

class DerivedObject : BaseObject
{
public DerivedObject()
{
}

protected override void Init()
{
name = "MyTable";
base.Init();
}
}
RickHodder wrote:
I'm having a problem, and here is a simplified example of code that
demonstrates it:
public class BizObj
{
public string TableName="";
private DataSet oData;

public BizObj()
{
LoadData();
}

public void LoadData()
{
SqlConnection oConn = new SqlConnection(" ...");
SqlDataAdapter oAdapter = new SqlDataAdapter( "SELECT * FROM
"+this.TableNam e,oConn);
oAdapter.Fill(o Data);

}
}

public class AuthorBizObj: BizObj
{
public AuthorBizObj()
{
this.TableName= "Authors";
}
}

Now, because the base class constructor fires first, LoadData is called
before the TableName can be set to "Authors".

So I try to override the TableName field in the following manner, hoping
that it will be set before the constructor methods fire:
public class AuthorBizObj: BizObj
{
public string TableName="Auth ors";

public AuthorBizObj()
{
}
}

But I get a compile warning: "The keyword new is required on
AuthorBizObj.Ta bleName because it hides inherited member BizObj.TableNam e."

I'm not trying to hide the member, I'm trying to override it.

How can I make sure that the TableName field has the value "Authors" before
the BizObj constructor fires?

<b>This question is strictly about inheritance.</b>

You might say "Pass a value for TableName to the constructor" , but I think
that the Authors bizobj should know its table, not have its name passed into
it.

You might ask "Why do you want to call LoadData in the constructor?" or say
"Call LoadData after you have instantiated AuthorBizObj, not in the
constructor" - I'm actually not creating bizobjs, this is just some sample
code that demonstrates the problem, I'm trying to override a member field,
not figure out where LoadData should be called.

Thanks,

Jan 20 '06 #2
I'm not trying to hide the member, I'm trying to override it.
Well you can't override fields.

You might say "Pass a value for TableName to the constructor" ,
Exactly.

but I think
that the Authors bizobj should know its table, not have its name passed into
it.


I don't see how that prevents you from passing it to a BizObj
constructor. Wouldn't this work for you?

public class BizObj
{
public string TableName;

protected BizObj(string tableName)
{
TableName = tableName;
LoadData();
}

...
}

public class AuthorBizObj : BizObj
{
public AuthorBizObj() : base("Authors")
{
}
}
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 20 '06 #3
> I'm having a problem, and here is a simplified example of code that
demonstrates it:

public class BizObj
{
public string TableName="";
private DataSet oData;
public BizObj()
{
LoadData();
}
public void LoadData()
{
SqlConnection oConn = new SqlConnection(" ...");
SqlDataAdapter oAdapter = new SqlDataAdapter( "SELECT * FROM
"+this.TableNam e,oConn);
oAdapter.Fill(o Data);
}
}
public class AuthorBizObj: BizObj
{
public AuthorBizObj()
{
this.TableName= "Authors";
}
}
Now, because the base class constructor fires first, LoadData is
called before the TableName can be set to "Authors".

So I try to override the TableName field in the following manner,
hoping that it will be set before the constructor methods fire:

public class AuthorBizObj: BizObj
{
public string TableName="Auth ors";
public AuthorBizObj()
{
}
}
But I get a compile warning: "The keyword new is required on
AuthorBizObj.Ta bleName because it hides inherited member
BizObj.TableNam e."

I'm not trying to hide the member, I'm trying to override it.

How can I make sure that the TableName field has the value "Authors"
before the BizObj constructor fires?

<b>This question is strictly about inheritance.</b>

You might say "Pass a value for TableName to the constructor" , but I
think that the Authors bizobj should know its table, not have its name
passed into it.

You might ask "Why do you want to call LoadData in the constructor?"
or say "Call LoadData after you have instantiated AuthorBizObj, not in
the constructor" - I'm actually not creating bizobjs, this is just
some sample code that demonstrates the problem, I'm trying to override
a member field, not figure out where LoadData should be called.

Thanks,


I think "overriding " or "hiding" a field is bad news. How about this.

public abstract class BizObj
{
private readonly string TableName = string.Empty;

protected BizObj(string tableName)
{
this.TableName = tableName;
Console.WriteLi ne("BizObj Ctor");
LoadData();
}

protected void LoadData()
{
Console.WriteLi ne("TableName: {0}", TableName);
}
}

public class AuthorBizObj : BizObj
{
public AuthorBizObj() : base("Author")
{
Console.WriteLi ne("AuthorBizOb j Ctor.");
}
}
Jan 20 '06 #4
Hi Darren,

Thanks for the reply.

I am trying to override a field: something that should be available in an
object-oriented language, IMO.

For example: If I create a Vehicle class with a WheelCount property, and
then I create a subclass Car, I dont think that I should have to set up a
special method just so that I can set the WheelCount to 4, I should just be
able to initialize WheelCount to 4.

And what's more annoying is that you can override a property - another
developer suggested that I should override the property:

public string TableName
{
get
{
return "Authors";
}
}

But that seems like it circumvents inheritance.

I'm trying to understand the thought process.
--
Rick Hodder

Jan 20 '06 #5
RickHodder <Ri********@dis cussions.micros oft.com> wrote:
Thanks for the reply.

I am trying to override a field: something that should be available in an
object-oriented language, IMO.
It's not been available in any of the languages I've worked in. I don't
see it's necessary, either.
For example: If I create a Vehicle class with a WheelCount property, and
then I create a subclass Car, I dont think that I should have to set up a
special method just so that I can set the WheelCount to 4, I should just be
able to initialize WheelCount to 4.
You could have a virtual *property*, as you say below.
And what's more annoying is that you can override a property - another
developer suggested that I should override the property:

public string TableName
{
get
{
return "Authors";
}
}

But that seems like it circumvents inheritance.

I'm trying to understand the thought process.


In what way does that circumvent inheritance? I'd suggest making it
abstract in the base class, even.

I prefer the idea of passing the name of the table to the base
constructor though. The business object knows the table it's part of
that way, but the business object class itself doesn't know any
specific tables. Sounds ideal to me.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 20 '06 #6
Hi Mattias,

Thanks for replying

I hope what I say doesnt come across as an attack - I'm just trying to
understand.
Well you can't override fields.
What's the thinking behind that - why shouldnt fields be overridable?
Properties are, but they are public, what if I dont want it to be public?
You might say "Pass a value for TableName to the constructor" ,

Exactly.


That seems like a workaround - I'm trying to approach this totally from an
OOP point of view. Why have inheritance of fields if you are going to merely
pass all of the values into the constructor - and as the number of fields in
the base class(es) increase, the signature of the constructor must keep
growing.

I realize that part of the problem here is that I'm calling a method from a
constructor.
I don't see how that prevents you from passing it to a BizObj
constructor. Wouldn't this work for you?


If I know when I create a class what value a field should have, why should I
have to pass it in?

Bizobj was just an example of a class: I'm not trying to argue whether the
tablename should be passed into the constructor when I'm designing a Bizobj
class. But if I know that AuthorBizobj will have a table name "Authors" and
it will never change, why should I have to pass it in to the class?

--
Thanks,
Rick Hodder

Jan 20 '06 #7
RickHodder <Ri********@dis cussions.micros oft.com> wrote:
Well you can't override fields.
What's the thinking behind that - why shouldnt fields be overridable?


Fields aren't callable members - there's no vtable for them. I don't
think I even know what it would mean to be able to override a field.
Properties are, but they are public, what if I dont want it to be public?
Properties aren't always public. They can be private, protected,
internal etc.
You might say "Pass a value for TableName to the constructor" ,

Exactly.


That seems like a workaround - I'm trying to approach this totally from an
OOP point of view. Why have inheritance of fields if you are going to merely
pass all of the values into the constructor - and as the number of fields in
the base class(es) increase, the signature of the constructor must keep
growing.


Well, only the fields for which the base class needs information from
the derived classes need to be initialised in the constructor.
I realize that part of the problem here is that I'm calling a method from a
constructor.
Partly. Calling overridable methods in the constructor is almost always
a no-no. You need to clearly indicate in the documentation that the
method will be called before the object is fully constructed. For
instance, at that point there may be invariants which don't yet hold.
I don't see how that prevents you from passing it to a BizObj
constructor. Wouldn't this work for you?


If I know when I create a class what value a field should have, why should I
have to pass it in?


So that Bizobj doesn't need to know about the Author class at all.
Bizobj was just an example of a class: I'm not trying to argue whether the
tablename should be passed into the constructor when I'm designing a Bizobj
class. But if I know that AuthorBizobj will have a table name "Authors" and
it will never change, why should I have to pass it in to the class?


Well you need to make the data available *somewhere*. I don't see why
passing it in the constructor is worse than having it anywhere else -
it's simple and it works.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 21 '06 #8
Hi Jon,

Thanks for replying
I prefer the idea of passing the name of the table to the base
constructor though. The business object knows the table it's part of
that way, but the business object class itself doesn't know any
specific tables. Sounds ideal to me.


As I said in my post, I'm asking a more general question, the bizobj just
happened to be the example. I was trying to steer away from an app-specific
answer. Passing the name of the table to the bizobj speaks to the developers
thoughts on bizobjs. It works well if the hierarchy is more geared towards a
generic bizobj that won't necessarily be subclassed - you pass in the
tablename and the queries are structured programmaticall y.

I guess the best way to re-state my request is "I have code in a constructor
that calls out to another method . When creating subclasses of this class,
how can I override the values of fields (using initializers maybe??) so that
when the base class calls out to the method, the field is initialized to the
value appropriate for the subclass"

Thanks,
Rick
Jan 23 '06 #9
> Hi Jon,

Thanks for replying
I prefer the idea of passing the name of the table to the base
constructor though. The business object knows the table it's part of
that way, but the business object class itself doesn't know any
specific tables. Sounds ideal to me.

As I said in my post, I'm asking a more general question, the bizobj
just happened to be the example. I was trying to steer away from an
app-specific answer. Passing the name of the table to the bizobj
speaks to the developers thoughts on bizobjs. It works well if the
hierarchy is more geared towards a generic bizobj that won't
necessarily be subclassed - you pass in the tablename and the queries
are structured programmaticall y.

I guess the best way to re-state my request is "I have code in a
constructor that calls out to another method . When creating
subclasses of this class, how can I override the values of fields
(using initializers maybe??) so that when the base class calls out to
the method, the field is initialized to the value appropriate for the
subclass"

Thanks,
Rick


You've already heard the correct answer. You don't "override" fields that
the method depends on. You have to pass the variables into the base constructor.
Jan 23 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
13204
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
0
4245
by: Lefevre | last post by:
Hello I recently had troubles with a class inheritance hierarchy. I solved it, but it didn't satisfied me. I found the solution using this forum :) Actualy i found the following message (with no responces associated) :
10
7218
by: jeffc | last post by:
When compiling the following program, I get an error on the line specified: The base class "B" cannot be initialized because it does not have a default constructor. If I remove the "virtual" inheritance of B (so that it uses plain inheritance), it compiles fine. It also compiles if I invoke the constructor for B explicitly, as shown in the...
3
4549
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
45
6334
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { //...
4
2652
by: Francisco Amaro | last post by:
Hi all, Have question about inheriting a class that has parameters in the constructor such as : Public MustInherit Class MyParentClass Public mystring As String Public Sub New(ByVal paramstring As String)
3
2745
by: kk_oop | last post by:
Hi. I have base class Base and derived class Derived. My derived class constructor is dependent upon attributes set in the base class constructor. Is this okay? Does ANSI C++ guarantee that a base class constructor will complete before the derived class contructor executes? Thanks! Ken
4
3885
by: ingoweiss | last post by:
Hi, I am having trouble passing parameters of a Javascript subclass constructor through to it's superclass constructor. I am trying all sorts of things, including the below, but nothing worked so far. Thanks for any help!
3
2234
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8200
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.