473,573 Members | 3,036 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Require base class member to be populated by derived classes.

I have a base class that must have a member variable populated by, and only
by, derived classes.

It appears that if I declare the variable as "internal protected" then the
base class *can* populate the variable, but the population is not *required*
by the derived class (which must be the case).

What would meet the requirements is if I create an abstract method in the
base class that populates the member variable. In this case the derived
classes would be required to implement the method.

Is there another way to meet the requirement?... specifically that a member
variable of a base class *must* be populated by a derived class (and only by
derived classes)?

Thanks!
Jun 15 '07 #1
15 3826
Bob,

Internal protected allows internal classes to access the field as well,
so you might want to consider just protected if you want just base classes
to be able to access the field.

There really isn't way to enforce that the derived type populate the
field. The best way I can think of to do this is to set the default value
of the field to a value that is valid for the field type, but not for what
you are trying to do (e.g., 0 if it is an integer, null if it is a reference
type). Then, when your base class works with the field, and it is the
invalid value, you can throw an exception.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bob Johnson" <A@B.COMwrote in message
news:Ov******** ******@TK2MSFTN GP03.phx.gbl...
>I have a base class that must have a member variable populated by, and only
by, derived classes.

It appears that if I declare the variable as "internal protected" then the
base class *can* populate the variable, but the population is not
*required* by the derived class (which must be the case).

What would meet the requirements is if I create an abstract method in the
base class that populates the member variable. In this case the derived
classes would be required to implement the method.

Is there another way to meet the requirement?... specifically that a
member variable of a base class *must* be populated by a derived class
(and only by derived classes)?

Thanks!

Jun 15 '07 #2
Yes - it helps. What I'm doing is writing a small console app for purposes
of me learning the strategy pattern. I'm adding reference member variables
to a base class. These reference variables are for adding "behavior" to the
base class (by holding an instance of a class that implements said
behavior). The derived classes then are supposed to implement whatever
behavior is relevant to the derived class BY instantiating and populating
the base class' "behavior variables".

This all works fine and well, but it dawned on me that there is nothing
*requiring* the derived class to implement the behavior (i.e., populate the
base class "behavior bariables".

Thus the question. As the designer of the original implementation here, I'm
thinking that [in a real-world scenario] future developers might add new
derived classes to the system without knowing that they must implement the
behavior (by populating the "behavior variables" in the base class). By
"must implement" I'm thinking that could be a requirement of the real-world
situation... where some class of objects MUST implement some specific
type(s) of behavior (thus they're in the base class) and the particular
behavior is known/added by the derived class at runtime. It would apparently
be up to the documentation (or runtime errors) to inform the future/other
developers that they must implement the behavior.

-Bob

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
Bob,

Internal protected allows internal classes to access the field as well,
so you might want to consider just protected if you want just base classes
to be able to access the field.

There really isn't way to enforce that the derived type populate the
field. The best way I can think of to do this is to set the default value
of the field to a value that is valid for the field type, but not for what
you are trying to do (e.g., 0 if it is an integer, null if it is a
reference type). Then, when your base class works with the field, and it
is the invalid value, you can throw an exception.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bob Johnson" <A@B.COMwrote in message
news:Ov******** ******@TK2MSFTN GP03.phx.gbl...
>>I have a base class that must have a member variable populated by, and
only by, derived classes.

It appears that if I declare the variable as "internal protected" then
the base class *can* populate the variable, but the population is not
*required* by the derived class (which must be the case).

What would meet the requirements is if I create an abstract method in the
base class that populates the member variable. In this case the derived
classes would be required to implement the method.

Is there another way to meet the requirement?... specifically that a
member variable of a base class *must* be populated by a derived class
(and only by derived classes)?

Thanks!


Jun 15 '07 #3
Abstract is the only way to check this at compile time. You can
guarantee your field is set by putting something like this in the base
class

object m_FieldValue;
object FieldValue
{
get
{
if(m_FieldValue == null)
m_FieldValue = IntialFieldValu e;
return m_FieldValue;
}
}
protected abstract object IntialFieldValu e { get; }
Jun 15 '07 #4
Make your member variable private to the base class. Then make your base
class constructor protected and have it take a parameter of the variable
type. In order to be instantiated the derived class must call the protected
constructor. Validate the parameter in the constructor and thow an exception
or set your base class variable as the case may be.
"Bob Johnson" <A@B.COMwrote in message
news:Ov******** ******@TK2MSFTN GP03.phx.gbl...
>I have a base class that must have a member variable populated by, and only
by, derived classes.

It appears that if I declare the variable as "internal protected" then the
base class *can* populate the variable, but the population is not
*required* by the derived class (which must be the case).

What would meet the requirements is if I create an abstract method in the
base class that populates the member variable. In this case the derived
classes would be required to implement the method.

Is there another way to meet the requirement?... specifically that a
member variable of a base class *must* be populated by a derived class
(and only by derived classes)?

Thanks!

Jun 15 '07 #5
Bob,

I think that in this case, it might be better to provide default
implementations when you expect there to be certain behaviors present, and
then offer a way for the derived class to provide them, if they wish. This
way, if the behaviors aren't provided by the appropriate derived classes,
you have a default to fall back on.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bob Johnson" <A@B.COMwrote in message
news:eC******** ******@TK2MSFTN GP02.phx.gbl...
Yes - it helps. What I'm doing is writing a small console app for purposes
of me learning the strategy pattern. I'm adding reference member variables
to a base class. These reference variables are for adding "behavior" to
the base class (by holding an instance of a class that implements said
behavior). The derived classes then are supposed to implement whatever
behavior is relevant to the derived class BY instantiating and populating
the base class' "behavior variables".

This all works fine and well, but it dawned on me that there is nothing
*requiring* the derived class to implement the behavior (i.e., populate
the base class "behavior bariables".

Thus the question. As the designer of the original implementation here,
I'm thinking that [in a real-world scenario] future developers might add
new derived classes to the system without knowing that they must implement
the behavior (by populating the "behavior variables" in the base class).
By "must implement" I'm thinking that could be a requirement of the
real-world situation... where some class of objects MUST implement some
specific type(s) of behavior (thus they're in the base class) and the
particular behavior is known/added by the derived class at runtime. It
would apparently be up to the documentation (or runtime errors) to inform
the future/other developers that they must implement the behavior.

-Bob

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote
in message news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
>Bob,

Internal protected allows internal classes to access the field as
well, so you might want to consider just protected if you want just base
classes to be able to access the field.

There really isn't way to enforce that the derived type populate the
field. The best way I can think of to do this is to set the default
value of the field to a value that is valid for the field type, but not
for what you are trying to do (e.g., 0 if it is an integer, null if it is
a reference type). Then, when your base class works with the field, and
it is the invalid value, you can throw an exception.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bob Johnson" <A@B.COMwrote in message
news:Ov******* *******@TK2MSFT NGP03.phx.gbl.. .
>>>I have a base class that must have a member variable populated by, and
only by, derived classes.

It appears that if I declare the variable as "internal protected" then
the base class *can* populate the variable, but the population is not
*required* by the derived class (which must be the case).

What would meet the requirements is if I create an abstract method in
the base class that populates the member variable. In this case the
derived classes would be required to implement the method.

Is there another way to meet the requirement?... specifically that a
member variable of a base class *must* be populated by a derived class
(and only by derived classes)?

Thanks!



Jun 15 '07 #6
Hi,

"Bob Johnson" <A@B.COMwrote in message
news:Ov******** ******@TK2MSFTN GP03.phx.gbl...
>I have a base class that must have a member variable populated by, and only
by, derived classes.
Do you have good reasons for this? , I would love to hear them.

In reality there is no way to enforce this. Of course if you are the one
writting the base class you can be sure of not using it and you are ready :)
another posibility is to use an abstract property. if you do not implement
it in the base class you know for sure it was implemented in a derived class
and you don't really have to worry about members variables.
Jun 15 '07 #7
On Fri, 15 Jun 2007 08:53:12 -0700, Bob Johnson <A@B.COMwrote :
[...]
What would meet the requirements is if I create an abstract method in the
base class that populates the member variable. In this case the derived
classes would be required to implement the method.
As Nicholas says, there's not a way to force derived classes to initialize
a particular member variable, other than simply stipulating that they must
and doing some sort of run-time check that they have.

Creating an abstract method only requires that a method with the same
signature is implemented in the derived class, but even that does not make
any requirements as to what that method does. It could do nothing, or it
could initialize your member, or it could do something entirely
different. You have no way to control what the implementation actually is.

Pete
Jun 15 '07 #8
PS

"Bob Johnson" <A@B.COMwrote in message
news:Ov******** ******@TK2MSFTN GP03.phx.gbl...
>I have a base class that must have a member variable populated by, and only
by, derived classes.

It appears that if I declare the variable as "internal protected" then the
base class *can* populate the variable, but the population is not
*required* by the derived class (which must be the case).

What would meet the requirements is if I create an abstract method in the
base class that populates the member variable. In this case the derived
classes would be required to implement the method.

Is there another way to meet the requirement?... specifically that a
member variable of a base class *must* be populated by a derived class
(and only by derived classes)?
IPerhaps I am misreading the question. Can't you have an abstract class with
one constructor that has all the "necessary" parameters?

public abstract class Abstract
{
protected int mustSetMe;
public Abstract(int mustSetMe)
{
this.mustSetMe = mustSetMe;
}
}
public class Derived : Abstract
{
public Derived() : base(101)
{
int i = base.mustSetMe;
}
}

PS
Jun 15 '07 #9
YES!

RE:
<< Can't you have an abstract class with one constructor that has all the
"necessary" parameters>>

That gets me what I was looking for. Thanks. So obvious now.

Now please excuse me while I go and knock my head against the wall :-)

-B

"PS" <ec***********@ hotmail.comwrot e in message
news:ue******** ******@TK2MSFTN GP05.phx.gbl...
>
"Bob Johnson" <A@B.COMwrote in message
news:Ov******** ******@TK2MSFTN GP03.phx.gbl...
>>I have a base class that must have a member variable populated by, and
only by, derived classes.

It appears that if I declare the variable as "internal protected" then
the base class *can* populate the variable, but the population is not
*required* by the derived class (which must be the case).

What would meet the requirements is if I create an abstract method in the
base class that populates the member variable. In this case the derived
classes would be required to implement the method.

Is there another way to meet the requirement?... specifically that a
member variable of a base class *must* be populated by a derived class
(and only by derived classes)?

IPerhaps I am misreading the question. Can't you have an abstract class
with one constructor that has all the "necessary" parameters?

public abstract class Abstract
{
protected int mustSetMe;
public Abstract(int mustSetMe)
{
this.mustSetMe = mustSetMe;
}
}
public class Derived : Abstract
{
public Derived() : base(101)
{
int i = base.mustSetMe;
}
}

PS

Jun 15 '07 #10

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

Similar topics

8
1645
by: Webster | last post by:
Hello, I have a class Shape and subclasses Circle, Rectangle etc. I also have a function to write them to a file given a filepointer. However, I store the colour info in the Shape class. My question is, is it possible to create a WriteColour function in the Shape class, but when I call the sub class writes (i.e. Circle.Write(...)), it...
6
6655
by: Microsoft | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
7
6604
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
0
917
by: Armin Zingler | last post by:
Hi group, how can I have a base class member implement an interface member? Example: class a sub Member1 end sub end class
11
32286
by: Frederic Rentsch | last post by:
Hi all, If I derive a class from another one because I need a few extra features, is there a way to promote the base class to the derived one without having to make copies of all attributes? class Derived (Base): def __init__ (self, base_object): # ( copy all attributes ) ...
11
18322
by: Rahul | last post by:
Hi Everyone, While working with Java, i came across super() which passes values to base class constructor from derived class constructor. I was wondering if this could be implemented in c++ by any mechanism as super is not supported by c++, atleast by MS vc++ 6.0.
12
2850
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair of pointers, where each is a pointer to the base class (each is a SPRITE *). I know that each of these pointers actually points to one of the...
0
8029
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
7794
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
6424
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...
1
5600
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
5293
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
3734
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...
0
3735
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1307
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1044
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.