![]() |
| | #1 |
| Registered User Join Date: May 2005
Posts: 4
| "Collection" of objects in C# I am trying to convert a program from VB6 into C#.NET. The VB6 code uses a very usful thing called a "collection" where you can simply add an object to it without defining the collection type. It may be a really obvious answer, but is there a similar system in C#. I looked at ArrayList but the problem with that is I can't do myArray[x].objectMethod();" for example. I want to add the object of same type to an array but at the same time, be able to reference the attributes and methods of the objects directly. Any ideas? Thanks, Rich |
| richard_hooper is offline | |
| | #2 |
| Registered User Join Date: May 2002 Location: Cape Town
Posts: 777
| My guess is that the C# ArrayList indexer method returns an object. This then has to be casted to your particular type, then the method can be called. |
| The Dog is offline | |
| | #3 |
| Banned Join Date: Feb 2003 Location: Australia
Posts: 986
| The Dog is correct. You can create strongly typed collections (which just inherit from CollectionBase and and do all the casting). There are a number of collection generators out there. A google search will show you how. In C# 2.0 (released later this year) you can use generics to accomplish this. Code: Collection<WhateverTypeYouWant> myCollection = new Collection<WhateverTypeYouWant>(); |
| nickname_changed is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| most efficient way to write filestream objects? | darsunt | C++ Programming | 3 | 01-26-2009 05:17 PM |
| I believe I've found an official Answer to my Problem (Object Factories) | Shamino | Game Programming | 60 | 12-20-2005 11:36 PM |
| Question about cout an stack object? | joenching | C++ Programming | 8 | 05-08-2005 10:10 PM |
| chain of objects within pop framework help needed | Davey | C++ Programming | 0 | 04-15-2004 10:01 AM |
| array of objects? | *~*~*~* | C++ Programming | 4 | 05-31-2003 05:57 PM |