473,385 Members | 1,856 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,385 software developers and data experts.

how to add to an Array?

Hi,

This may sound simple, here is my problem.

I have a couple classes, Class1, Class2, Class3.

Class1 has a collection of Class2 and Class3. Right now those collections
are defined as ArrayLists. This is a problem because when you reference them
eg: Class1.Class2s[index] you just get an Object because this is an
ArrayList as opposed to an Array of Class2.

So this means I have to do ((Class2)Class1.Class2s[index]).Method which
really sux. Now, if I make Class2s an Array such as Class2[], there is no
Add method to extend the array collection?

What is a good method of having a collection which is extendable but of a
particular type?

thanks

-c
Nov 16 '05 #1
1 4756
You have three choices that I can think of offhand.

First option is to check out the System.Collections.Specialized namespace to
see if there's anything there that happens to fit your needs. There is for
instance a StringCollection class that is a wrapper around ArrayList that
provides what amounts to an ArrayList of strings, strongly typed. The
casting overhead is still there, but the implementation is hidden and you
don't have to worry about it in your own code.

Second option is to write a class that inerits from CollectionBase and do
the implementation for the type you want to store in the collection. This
gives you an Add() method and so forth as you'd expect.

Third option is to use VS 2005 beta or C# Express 2005 beta and use the
System.Collections.Generic namespace. For example, the Generic.List class
is a replacement for ArrayList:

List<int> list = new List<int>();
list.Add(27);
list.Add(243);
Console.WriteLine("Second element: {0}",list[1]);

It works just the same as ArrayList but is strongly typed, and there's no
casting, hidden or otherwise.

--Bob

"typeMisMatch" <ne**@ip80.com> wrote in message
news:OS**************@TK2MSFTNGP14.phx.gbl...
Hi,

This may sound simple, here is my problem.

I have a couple classes, Class1, Class2, Class3.

Class1 has a collection of Class2 and Class3. Right now those collections
are defined as ArrayLists. This is a problem because when you reference them eg: Class1.Class2s[index] you just get an Object because this is an
ArrayList as opposed to an Array of Class2.

So this means I have to do ((Class2)Class1.Class2s[index]).Method which
really sux. Now, if I make Class2s an Array such as Class2[], there is no
Add method to extend the array collection?

What is a good method of having a collection which is extendable but of a
particular type?

thanks

-c

Nov 16 '05 #2

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

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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...

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.