Handling Group of similar objects in C# ?

  • Thread starter Thread starter ree
  • Start date Start date
R

ree

I am new to C#

in C++ you can use vectors to handle a unknown quantity of set of objects.

But in C# not sure what to use.

As I tried arraylists but they can hold a variety of objects. So when I try
to use functions that go through (eg. for loop) of group objects (eg. Cars)
and get their (attributes eg. for Class Car attribute Year of Make ) I am
having compilation errors.

Is their anything easier than arraylists for C# like vectors in C++. Is
there a similar equivalent to vectors in C#?

Or should I use create an enormous array even though it will have memory
issues?
 
ree said:
I am new to C#

in C++ you can use vectors to handle a unknown quantity of set of objects.

But in C# not sure what to use.

As I tried arraylists but they can hold a variety of objects. So when I try
to use functions that go through (eg. for loop) of group objects (eg. Cars)
and get their (attributes eg. for Class Car attribute Year of Make ) I am
having compilation errors.

Is their anything easier than arraylists for C# like vectors in C++. Is
there a similar equivalent to vectors in C#?

Or should I use create an enormous array even though it will have memory
issues?

In the future, there'll be generics. Currently, you have three options
(that I can think of off the top of my head):

1) Expose an ArrayList and document that only the relevant types of
objects should be added.

2) Don't expose the ArrayList itself - make a strongly-typed public
interface and make sure that you only put the right things in the list.

3) Derive from CollectionBase to create a strongly typed list. There
are tools around to automate this, although I haven't used them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top