oops questions c#

P

parez

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
 
S

Samuel R. Neff

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
 
T

Tom Porterfield

parez said:
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.
 
P

parez

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.

}
 
G

Guest

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)
 
P

parez

Thank you all for you ideas..

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

Samuel R. Neff

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.
 
G

Guest

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)
 
P

parez

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
 
J

Joanna Carter [TeamB]

"parez" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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
 
J

Jon Skeet [C# MVP]

parez said:
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.
 
B

Bob Jones

if (myEvent == null) {
Console.writeLine("No one wants to listen to me!");
} else {
Console.writeLine("I have at least one subscriber!");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top