473,320 Members | 1,817 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,320 software developers and data experts.

newb generic ?: How to cast reference to type parameter

I'm revisiting an old series of overloaded methods I have and I would like
to convert them to a single generic method. Here is the method in it's
current, overloaded implementation:
<code>
public StringCustomFieldRef GetStringField(string fieldID, CustomRecord
record)
{
if(record.customFieldList == null)
{
return null;
}

foreach(CustomFieldRef fieldRef in record.customFieldList)
{
if(fieldRef is StringCustomFieldRef &&
((StringCustomFieldRef)fieldRef).internalId == fieldID)
{
return fieldRef as StringCustomFieldRef;
}
}

return null;
}
</code>

I then tried to change it like so:
<code>
public TFieldType GetStringField<TFieldType>(string fieldID, CustomRecord
record)
{
if (record.customFieldList == null)
{
return default(TFieldType);
}

foreach (CustomFieldRef fieldRef in record.customFieldList)
{
if (fieldRef is TFieldType && ((TFieldType)fieldRef).internalId ==
fieldID)
{
return (TFieldType)fieldRef;
}
}

return default(TFieldType);
}
</code>

The above code throws to compiler errors that have me stumped:
"Cannot convert type 'PMDOutAddin.com.netsuite.webservices.CustomFieldR ef'
to 'TFieldType'"

It could be that what I'm trying to do isn't possible, like I said, I'm very
new to generics. I've seen generic classes before that have something like
" : where ClassName" after them, it looks like it's a base class of
interface requirement. Maybe something like this on a method would be
needed?

Any suggestions welcome.
Thanks for reading,
Steve
Jun 4 '07 #1
4 4209
On Jun 4, 3:41 pm, "sklett" <s...@s.comwrote:

<snip>
It could be that what I'm trying to do isn't possible, like I said, I'm very
new to generics. I've seen generic classes before that have something like
" : where ClassName" after them, it looks like it's a base class of
interface requirement. Maybe something like this on a method would be
needed?
You'll need some kind of constraint so that the internalId member is
available. I'm not immediately sure why it's complaining in quite the
way it is though - could you come up with a short but complete program
that demonstrates the problem, and we can take it from there?

See http://pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Jun 4 '07 #2
Hi Steve,

My Question would be, how the types CustomFieldRef and StringCustomFieldRef
(and other types, for wich there is an overload) relate to one another.
Does StringCustomFieldRef derive from CustomFieldRef (and do the other types
also)?
If so, a first step could be to add a type constraint.

public TFieldType GetStringField<TFieldType>(string fieldID, CustomRecord
record) where TFieldType : CustomFieldRef
{
...
}

This ensures, that TFieldType is a class deriving from CustomFieldRef and
the cast is possible.

Then still remains the problem of internalId. This property (or field?)
seems be defined in the deriving classes. If you can change the definition
of that classes, you should try to move it into the common base class.
Atleast there should be an abstract or virtual declaration of it, and maybe
overrides in the derived classes.
If that's not possible, you would be bound to use reflection, but that vere
likely will be worse, than having seperate declarations for each type.

HTH
Christof
Jun 4 '07 #3
Hi Christof,

The constraint is what I was looking for, thanks. I'm not able to change
the implementation of CustomFieldRef, but I can suggest the modification to
the developer(s) of the Web Service that it is generated from.

You are correct, StringCustomFieldRef inherits from the abstract base
CustomFieldRef

Thank you for your suggestion,
Steve
"Christof Nordiek" <cn@nospam.dewrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Hi Steve,

My Question would be, how the types CustomFieldRef and
StringCustomFieldRef (and other types, for wich there is an overload)
relate to one another.
Does StringCustomFieldRef derive from CustomFieldRef (and do the other
types also)?
If so, a first step could be to add a type constraint.

public TFieldType GetStringField<TFieldType>(string fieldID, CustomRecord
record) where TFieldType : CustomFieldRef
{
...
}

This ensures, that TFieldType is a class deriving from CustomFieldRef and
the cast is possible.

Then still remains the problem of internalId. This property (or field?)
seems be defined in the deriving classes. If you can change the definition
of that classes, you should try to move it into the common base class.
Atleast there should be an abstract or virtual declaration of it, and
maybe overrides in the derived classes.
If that's not possible, you would be bound to use reflection, but that
vere likely will be worse, than having seperate declarations for each
type.

HTH
Christof


Jun 4 '07 #4
Thank you for the reply Jon,

where TType : Base/Interface is what I was looking for. Learn something
every day :)

Take care,
Steve
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11*********************@g4g2000hsf.googlegrou ps.com...
On Jun 4, 3:41 pm, "sklett" <s...@s.comwrote:

<snip>
>It could be that what I'm trying to do isn't possible, like I said, I'm
very
new to generics. I've seen generic classes before that have something
like
" : where ClassName" after them, it looks like it's a base class of
interface requirement. Maybe something like this on a method would be
needed?

You'll need some kind of constraint so that the internalId member is
available. I'm not immediately sure why it's complaining in quite the
way it is though - could you come up with a short but complete program
that demonstrates the problem, and we can take it from there?

See http://pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Jun 4 '07 #5

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

Similar topics

6
by: Lenn | last post by:
Hi, Could someone clarify my confusion regarding passing reference types to a method with ref keyword and explain when it's practical to use it. It's my understanding that in .NET reference...
13
by: ahaupt | last post by:
Hi all, I'm implementing the Clone() method through the ICloneable interface and don't quite know how deep I need to go for a deep copy. Example: class A: ICloneable { object _val;
1
by: Jinsong Liu | last post by:
Hello Group: I am playing with .NET 2.0 Generic. I have a CarsList<T> Generic class, Is it possible that I can control what T could be? I want to ensure the T can only be classes derived from a...
9
by: Edward Diener | last post by:
Can one use 'ref' ( or 'out' ) on a reference type to create a reference to a reference in C#. I know one can use it on a value type to create a reference to that value.
1
by: Néstor Sánchez A. | last post by:
Hi, is there a way, maybe using reflection, to use a generic class passing the type parameter dynamicly (not kwnowing the exact type at compile time)? I tried the next example, but doesn't work: ...
1
by: interX | last post by:
Hi I'm new in VC++ and have a question to generics. I have a generic class, which contains an array of the generic type. This array I can pin and then I would like to get an unmanaged pointer to...
3
by: jbaldi | last post by:
I am trying to pass an object as a reference parameter in a constructor. I would like my class with the reference parameter constructor to be able to change the value of that object from one of...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
9
by: tadmill | last post by:
Is it possible to pass a generic parameter of the same class to to its constructor, where the "T" type passed in the constructor is different than the "T" type of the instanced class? ie, ...
6
by: GiJeet | last post by:
Problem using AS operator to cast Tag property Hello, I’m using the Tag property of a menu item to hold an enum value of a PictureBoxSizeMode. Eg: this.menuImageStretch.Tag =...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.