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 StringCustomFie ldRef GetStringField( string fieldID, CustomRecord
record)
{
if(record.custo mFieldList == null)
{
return null;
}
foreach(CustomF ieldRef fieldRef in record.customFi eldList)
{
if(fieldRef is StringCustomFie ldRef &&
((StringCustomF ieldRef)fieldRe f).internalId == fieldID)
{
return fieldRef as StringCustomFie ldRef;
}
}
return null;
}
</code>
I then tried to change it like so:
<code>
public TFieldType GetStringField< TFieldType>(str ing fieldID, CustomRecord
record)
{
if (record.customF ieldList == null)
{
return default(TFieldT ype);
}
foreach (CustomFieldRef fieldRef in record.customFi eldList)
{
if (fieldRef is TFieldType && ((TFieldType)fi eldRef).interna lId ==
fieldID)
{
return (TFieldType)fie ldRef;
}
}
return default(TFieldT ype);
}
</code>
The above code throws to compiler errors that have me stumped:
"Cannot convert type 'PMDOutAddin.co m.netsuite.webs ervices.CustomF ieldRef'
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 4 4220
On Jun 4, 3:41 pm, "sklett" <s...@s.comwrot e:
<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
Hi Steve,
My Question would be, how the types CustomFieldRef and StringCustomFie ldRef
(and other types, for wich there is an overload) relate to one another.
Does StringCustomFie ldRef 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>(str ing 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
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, StringCustomFie ldRef inherits from the abstract base
CustomFieldRef
Thank you for your suggestion,
Steve
"Christof Nordiek" <cn@nospam.dewr ote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
Hi Steve,
My Question would be, how the types CustomFieldRef and
StringCustomFie ldRef (and other types, for wich there is an overload)
relate to one another.
Does StringCustomFie ldRef 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>(str ing 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
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.co mwrote in message
news:11******** *************@g 4g2000hsf.googl egroups.com...
On Jun 4, 3:41 pm, "sklett" <s...@s.comwrot e:
<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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 types hold a reference to an
object as opposed to object data itself. So, when reference type parameter is
passed into a method, a copy of objects reference is passed in, so called
method can do whatever to "original" object and a caller will see...
|
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;
|
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 specific class (lets
say Car).
I understand that I can create a CarList without using Generic, but
when I add a derived class to the list, type conversion will happen,
and I will not be able to enforce type at child class level.
|
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.
|
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:
public abstract class Vehicle
{ ... }
public class Aircraft : Vehicle
{ ... }
| |
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 it. Therefore
I wanted to creat a class member which represents the pointer to the
array. Unfortunately I get the folowring compile error for the code
beneath:
error C3229: 'DataType *' : indirections on a generic type parameter
|
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
its methods as in the following example.
public class Class1 {
private object _myObject;
public Class1(ref object myObject) {
|
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 to support cloning:
Public Class RefClass
Public tcp As TcpClient
Public name As String
End Class
Public Class RefClassList
Inherits List(Of RefClass)
Implements ICloneable
|
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,
public class SomeList<T>
{
public SomeList(SomeList<TthisSomeList)
|
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 =
System.Windows.Forms.PictureBoxSizeMode.StretchImage;
now in the ProcessImageClick method I check to make sure the Tag
propety is not null and cast from type Object to PictureBoxSizeMode
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |