PC Review


Reply
Thread Tools Rate Thread

Base class referring to inheritor

 
 
Ertugrul Söylemez
Guest
Posts: n/a
 
      14th May 2009
Hello people,

I would like to write a base class in the following way:

abstract class Something<T> {
// ...
public abstract void unpack(Something<Something<T>>);
// ...
}

An inheriting class should define the 'unpack' method such that
instances of Something<T> can become whatever is in a
Something<Something<T>> in some way that makes sense.

Example: Where 'xs' is a list of type MyList<T> and and 'xss' is a list
of lists of type MyList<MyList<T>>, 'xs.unpack(xss)' replaces the list
contained by 'xs' with the concatenation of all lists contained by 'xss'
such that:

xs = someList
xss = {{10,11,12}, {14,17}, {19,20,22}}

// After xs.unpack(xss):
xs = {10,11,12,14,17,19,20,22}
xss = {{10,11,12}, {14,17}, {19,20,22}}

The problem is this: The MyList<T>.unpack method can be called with any
Something<Something<T>>, so I have to use explicit casting, which is
very ugly and not type-safe:

class MyList<T> : Something<T> {
// ...
public override void unpack(Something<Something<T>> cc) {
MyList<MyList<T>> xss = cc as MyList<MyList<T>>;

// ...
}
// ...
}

Is there an elegant way to solve this? There doesn't seem to be a way
to refer to more special classes from a base class.

Interfaces don't solve this either, because I can't refer to the
particular type implemented by the interface.

Any ideas?


Greets,
Ertugrul.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Base class referring to inheritor Ertugrul Söylemez Microsoft C# .NET 14 15th May 2009 08:24 PM
how to make sure this.Test() call base class method in base class constructor Ryan Liu Microsoft C# .NET 2 27th Aug 2006 07:43 PM
Getting a reference to the base class of a base class from an inherited class Edward Forgacs Microsoft C# .NET 1 24th Apr 2004 11:06 AM
variable type that can be declared in a base class this is accessible only to classes that inherit the base class Dan Microsoft ASP .NET 1 24th Mar 2004 09:34 PM
How to call a base class's base class method Bill Menees Microsoft C# .NET 1 23rd Jul 2003 02:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:10 AM.