Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I pass an array to an ArrayList

Status
Not open for further replies.

egil84

Programmer
Oct 19, 2003
13
0
0
I have an array where I have loaded all the data I need, and then I'm trying to pass that information to go into an ArrayList so I serialize it. I've tried to pass an ArrayList to an ArrayList but it won't work. I have to pass the array through a method and then that mothod will serialize my ArrayList. How do i do this. Please help, I'm a begginer.
 
To answer your question we could really use some more information/code examples, like what kind of objects are in the array, what *exactly* do you want etc.

regards,
Blaxo
 
I have this array in my form:

Code:
//load values to array
string [] dataSetArray = {variable1, variable2, variable3};

//pass array to method Add
oDataStore.Add(dataSetArray);//oDataStore is the object instantiated out of DataStore

//here is my DataStore Namespace

using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using IPropertyNamespace //this is where I have all my Get and Set Methods

namespace DataStoreNamespace
{
[Serializable()]
class DataStore
{
public ArrayList propertyList;

public DataStore
{
  try
  {
IFormatter formatter = new BinatyFormatter();
Stream stream = new FileStrem("Properties.bin", FileMode.Open)
propertyList = (ArrayList)formatter.Deserialize(stream);
strema.Close();
  }
  catch(Exception)
  {
IFormatter formatter = new BinatyFormatter();
Stream stream = new FileStrem("Properties.bin", FileMode.Create);
formatter.Serialize(stream, propertyList)
stream.Close();
  }
}

public void Add(string [] newProperty)
{
  try
  {
  propertyList = newproperty;
  }
  catch(Exception err)
  {
  MessageBox.Show(err.Message);
  }
}
}

I know my code is totally wrong. I'm trying to do this:
I want to have all the information about a cruise loaded into an array, and then pass it to an ArrayList where I'll have the information of that cruise along with other cruises already created.
Please help.
 
ArrayList knows how to work with any ICollection. Arrays don't show all of their interface based methods (to simplify working with them with intellisense in the IDE) but they support all of the collection oriented interfaces that make sense for arrays. ICollection is supported by all arrays.

You can pass the array directly into the collection at create time, like this:

int[] ia = {1,2,3,4,5};
ArrayList al = new ArrayList(ia);

or you can add the elements to an existing ArrayList

al.AddRange(ia); // int array from above, or any array





 
Oh my God, thank you very much guys. It worked pretty well!!![thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top