Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Looping through an array

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John - 11 Mar 2008 15:36 GMT
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
Jon Skeet [C# MVP] - 11 Mar 2008 15:53 GMT
> 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
[quoted text clipped - 18 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

John - 11 Mar 2008 16:05 GMT
Thanks Jon,

This ArrayList looks like exactly what I need to use.

> 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.
Jon Skeet [C# MVP] - 11 Mar 2008 16:16 GMT
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Marc Gravell - 11 Mar 2008 16:17 GMT
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
Jon Skeet [C# MVP] - 11 Mar 2008 16:24 GMT
> 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...

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Marc Gravell - 11 Mar 2008 16:45 GMT
> 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
Michael Nemtsev [MVP] - 11 Mar 2008 22:19 GMT
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
John - 11 Mar 2008 17:02 GMT
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.

> 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
Marc Gravell - 11 Mar 2008 15:56 GMT
> What is my best option to do this?

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

Marc
Lasse Vågsæther Karlsen - 11 Mar 2008 17:56 GMT
> 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
[quoted text clipped - 20 lines]
>
> 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> :)

Signature

Lasse Vågsæther Karlsen
mailto:lasse@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3

John - 11 Mar 2008 18:24 GMT
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.

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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.