473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

declare variable in base class that must be set in dervied class?

I have a variable in a base class defined as:

protected Control validationControl;

How can I ensure that it is set is any dervied classes. I see that you
cannot set variables to abstract.

Thanks

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 16 '05 #1
3 2145
Something like that would force the developers of any derived class to
explicitely assign value to variable i that is declared in the base
class. If they don't an assertion message box would pop up.
abstract class Base
{
protected int i = DEFAULT_I;

protected Base()
{
AssignI();
System.Diagnostics.Debug.Assert(i != DEFAULT_I, errorMessage);
System.Diagnostics.Trace.Assert(i != DEFAULT_I, errorMessage);
}

protected abstract void AssignI();

private static int DEFAULT_I = -1;
private static String errorMessage = @"You have to explicitely assign
variable 'i'";
}

class Derived : Base
{
protected override void AssignI()
{
// delete the following line and assertion will fail
i = 1;
}
}

Nov 16 '05 #2
Could you use an abstract Property rather than an actual variable? Let them
worry about the variable...

--
Adam Clauss
ca*****@tamu.edu
"John B" <sp******@hotmail.con> wrote in message
news:42**********@127.0.0.1...
I have a variable in a base class defined as:

protected Control validationControl;

How can I ensure that it is set is any dervied classes. I see that you
cannot set variables to abstract.

Thanks

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet
News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+
Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption
=----

Nov 16 '05 #3
John,

1) Make sure every constructor in your base class takes a Control as a
parameter. The derived classes must call one of them.

public abstract BaseClass
{
public BaseClass(Control validationControl)
{
}
}

public class DerivedClass : BaseClass
{
public DerivedClass()
: base(GetControlSomehow())
{
}

public DerivedClass(Control validationControl)
: base(validationControl)
{
}

public Control GetControlSomehow()
{
// Do whatever it is you need to do to return a control.
}
}

2) Create a protected property or method that must be implemented in
the derived classes that returns a reference to a Control.

public abstract BaseClass
{
protected abstract ValidationControl { get; }

public void DoSomething()
{
// This line gets a reference from the derived class.
Control control = ValidationControl;
}
}

public class DerivedClass : BaseClass
{
protected override ValidationControl
{
get
{
// Return a reference to a Control.
}
}
}

These are the most common ways I do it.

Brian

John B wrote:
I have a variable in a base class defined as:

protected Control validationControl;

How can I ensure that it is set is any dervied classes. I see that
you cannot set variables to abstract.

Thanks


Nov 16 '05 #4

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

Similar topics

7
by: Douglas Peterson | last post by:
Take a look at this code, it looks funny as its written to be as short as possible: -- code -- struct Base { ~Base() { *((char*)0) = 0; } }; struct Derived : public Base
6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
17
by: Krishnan | last post by:
Hi, Am having a base class that implements an interface (air code) : class BaseClass : SomeInterface { void SomeInterface.ImplMethod() { // -- code here -- } }
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.