473,402 Members | 2,053 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,402 software developers and data experts.

class must only be used as a base class

I have a class, "BaseUserTable", that must be derived from to be used
correctly:

class BaseUserTable
{
public virtual string TableName
{
get { throw( new ApplicationException( "where's the TableName
property in the derived class??" )) ; }
}
}

( the idea is that BaseUserTable contains methods for managing the
UserTable, only an application that uses the class must supply the
actual table name and database connection string. )

How do I do this in c#/.net? I want derived classes to get a compile
error if they dont implement the required virtual methods and
properties.

I am curious if an interface can do the trick. I just dont want to have
to implement the interface in this base class.

thanks,

-Steve

Nov 17 '05 #1
4 1069
What you probably want to do is create an abstract class. Add the keyword
"abstract" before your class definition. This will mean that your class can
not be instantiated and only be derived.

http://msdn.microsoft.com/library/de...actclasses.asp
"Steve Richter" wrote:
I have a class, "BaseUserTable", that must be derived from to be used
correctly:

class BaseUserTable
{
public virtual string TableName
{
get { throw( new ApplicationException( "where's the TableName
property in the derived class??" )) ; }
}
}

( the idea is that BaseUserTable contains methods for managing the
UserTable, only an application that uses the class must supply the
actual table name and database connection string. )

How do I do this in c#/.net? I want derived classes to get a compile
error if they dont implement the required virtual methods and
properties.

I am curious if an interface can do the trick. I just dont want to have
to implement the interface in this base class.

thanks,

-Steve

Nov 17 '05 #2
You may want to make the base class abstract (methods that have to be
overriden as well)

abstract class Foo
{
public abstract void Do();
}

class Bar : Foo
{
public override void Do()
{
//do foo
}
}

"Steve Richter" <St************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I have a class, "BaseUserTable", that must be derived from to be used
correctly:

class BaseUserTable
{
public virtual string TableName
{
get { throw( new ApplicationException( "where's the TableName
property in the derived class??" )) ; }
}
}

( the idea is that BaseUserTable contains methods for managing the
UserTable, only an application that uses the class must supply the
actual table name and database connection string. )

How do I do this in c#/.net? I want derived classes to get a compile
error if they dont implement the required virtual methods and
properties.

I am curious if an interface can do the trick. I just dont want to have
to implement the interface in this base class.

thanks,

-Steve

Nov 17 '05 #3


rmacias wrote:
What you probably want to do is create an abstract class. Add the keyword
"abstract" before your class definition. This will mean that your class can
not be instantiated and only be derived.

http://msdn.microsoft.com/library/de...actclasses.asp

perfect!

thanks,

-Steve


"Steve Richter" wrote:
I have a class, "BaseUserTable", that must be derived from to be used
correctly:

class BaseUserTable
{
public virtual string TableName
{
get { throw( new ApplicationException( "where's the TableName
property in the derived class??" )) ; }
}
}

( the idea is that BaseUserTable contains methods for managing the
UserTable, only an application that uses the class must supply the
actual table name and database connection string. )

How do I do this in c#/.net? I want derived classes to get a compile
error if they dont implement the required virtual methods and
properties.

I am curious if an interface can do the trick. I just dont want to have
to implement the interface in this base class.

thanks,

-Steve


Nov 17 '05 #4
Hi,

Just declare the base class as abstract. Another way to avoid the creation
of a class is declaring the constructor as private , in this way you cannot
create an instance of it.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Steve Richter" <St************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I have a class, "BaseUserTable", that must be derived from to be used
correctly:

class BaseUserTable
{
public virtual string TableName
{
get { throw( new ApplicationException( "where's the TableName
property in the derived class??" )) ; }
}
}

( the idea is that BaseUserTable contains methods for managing the
UserTable, only an application that uses the class must supply the
actual table name and database connection string. )

How do I do this in c#/.net? I want derived classes to get a compile
error if they dont implement the required virtual methods and
properties.

I am curious if an interface can do the trick. I just dont want to have
to implement the interface in this base class.

thanks,

-Steve

Nov 17 '05 #5

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

Similar topics

6
by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A {
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
18
by: Bradley | last post by:
I'm trying to determine if there's a general rule for when an Interface should used vs. an Abstract Class. Is there any design advantage to using one or the other? Brad
9
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't...
11
by: anongroupaccount | last post by:
What measures should be taken to avoid this sort of thing? class Base { }; class Derived1 : public Base { private: int i, j, k;
9
by: olanglois | last post by:
Hi, I am not sure if I have found a compiler bug (I am using VC++.NET2003) or if this is the correct behavior defined by the language but I am sure someone can clear up my confusion. Suppose the...
5
by: The Cool Giraffe | last post by:
I'm designing an ABC and in connection to that i have run into some "huh!" and "oh...". Let me put it as a list. 1. Since the class will only contain bodies of the methods, only the header file...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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.