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

Runtime Type casting

Hi,

How can do runtime casting?

MyCollection derives from ArrayList

I will store lost of different objects that all derive from the same parent
class. I then want to be able to pass in the object type and collection type
I have a number of classes that derive from ArrayList and have if pick out
on ly those i asked for.

Problem is it wont let me cast using a type as a method argument.

//public method in instance of MyCollection
public MyCollection GetTypesFromCollection(Type ObjectType, Type
CollectionType)
{

MyCollection s;
(CollectionType)s = new MyCollection(); //create collection and cast to
runtime collection type

foreach(object o in this) //loop through all entries in this arraylist
{
if(o.GetType() == ObjectType.GetType()) //if this object type is the
same as the type I'm asking for
{
s.Add( (ObjectType) o); //add it to the tryped arraylist
}
}
return s; //give it all back
}

Help!!
Nov 16 '05 #1
2 5467
Try this:

public MyCollection GetTypesFromCollection(Type ObjectType, Type
CollectionType)
{

MyCollection s = CollectionType.GetConstructor(null).Invoke() as
MyCollection; //create collection via reflection

foreach(object o in this) //loop through all entries in this arraylist
{
if(o.GetType() == ObjectType) //if this object type is the same as
the type I'm asking for
{
s.Add(o); //add it to the tryped arraylist
}
}
return s; //give it all back
}
"MattC" <m@m.com> wrote in message
news:eH**************@TK2MSFTNGP10.phx.gbl...
Hi,

How can do runtime casting?

MyCollection derives from ArrayList

I will store lost of different objects that all derive from the same parent class. I then want to be able to pass in the object type and collection type I have a number of classes that derive from ArrayList and have if pick out
on ly those i asked for.

Problem is it wont let me cast using a type as a method argument.

//public method in instance of MyCollection
public MyCollection GetTypesFromCollection(Type ObjectType, Type
CollectionType)
{

MyCollection s;
(CollectionType)s = new MyCollection(); //create collection and cast to runtime collection type

foreach(object o in this) //loop through all entries in this arraylist
{
if(o.GetType() == ObjectType.GetType()) //if this object type is the same as the type I'm asking for
{
s.Add( (ObjectType) o); //add it to the tryped arraylist
}
}
return s; //give it all back
}

Help!!

Nov 16 '05 #2
This is handled much more elegantly in C# 2.0 by using generics.

Until then, I suggest that you DON'T do this. The collection will still
contain strongly typed objects. Simply have the collection return the
object that is in it and, if you are concerned that the object isn't of the
right type, check it.

You can solve most of these problems using good patterns, without having to
force the collection type to a particular class.

I hope this helps,
--- Nick

"MattC" <m@m.com> wrote in message
news:eH**************@TK2MSFTNGP10.phx.gbl...
Hi,

How can do runtime casting?

MyCollection derives from ArrayList

I will store lost of different objects that all derive from the same parent class. I then want to be able to pass in the object type and collection type I have a number of classes that derive from ArrayList and have if pick out
on ly those i asked for.

Problem is it wont let me cast using a type as a method argument.

//public method in instance of MyCollection
public MyCollection GetTypesFromCollection(Type ObjectType, Type
CollectionType)
{

MyCollection s;
(CollectionType)s = new MyCollection(); //create collection and cast to runtime collection type

foreach(object o in this) //loop through all entries in this arraylist
{
if(o.GetType() == ObjectType.GetType()) //if this object type is the same as the type I'm asking for
{
s.Add( (ObjectType) o); //add it to the tryped arraylist
}
}
return s; //give it all back
}

Help!!

Nov 16 '05 #3

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

Similar topics

2
by: Dave | last post by:
Hello all, I am creating a linked list implementation which will be used in a number of contexts. As a result, I am defining its value node as type (void *). I hope to pass something in to its...
4
by: AdamM | last post by:
How can I change an object's type at runtime? For example, here's what I want to do in psedocode: object animal; if (dog) { animal=(dog)animal;
22
by: José Teixeira Junior | last post by:
Hi, How i can add one new unknown control at runtime in my form1? Thaks.
7
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
10
by: Rich | last post by:
I want to replace CSomeObject class with some kind of runtime method that returns type CSomeObject that I can use as cast. How do I specify type of explicit cast at runtime? eg: object...
2
by: eric.dennison | last post by:
In the sample below: testClass is derived from object. We can cast object to testClass, no problem We can cast testClass to object no problem Compiler is ok with cast object to testClass but...
13
by: DaTurk | last post by:
Hi, This is a question brought about by a solution I came up with to another question I had, which was "Dynamic object creation". So, I'm curious if you can dynamically cast an object. If you...
42
by: blisspikle | last post by:
I tried closely copying some code that I found on this group for assigning a type at runtime, but I cannot get it to work. Can someone see what is wrong with my logic? Thanks, Private Sub...
16
by: desktop | last post by:
I have read that using templates makes types know at compile time and using inheritance the types are first decided at runtime. The use of pointers and casts also indicates that the types will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.