473,785 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic casting problem ?

Can generic types be casted?

say Vector<Type^>^ to Vector<CodeType ^>^

Where CodeType is a derivative of Type

Is this possible ?

Or is there something quite static about generics?

I would like a pointer to Vector<Type^>^
and then dynamically instantiate it to a derivative of Type ???
i.e

Vector<Type^>^ mArray = gcnew Vector<CodeType ^>();

Or am i just expecting too much?

How would i get round this problem?

Thanks.

Mar 20 '06 #1
1 1001
"Herby" <pr********@gma il.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.com.. .
Can generic types be casted?

say Vector<Type^>^ to Vector<CodeType ^>^

Where CodeType is a derivative of Type

Is this possible ?

Or is there something quite static about generics?

I would like a pointer to Vector<Type^>^
and then dynamically instantiate it to a derivative of Type ???
i.e

Vector<Type^>^ mArray = gcnew Vector<CodeType ^>();

Or am i just expecting too much?

How would i get round this problem?
Basically, this is not at all typesafe. Collections are covariant on get
and contravariant on set. (unless I have my terminology backward).

Imagine the following pseudocode:

function HitMe(Vector<Ty pe^>^ hitArray) {
hitArray.Append (typeof(hitArra y));
}
That's legal, typeof(hitArray ) is a subtype of Type, and can be added to a
Vector of Type

Vector<Type^>^ mArray = gcnew Vector<CodeType ^>();
HitMe(mArray); // uh-oh, now mArray contains typeof(mArray), which is not a
subtype of CodeType.


Thanks.

Mar 23 '06 #2

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

Similar topics

2
1722
by: cylt | last post by:
Hi, I would like to have something like that : IList<IUser> list2 = list1 as IList<IUser>; where list1 is a generic collection of User ( IList list1<User>=new List<User>() ) and where User implement the interface IUser. But this did not work... I thought that as User implement IUser it was possible to cast the generic
3
2772
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some dead ends. I'm posting this to get some feedback on wether I'm going in the right direction, and at the same time hopefully save others from going through the process.
4
3243
by: Andrew Ducker | last post by:
I have a collection of classes descending from a single root class (let's call it RootClass). They all currently have a property of Logical, of type Logical. However they actually return a subclass of Logical (LogicalA, LogicalB). I'm currently casting them to the right type when I retrieve them, but obviously this isn't terribly 'nice'. I tried converting the class to be generic, and having a type parameter that affected the type of...
3
4998
by: Vincent Finn | last post by:
Hi, I am trying to write a generic function and it isn't behaving as expected. I want to avoid having to write custom convert functions for my enums so I want to convert to an int and cast to the enum, this wouldn't compile so I tried a few variations. The cast fails inside the generic function even though its valid. The code below is the simplest example of the problem I could think of, even casting int to int fails.
3
6004
by: =?Utf-8?B?TWlydHVs?= | last post by:
Hi I'm currently working with vbscripting through MSScriptControl. We have shared some of our objects that should be available for scripting. Some of the functions of these objects will return an interface to a class that must be cast to the appropriate higher level class to use all functionality. Thus I tried to implement a generic cast function for use from the vbscript
2
3520
by: JB | last post by:
Hi All, I'm pulling my hair over this and could really do with a bit of help. I'm using various different enums as bit fields and I'd like to create a set of generic Functions or Subs to manipulate the bitfields. For instance: <Flags()_ Enum ActionFlags As Byte
7
2040
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation: displayMsgDelegate = DisplayStatusMessage; implementation: public void DisplayStatusMessage(string statusMessage)
4
7848
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have written a generic method which does different things depending on the type of the parameter. I got it to work, but it seems really inelegant. Is there a better way to do this? In the code below, ColumnMap is a simple struct which basically has three properties, Header (a string), Index (an int), TypeOfData (which is a DataType which is a local eNum). _Mapping is a local List<ColumnMap>. public ColumnMap GetMap<T>(T value)
8
3154
by: MMAS | last post by:
Hey everyone -- Curious about some strange behaviour I'm seeing that seems to be related to my lack of understanding on how generics work in C#. Here's some simplified code (sorry for strange formatting) to show what my issue is. I have a generic abstract class here: public abstract class MyGenericAbstractClass<Twhere T : MyInterface
2
4186
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of types derived from that base type, or arrays or generic collections of instances of types derived from that base type. All is well until I come to the properties which are generic collections, I don't seem to be able to find an elegant way of...
0
9480
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,...
0
10329
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, 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...
1
10092
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,...
1
7500
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
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...

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.