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

Extending templated classes for specific data types.

How do I extend a template class for a specific template data type? I am
trying to achieve something like....

public class SomeItem{}
public class SomeProcess < T> { }

and now the extension

public class SomeProcess<SomeItem>
{ public SomeMethod() { } // special code for just this combination }

--
Paul
Jan 13 '06 #1
7 1760
Paul,

You essentially have to derive from the generic type with your specific
parameter, like so:

public class NewClass : SomeProcess<SomeItem>
{
public void SomeMethod()
{
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paulustrious" <msdn_whoisat_paulcotter.com> wrote in message
news:5A**********************************@microsof t.com...
How do I extend a template class for a specific template data type? I am
trying to achieve something like....

public class SomeItem{}
public class SomeProcess < T> { }

and now the extension

public class SomeProcess<SomeItem>
{ public SomeMethod() { } // special code for just this combination }

--
Paul

Jan 13 '06 #2
Hello Nicholas

That is what I have been trying but it is causing me problems. If I have

public class NewClass : SomeProcess<SomeItem>
{
public void SomeMethod()
{
NewClass nc = new NewClass( ) ;
AddChild (nc); // routine in SomeProcess<T>
NewClass ncFirst = GetFirstChild( ) // ditto
}
}

This causes casting errors as I cannot cast from NewClass to
SomeProcess<SomeItem>.

In my office I have a brick wall with a number of impressions of my skull
and a worn circular path in the carpet.

--
Paul
"Nicholas Paldino [.NET/C# MVP]" wrote:
Paul,

You essentially have to derive from the generic type with your specific
parameter, like so:

public class NewClass : SomeProcess<SomeItem>
{
public void SomeMethod()
{
}
}

Jan 13 '06 #3
Paul,

What is the signature of the AddChild method that is inherited from
SomeProcess<T>?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paulustrious" <msdn_whoisat_paulcotter.com> wrote in message
news:8B**********************************@microsof t.com...
Hello Nicholas

That is what I have been trying but it is causing me problems. If I have

public class NewClass : SomeProcess<SomeItem>
{
public void SomeMethod()
{
NewClass nc = new NewClass( ) ;
AddChild (nc); // routine in SomeProcess<T>
NewClass ncFirst = GetFirstChild( ) // ditto
}
}

This causes casting errors as I cannot cast from NewClass to
SomeProcess<SomeItem>.

In my office I have a brick wall with a number of impressions of my skull
and a worn circular path in the carpet.

--
Paul
"Nicholas Paldino [.NET/C# MVP]" wrote:
Paul,

You essentially have to derive from the generic type with your
specific
parameter, like so:

public class NewClass : SomeProcess<SomeItem>
{
public void SomeMethod()
{
}
}

Jan 13 '06 #4
Paulustrious wrote:
Hello Nicholas

That is what I have been trying but it is causing me problems. If I have

public class NewClass : SomeProcess<SomeItem>
{
public void SomeMethod()
{
NewClass nc = new NewClass( ) ;
AddChild (nc); // routine in SomeProcess<T>
NewClass ncFirst = GetFirstChild( ) // ditto
}
}

This causes casting errors as I cannot cast from NewClass to
SomeProcess<SomeItem>.

In my office I have a brick wall with a number of impressions of my skull
and a worn circular path in the carpet.

If the AddChild() method has the signature AddChild(T t), it can't work,
as NewClass "is a" SomeProcess<SomeItem>, not a SomeItem.

As C# doesn't have template specialization as C++, the only way I know
is to provide a member function template (BTW, what's the C# wording for
that?)

class SomeItem { }
class SomeProcess<T>
{
void AddChild(T t) { }
public void SomeSpecialMethod<U>(U u) where U : SomeItem, T
{
AddChild(u);
}
}
HTH,
Andy
--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40@*NO*SPAM*gmx.net
Jan 13 '06 #5
Nicholas,

The reality is more like this....

public class SomeProcess<T>
{
public List <SomeProcess<T> > [] Children
public string/int/class-name SomeOtherStuffNeededInClass;
public void AddChild(SomeProcess<T> childSomeProcess)
{
Children.Add(childSomeProcess) // List.Add being called.
}
}
It may look a bit odd, and I am sorry to bore you with what I am doing. I am
reading a (almost) complete database into memory including tables, keys and
foreign keys and dynamically generating in-memory copies with the foreign
keys represented by Children. The mappings are:

Database ===> MyDataSetClass : System.DataSet
Table ===> MyTable : List<MyDataRow : DataRow>
Foreign Key ==> MyKey : List< MyDataRowParentToMyDataRowChildren>

The above is not entirely correct (compile-wise or semantically) , but you
probably get the idea.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Paul,

What is the signature of the AddChild method that is inherited from
SomeProcess<T>?

Jan 13 '06 #6
Thank you Andreas. You have added to the indentations in the wall. I tried
this...

class Program
{
static void Main(string [] args)
{
SomeProcess<SomeItem> ss = new SomeProcess<SomeItem>();
}
}
class SomeItem { }
class SomeProcess<T>
{
public List <SomeProcess<T>> Children = new List<SomeProcess<T>>() ;
void AddChild(SomeProcess<T> t) { Children.Add( t ); }
public void SomeSpecialMethod<U>(U u) where U :
SomeProcess<SomeItem> , T
{
Children.Add( (SomeProcess < SomeItem >) u );
}
}

But of course the Children.Add is causing the same old casting error. I can
see ways round it but only by being 'dirty'. (eg adding non-generalized
variables to my
SomeProcess class that are specially for <U> stuff. I am new to C# so my
answer to your question...
As C# doesn't have template specialization as C++, the only way I know
is to provide a member function template (BTW, what's the C# wording for
that?)


is ...duh?
--
Paul

email: "yourname" WhoIsAt paulcotter.com
Jan 13 '06 #7
Paulustrious wrote:
Thank you Andreas. You have added to the indentations in the wall. I tried
this...

class Program
{
static void Main(string [] args)
{
SomeProcess<SomeItem> ss = new SomeProcess<SomeItem>();
}
}
class SomeItem { }
class SomeProcess<T>
{
public List <SomeProcess<T>> Children = new List<SomeProcess<T>>() ;
void AddChild(SomeProcess<T> t) { Children.Add( t ); }
public void SomeSpecialMethod<U>(U u) where U :
SomeProcess<SomeItem> , T
{
Children.Add( (SomeProcess < SomeItem >) u );
}
}

But of course the Children.Add is causing the same old casting error. I can
see ways round it but only by being 'dirty'. (eg adding non-generalized
variables to my
SomeProcess class that are specially for <U> stuff. I am new to C# so my
answer to your question...


The AddChild(SomeProcess<T> t) signature is killing my solution :-(.
Thought it was AddChild(T t).
I then see no way to do that using pure generics, as they don't have
template specialization.

However switching behavior for special circumstances can be achieved in
a "classic" way using the strategy pattern and a factory. Here is a
little sketch of what I mean:

class SomeItem { }
interface SomeSpecialMethodExec<T>
{
void SomeSpecialMethod(SomeProcess<T> item);
}
class SomeSpecialMethodExecSomeItem : SomeSpecialMethodExec<SomeItem>
{
private List<SomeProcess<SomeItem>> l;
internal SomeSpecialMethodExecSomeItem(List<SomeProcess<Som eItem>> l)
{
this.l = l;
}
void
SomeSpecialMethodExec<SomeItem>.SomeSpecialMethod( SomeProcess<SomeItem>
item)
{// special for SomeItem.
l.Add(item);
}
}
class SomeSpecialMethodExecDefault<T> : SomeSpecialMethodExec<T>
{
private List<SomeProcess<T>> l;
internal SomeSpecialMethodExecDefault(List<SomeProcess<T>> l)
{
this.l = l;
}
void SomeSpecialMethodExec<T>.SomeSpecialMethod(SomePro cess<T> item)
{// normal execution
l.Add(item);
}
}
static class SomeSpecialMethodExecFactory
{
public static SomeSpecialMethodExec<T> Create<T>
(List<SomeProcess<T>> l)
{
if(typeof(SomeItem) == typeof(T))
return (SomeSpecialMethodExec<T>)new
SomeSpecialMethodExecSomeItem((List<SomeProcess<So meItem>>)(object)l);
return new SomeSpecialMethodExecDefault<T>(l);
}
}
class SomeProcess<T>
{
private List<SomeProcess<T>> Children = new List<SomeProcess<T>>();
private void AddChild(SomeProcess<T> t) { Children.Add(t); }
public void SomeSpecialMethod(SomeProcess<T> t)
{
// get method
SomeSpecialMethodExec<T> meth =
SomeSpecialMethodExecFactory.Create<T>(Children);
meth.SomeSpecialMethod(t);
}
}
BTW, It is usually more fun to get headache from a couple of beers that
from reorganizing some bricks ;-)

HTH,
Andy
--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40@*NO*SPAM*gmx.net
Jan 14 '06 #8

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

Similar topics

3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
1
by: Art | last post by:
This is partially an academic question, but I'm trying to understand templates better. I have a base class that uses template parameters to define the behavior of its class. I want to subclass this...
4
by: Carl Youngblood | last post by:
I imagine this subject has probably been brought up numerous times. Forgive me for bringing it up again. I was googling through old posts on this newsgroup about it and found a good suggestion on...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
2
by: Indiana Epilepsy and Child Neurology | last post by:
Before asking this questions I've spent literally _years_ reading (Meyer, Stroustrup, Holub), googling, asking more general design questions, and just plain thinking about it. I am truly unable to...
17
by: devmentee | last post by:
Hello, I am trying to create a map/dictionary where the type of key is known ie std::string, but the value could be of any built in type. ie. int, double etc. (something along the lines of...
12
by: Robert.Holic | last post by:
Hi All (first time caller, long time listener), I've stumbled across a problem that I have yet to figure out, although Im sure I'll kick myself when I figure it out. Here it is: I need to...
7
by: Maximus Decimus | last post by:
HI all, I am using python v2.5 and I am an amateur working on python. I am extending python for my research work and would like some help and guidance w.r.t this matter from you experienced...
2
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.