473,770 Members | 2,120 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
11 2416
RickHodder wrote:

<snip>
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"


You *could* have an Init method which could be overridden in derived
classes. However, my best advice would be to try to avoid that design
pattern in the first place.

Jon

Jan 23 '06 #11
It is best to keep as much of the initialization code in the base as possible.
That way, if your base class needs to change there is less impact on derived classes.
Jon Skeet [C# MVP] wrote:
RickHodder wrote:

<snip>
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"


You *could* have an Init method which could be overridden in derived
classes. However, my best advice would be to try to avoid that design
pattern in the first place.

Jon

Jan 25 '06 #12

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

Similar topics

7
13228
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
4256
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
7235
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 comment after the error. I'm not aware of any reason that virtual inheritance should be special...
3
4570
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
45
6368
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 { // Random base class. public class A { protected int i;
4
2662
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
2754
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
3901
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
2252
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
9454
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9906
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.