Thread: "Collection" of objects in C#

  1. #1
    Registered User
    Join Date
    May 2005

    "Collection" of objects in C#

    Hi there,

    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?



  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Cape Town
    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.

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    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.
    Collection<WhateverTypeYouWant> myCollection = new Collection<WhateverTypeYouWant>();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. most efficient way to write filestream objects?
    By darsunt in forum C++ Programming
    Replies: 3
    Last Post: 01-26-2009, 05:17 PM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM