473,511 Members | 16,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

oops questions c#

hi all,

I want a method in my base class to be overriden by all derived
classes.My base class cannot be a abstract class. Also I dont want to
explicitly implement an interface for all derived classes.
How do i do it?
Can i just have one abstract method in a regular class?

Thanks in advance!!
PS

Nov 28 '06 #1
14 6900

If you want the method to be overriden by all derived classes then the
class is by definition abstract (at least in oop terminology).

Why don't you want to mark it abstract and declare an abstract method?
There are probably some tricks you could employ to achieve what you
want like passing a delegate to a protected constructor or an
interface to a protected constructor, but I don't see any reason why
you'd want to jump through those hoops to avoid the "abstract" keyword
on your class when your class really is abstract.

Besides, having a class that is essentialy abstract by design but not
marked abstract would be confusing to maintain.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 28 Nov 2006 13:27:01 -0800, "parez" <ps*****@gmail.comwrote:
>hi all,

I want a method in my base class to be overriden by all derived
classes.My base class cannot be a abstract class. Also I dont want to
explicitly implement an interface for all derived classes.
How do i do it?
Can i just have one abstract method in a regular class?

Thanks in advance!!
PS
Nov 28 '06 #2
Well there is a function in the class the that uses an instance of
itself.

e.g if the class name is somthingCollection

there is a function defined (implenment) call getrandonsomethings(int
count)
which looks liek this
{

SomethingCollectin sc = new SomethingCollectin ();

sc.add( ) // add random values.

}

Samuel R. Neff wrote:
If you want the method to be overriden by all derived classes then the
class is by definition abstract (at least in oop terminology).

Why don't you want to mark it abstract and declare an abstract method?
There are probably some tricks you could employ to achieve what you
want like passing a delegate to a protected constructor or an
interface to a protected constructor, but I don't see any reason why
you'd want to jump through those hoops to avoid the "abstract" keyword
on your class when your class really is abstract.

Besides, having a class that is essentialy abstract by design but not
marked abstract would be confusing to maintain.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 28 Nov 2006 13:27:01 -0800, "parez" <ps*****@gmail.comwrote:
hi all,

I want a method in my base class to be overriden by all derived
classes.My base class cannot be a abstract class. Also I dont want to
explicitly implement an interface for all derived classes.
How do i do it?
Can i just have one abstract method in a regular class?

Thanks in advance!!
PS
Nov 28 '06 #3
parez wrote:
I want a method in my base class to be overriden by all derived
classes.My base class cannot be a abstract class. Also I dont want to
explicitly implement an interface for all derived classes.
How do i do it?
Can i just have one abstract method in a regular class?
What is the reason for not wanting an abstract base class?

Working on the assumption it is legit, a simple solution would be to mark
the method as virtual in the base class and have it throw a
NotImplementedException. The problem with this is you won't know until
runtime that you might have forgotten to override the method in a derived
class.
--
Tom Porterfield

Nov 28 '06 #4
Unfortunately you will have to use an abstract class to achieve that.
However, do not despair, there is another way.

You can provide a regular method in your base class and have it raise an
event that should be handled by your inheriting class. In the method
invocation, you can then check to see if the event has been subscribed to and
raise an error if it has not.

Hope that helps.

--
Good luck!

Shailen Sukul
Architect
(BSc MCTS, MCSD.Net MCSD MCAD)
Ashlen Consulting Service P/L
(http://www.ashlen.net.au)
"parez" wrote:
hi all,

I want a method in my base class to be overriden by all derived
classes.My base class cannot be a abstract class. Also I dont want to
explicitly implement an interface for all derived classes.
How do i do it?
Can i just have one abstract method in a regular class?

Thanks in advance!!
PS

Nov 28 '06 #5
How do you check to see if an event has been subscribed to?

Mike Ober.

"Shailen Sukul" <sh***@ashlen.net.auwrote in message
news:69**********************************@microsof t.com...
Unfortunately you will have to use an abstract class to achieve that.
However, do not despair, there is another way.

You can provide a regular method in your base class and have it raise an
event that should be handled by your inheriting class. In the method
invocation, you can then check to see if the event has been subscribed to
and
raise an error if it has not.

Hope that helps.

--
Good luck!

Shailen Sukul
Architect
(BSc MCTS, MCSD.Net MCSD MCAD)
Ashlen Consulting Service P/L
(http://www.ashlen.net.au)
"parez" wrote:
>hi all,

I want a method in my base class to be overriden by all derived
classes.My base class cannot be a abstract class. Also I dont want to
explicitly implement an interface for all derived classes.
How do i do it?
Can i just have one abstract method in a regular class?

Thanks in advance!!
PS


Nov 28 '06 #6
Thank you all for you ideas..

I will do the virtual method in the base class with exception..

PS
Michael D. Ober wrote:
How do you check to see if an event has been subscribed to?

Mike Ober.

"Shailen Sukul" <sh***@ashlen.net.auwrote in message
news:69**********************************@microsof t.com...
Unfortunately you will have to use an abstract class to achieve that.
However, do not despair, there is another way.

You can provide a regular method in your base class and have it raise an
event that should be handled by your inheriting class. In the method
invocation, you can then check to see if the event has been subscribed to
and
raise an error if it has not.

Hope that helps.

--
Good luck!

Shailen Sukul
Architect
(BSc MCTS, MCSD.Net MCSD MCAD)
Ashlen Consulting Service P/L
(http://www.ashlen.net.au)
"parez" wrote:
hi all,

I want a method in my base class to be overriden by all derived
classes.My base class cannot be a abstract class. Also I dont want to
explicitly implement an interface for all derived classes.
How do i do it?
Can i just have one abstract method in a regular class?

Thanks in advance!!
PS

Nov 28 '06 #7

But if the class has a method which must be implemented by a derived
class then you should never create an instance of that class. If you
need to create an instance that is the same type as the current class
use reflection:

SomethingCollection sc = Activator.CreateInstance(this.GetType());

But even that you'd be better off implementing as a virtual factory
method in case a derived class requires a parameterized constructor.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 28 Nov 2006 14:06:51 -0800, "parez" <ps*****@gmail.comwrote:
>Well there is a function in the class the that uses an instance of
itself.

e.g if the class name is somthingCollection

there is a function defined (implenment) call getrandonsomethings(int
count)
which looks liek this
{

SomethingCollectin sc = new SomethingCollectin ();

sc.add( ) // add random values.

}

Samuel R. Neff wrote:
>If you want the method to be overriden by all derived classes then the
class is by definition abstract (at least in oop terminology).

Why don't you want to mark it abstract and declare an abstract method?
There are probably some tricks you could employ to achieve what you
want like passing a delegate to a protected constructor or an
interface to a protected constructor, but I don't see any reason why
you'd want to jump through those hoops to avoid the "abstract" keyword
on your class when your class really is abstract.

Besides, having a class that is essentialy abstract by design but not
marked abstract would be confusing to maintain.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 28 Nov 2006 13:27:01 -0800, "parez" <ps*****@gmail.comwrote:
>hi all,

I want a method in my base class to be overriden by all derived
classes.My base class cannot be a abstract class. Also I dont want to
explicitly implement an interface for all derived classes.
How do i do it?
Can i just have one abstract method in a regular class?

Thanks in advance!!
PS
Nov 28 '06 #8
to check if an event has been subscribed to do:

if (eventName != null)
{
eventName(); // raise event
}
else
{
throw ApplicationException("Even has not been subscribed to");
}
--
Good luck!

Shailen Sukul
Architect
(BSc MCTS, MCSD.Net MCSD MCAD)
Ashlen Consulting Service P/L
(http://www.ashlen.net.au)
"Michael D. Ober" wrote:
How do you check to see if an event has been subscribed to?

Mike Ober.

"Shailen Sukul" <sh***@ashlen.net.auwrote in message
news:69**********************************@microsof t.com...
Unfortunately you will have to use an abstract class to achieve that.
However, do not despair, there is another way.

You can provide a regular method in your base class and have it raise an
event that should be handled by your inheriting class. In the method
invocation, you can then check to see if the event has been subscribed to
and
raise an error if it has not.

Hope that helps.

--
Good luck!

Shailen Sukul
Architect
(BSc MCTS, MCSD.Net MCSD MCAD)
Ashlen Consulting Service P/L
(http://www.ashlen.net.au)
"parez" wrote:
hi all,

I want a method in my base class to be overriden by all derived
classes.My base class cannot be a abstract class. Also I dont want to
explicitly implement an interface for all derived classes.
How do i do it?
Can i just have one abstract method in a regular class?

Thanks in advance!!
PS



Nov 28 '06 #9


Hi All,

I just realised that the method which should be must override is a
static method and i dont think i can have a static virtual method.
GetInstance is the name of the staitic method which should be overriden
by all base classes.

what do you think i should

Samuel R. Neff wrote:
But if the class has a method which must be implemented by a derived
class then you should never create an instance of that class. If you
need to create an instance that is the same type as the current class
use reflection:

SomethingCollection sc = Activator.CreateInstance(this.GetType());

But even that you'd be better off implementing as a virtual factory
method in case a derived class requires a parameterized constructor.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 28 Nov 2006 14:06:51 -0800, "parez" <ps*****@gmail.comwrote:
Well there is a function in the class the that uses an instance of
itself.

e.g if the class name is somthingCollection

there is a function defined (implenment) call getrandonsomethings(int
count)
which looks liek this
{

SomethingCollectin sc = new SomethingCollectin ();

sc.add( ) // add random values.

}

Samuel R. Neff wrote:
If you want the method to be overriden by all derived classes then the
class is by definition abstract (at least in oop terminology).

Why don't you want to mark it abstract and declare an abstract method?
There are probably some tricks you could employ to achieve what you
want like passing a delegate to a protected constructor or an
interface to a protected constructor, but I don't see any reason why
you'd want to jump through those hoops to avoid the "abstract" keyword
on your class when your class really is abstract.

Besides, having a class that is essentialy abstract by design but not
marked abstract would be confusing to maintain.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 28 Nov 2006 13:27:01 -0800, "parez" <ps*****@gmail.comwrote:

hi all,

I want a method in my base class to be overriden by all derived
classes.My base class cannot be a abstract class. Also I dont want to
explicitly implement an interface for all derived classes.
How do i do it?
Can i just have one abstract method in a regular class?

Thanks in advance!!
PS
Nov 28 '06 #10
Shailen Sukul <sh***@ashlen.net.auwrote:
to check if an event has been subscribed to do:

if (eventName != null)
{
eventName(); // raise event
}
else
{
throw ApplicationException("Even has not been subscribed to");
}
Note that that is not thread-safe. It's often not a problem for an
event not to be thread-safe, but if it is, you should use the pattern
shown at
http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 28 '06 #11
parez <ps*****@gmail.comwrote:
I just realised that the method which should be must override is a
static method and i dont think i can have a static virtual method.

GetInstance is the name of the staitic method which should be overriden
by all base classes.

what do you think i should
It sounds like you want two different types of class - one for the type
you're actually trying to create, and one to create instances - a
factory type.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 28 '06 #12
"parez" <ps*****@gmail.coma écrit dans le message de news:
11**********************@14g2000cws.googlegroups.c om...

| I just realised that the method which should be must override is a
| static method and i dont think i can have a static virtual method.
|
| GetInstance is the name of the staitic method which should be overriden
| by all base classes.
|
| what do you think i should

Write a metaclass with an instance method that calls the static method of
the appropriate sub-class using reflection.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Nov 28 '06 #13
if (myEvent == null) {
Console.writeLine("No one wants to listen to me!");
} else {
Console.writeLine("I have at least one subscriber!");
}
On 2006-11-28 16:29:22 -0600, "Michael D. Ober"
<obermd.@.alum.mit.edu.nospamsaid:
How do you check to see if an event has been subscribed to?

Mike Ober.

"Shailen Sukul" <sh***@ashlen.net.auwrote in message
news:69**********************************@microsof t.com...
>Unfortunately you will have to use an abstract class to achieve that.
However, do not despair, there is another way.

You can provide a regular method in your base class and have it raise an
event that should be handled by your inheriting class. In the method
invocation, you can then check to see if the event has been subscribed to and
raise an error if it has not.

Hope that helps.

Nov 29 '06 #14
Thanks.

Mike.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Shailen Sukul <sh***@ashlen.net.auwrote:
>to check if an event has been subscribed to do:

if (eventName != null)
{
eventName(); // raise event
}
else
{
throw ApplicationException("Even has not been subscribed to");
}

Note that that is not thread-safe. It's often not a problem for an
event not to be thread-safe, but if it is, you should use the pattern
shown at
http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 29 '06 #15

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

Similar topics

24
3545
by: matty | last post by:
Go away for a few days and you miss it all... A few opinions... Programming is a craft more than an art (software engineering, not black magic) and as such, is about writing code that works,...
4
3843
by: Vaishali | last post by:
How is Object Oriented Programming implemented in .NET?
3
5061
by: Robert Nolan | last post by:
This subject is probably simple to the readers, but I must be having one hell of a mental block cause I am beating my head against a wall. I need to create a simple folder tree that is about 3...
58
4615
by: Jeff_Relf | last post by:
Hi Tom, You showed: << private const string PHONE_LIST = "495.1000__424.1111___(206)564-5555_1.800.325.3333"; static void Main( string args ) { foreach (string phoneNumber in Regex.Split...
34
3156
by: Pmb | last post by:
Hi. I'm new to this group. I'm refreshing/learning C++ and am starting to learn Object Oriented Programming (OOP). In discussing this with people I came up short as to what the benefits of OOP are....
1
2444
by: dawn | last post by:
Hi, With the code I use to capture part of a form, an error shows up : External GDI+ error the code is the following private void button3_Click(object sender, System.EventArgs e) {
1
1934
by: priya | last post by:
hi.i made mistake while copying my code sorry friends.This is the one i tried .i got o/p for dynamic rows how should i h'v to incorporate "onChange"event in "setAttribute". following code is not...
2
3577
by: shrini | last post by:
Friends. I i am reading on pgp 5 oops. it is very interesting. But, in books i found only some chunks of code. I need a simple applicaion based on php5 oops. can you suggest me one?
1
1911
by: moreshwar | last post by:
Hi, I am working in software field from last 7 yrs basically on Turbo C, assembly. I recently switch over to .Net Programming. I want to understand first OOPS concept as i am totally unaware about...
1
1731
by: Atulit | last post by:
Can someone send me the oops questions asked on interview
0
7242
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
7353
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,...
1
5063
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...
0
4737
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...
0
3222
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...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
446
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...

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.