Generics question

  • Thread starter Thread starter Greg Bacchus
  • Start date Start date
G

Greg Bacchus

Hi
I have a question that is difficult to explain so I'll use code
snippets to help me...
I have my object:

public abstract class MyTemplate<TKey>
{
public <TKey> Key;
}

public class MyObject : MyTemplate<int>
{
public string Code;
}

and I want to make a data access class that will return a specific one
of my objects given it's key. Something like this...

public class DataAccess<TObject> where TObject: MyTemplate
{
public <TObject> GetObject( <TKey> key )
{
// TODO: do stuff here;
return null;
}
}

My question is: given type TObject, is there any way of automatically
getting TKey without having to specify it in the declaration of
DataAccess??

Cheers
Greg Bacchus
 
Greg Bacchus said:
Hi
I have a question that is difficult to explain so I'll use code
snippets to help me...
I have my object:

public abstract class MyTemplate<TKey>
{
public <TKey> Key;
}

public class MyObject : MyTemplate<int>
{
public string Code;
}

and I want to make a data access class that will return a specific one
of my objects given it's key. Something like this...

public class DataAccess<TObject> where TObject: MyTemplate
{
public <TObject> GetObject( <TKey> key )
{
// TODO: do stuff here;
return null;
}
}

My question is: given type TObject, is there any way of automatically
getting TKey without having to specify it in the declaration of
DataAccess??

No. Your code here won't even work since you hae not defined a MyTemplate,
just a MyTemplate<T>.

The closest you could come would be

public class DataAccess<TObject, TKey> where TObject : MyTemplate<TKey>
{

}

if I understand what you mean, anyway.
 

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

Back
Top