Looping through an array

J

John

I have an array with 5 items, when I retrieve the 5 items
I need to add (+1) one more item to the array to recreate it to
make a new array = 6. But as I am looping through the array
the program will crash because number 6 in the array does
not exist.

How can I loop through the array for the total items (5), but
still continue on for a loop of six? Because 6 is where I add
new informatiom into the array.

i.e

myClass[] mClass = new myClass[5];
int reccount = myClass.NumRecords;
for (int x = 0; x < reccount + 1; ++x)
{
mClass[x] = new myClass();
mClass[x].Name = chkRec[x].Name;
}

After it gets to 5 I need to add another record into the array
to make it six records. What is my best option to do this?


TIA
 
J

Jon Skeet [C# MVP]

John said:
I have an array with 5 items, when I retrieve the 5 items
I need to add (+1) one more item to the array to recreate it to
make a new array = 6. But as I am looping through the array
the program will crash because number 6 in the array does
not exist.

How can I loop through the array for the total items (5), but
still continue on for a loop of six? Because 6 is where I add
new informatiom into the array.

i.e

myClass[] mClass = new myClass[5];
int reccount = myClass.NumRecords;
for (int x = 0; x < reccount + 1; ++x)
{
mClass[x] = new myClass();
mClass[x].Name = chkRec[x].Name;
}

After it gets to 5 I need to add another record into the array
to make it six records. What is my best option to do this?

You can't add to an array - arrays are of a fixed size.

If you need a dynamically resizable collection, look at List<T> or
ArrayList.
 
M

Marc Gravell

What is my best option to do this?

Use a List<myClass>, and Add() each one in turn.

Marc
 
J

Jon Skeet [C# MVP]

John said:
This ArrayList looks like exactly what I need to use.

If you're using .NET 2.0, List<T> would almost certainly be a better
idea. Generics take a bit of getting used to, but they're very handy.
 
M

Marc Gravell

Note that although ArrayList is perfectly valid in .NET 1.1, it is not
an ideal choice if you are using .NET 2.0 or above (where List<T>
would be far preferable). At the minimum it will save you from a lot
of casting. More likely it will save a good number of daft bugs.

Marc
 
J

Jon Skeet [C# MVP]

Marc Gravell said:
Note that although ArrayList is perfectly valid in .NET 1.1, it is not
an ideal choice if you are using .NET 2.0 or above (where List<T>
would be far preferable). At the minimum it will save you from a lot
of casting. More likely it will save a good number of daft bugs.

I'm not sure about actually quashing bugs - not in and of itself.

How many times have you *actually* had an InvalidCastException when
fetching something from an ArrayList? It's more of a theoretical
problem than a real one, IMO.

However, it does make the code less readable and less self-documenting
- and *that* could cause bugs.


Another reason for using List<T> instead of ArrayList: the non-generic
collections aren't going to be available in Silverlight 2.0. Unlikely
to affect the OP, admittedly...
 
M

Marc Gravell

How many times have you *actually* had an InvalidCastException...
Not very many at all. But I do recall one such (back in the day) where
a Sort was barfing horribly; it was a really stupid and pointless bug,
and took 2 seconds to fix (IIRC it was a list being needlessly re-used
for different contents, and one code-path failing to Clear() it
first), but it took quite a bit longer to *find* the underlying cause.
Just the mentality of "it's object, I can use anything" breeds
unnecessary potential for error.
Another reason for using List<T> instead of ArrayList: the non-generic
collections aren't going to be available in Silverlight 2.0.

Yeah - I heard they had to be pretty brutal about what got included...
same between XmlDocument and XDocument IIRC... (from passing
conversations - I haven't looked myself).

Marc
 
J

John

I had looked at both after I posted the response, I quickly
realized that I need to use List<T> and not arrayList.
According to the description I can access the list by
index, insert to and delete from the list. That is just
what I need.

I am searching for some good tutorials on the topic now.
 
L

Lasse Vågsæther Karlsen

John said:
I have an array with 5 items, when I retrieve the 5 items
I need to add (+1) one more item to the array to recreate it to
make a new array = 6. But as I am looping through the array
the program will crash because number 6 in the array does
not exist.

How can I loop through the array for the total items (5), but
still continue on for a loop of six? Because 6 is where I add
new informatiom into the array.

i.e

myClass[] mClass = new myClass[5];
int reccount = myClass.NumRecords;
for (int x = 0; x < reccount + 1; ++x)
{
mClass[x] = new myClass();
mClass[x].Name = chkRec[x].Name;
}

After it gets to 5 I need to add another record into the array
to make it six records. What is my best option to do this?


TIA

As an alternative to the posts by Jon and Marc, if you know that you
need to place 6 elements into the array, the smallest change would of
course be to allocate the array with 6 elements, and not 5.

A simple re-arranging of the lines, and some minor changes would lead to
this:

int reccount = myClass.NumRecords;
myClass[] mClass = new myClass[reccount + 1];
for (int x = 0; x < reccount + 1; ++x)
{
mClass[x] = new myClass();
mClass[x].Name = chkRec[x].Name;
}

*However*, you should still use List<T> :)
 
J

John

Lasse,

Actually that is what I was going to do until the other option
were laid out on the table for me. I am trying to use list right
now. I have run into a few issues so far.

//This is my class:
public class Person
{
public string name;
public string address;
public string phone;
public Person(string name, string address, string phone)
{
this.name = name;
this.address = address;
this.phone = phone;
}
}

// apply info to the list and assign it to the nodes tag property
private void button4_Click(object sender, EventArgs e)
{
if (treeView1.SelectedNode == null)
return;

List<Person> people = new List<Person>();
people.Add(new Person(textBox1.Text, textBox2.Text,
textBox3.Text));
treeView1.SelectedNode.Tag = people;
}

After I am going through the treeview, just clicking here and there.
How do I get the information back out? I want to display it in a listview
but I am not gettting anything so far.

private void treeView1_Click(object sender, EventArgs e)
{
SelectedNode = treeView1.SelectedNode;

if (treeView1.SelectedNode == null || treeView1.SelectedNode.Tag
== null)
{
listView1.BeginUpdate();
listView1.Items.Clear();
listView1.EndUpdate();
return;
}

listView1.BeginUpdate();
listView1.Items.Clear();
List<Person> people = new List<Person>();
people.ForEach(delegate(Person p)
{
ListViewItem item1 = new ListViewItem(p.name, 0);
item1.SubItems.Add(p.address);
item1.SubItems.Add(p.phone);
listView1.Items.Add(item1);
});
listView1.EndUpdate();
}

I am just experimenting so far, eventually I have to figure out how to add
more items to the list,
delete items from the list etc.

Appreciate it.
 
M

Michael Nemtsev [MVP]

Hello Jon Skeet [C# MVP],

J> Another reason for using List<T> instead of ArrayList: the
J> non-generic collections aren't going to be available in Silverlight
J> 2.0. Unlikely to affect the OP, admittedly...

it's not actually only Silverlight 2.0.
MS announced that they won't include non-generic to the new developed libraries
and technologies.

so, I'd say if u are not using generics today, tomorrow u gonna port all
your apps to generics

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
 

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

Top