471,354 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

How do you store a list of Objects ?

I'm a newbie..

I'm trying to list all files in a directory, and store them in a aFile
object. As I iterate through each file, I store the size, name, path, date
etc...

How do I store them once the object is created? I thought they would simply
be added to an ArrayList, but this doesn't seem to be working.

eg:

for each file {
myFile afile = new myFile();
afile.name=...
...
ArrayList.add (aFile);
}

But this seems to store the same object in all the array slots....

How should this be done?
Jan 5 '06 #1
10 1630
"Craig Lister" <My*********@thelisters.co.uk> wrote in message
news:43***********************@news.zen.co.uk...
I'm a newbie..

I'm trying to list all files in a directory, and store them in a aFile
object. As I iterate through each file, I store the size, name, path, date
etc...

How do I store them once the object is created? I thought they would
simply be added to an ArrayList, but this doesn't seem to be working.

eg:

for each file {
myFile afile = new myFile();
afile.name=...
...
ArrayList.add (aFile);
}

But this seems to store the same object in all the array slots....

How should this be done?


do

ArrayList a = new ArrayList();

then

a.Add(...);

You have to allocate a new ArrayList to store your object references. If you
are using v2.0 you may find

List<T> in System.Collections.Generic better

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Jan 5 '06 #2
Craig Lister wrote:
I'm a newbie..

I'm trying to list all files in a directory, and store them in a aFile
object. As I iterate through each file, I store the size, name, path, date
etc...

How do I store them once the object is created? I thought they would simply
be added to an ArrayList, but this doesn't seem to be working.

eg:

for each file {
myFile afile = new myFile();
afile.name=...
...
ArrayList.add (aFile);
}

But this seems to store the same object in all the array slots....

How should this be done?


Well, I suspect your code isn't quite like the above. (In fact, I'm
sure it's not.) My guess is that your code actually creates a new
"myFile" instance *once*, then sets its name multiple times.

However, instead of guessing, it would be best if you could post a
short but complete program that demonstrates the problem. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
this in more detail.

Also, if you could say whether you're using .NET 1.1 or 2.0 that would
help.

Jon

Jan 5 '06 #3
Thanks.
OK, I'm using VS 2005 Pro (2.0)

It does seem to work now... But .. is this the best way to do this?
Basically, I scan a directory for files of a certain type. I then scan a dir
on a server, and check that the files are all the same. If they are
different, I copy the new version to the server....

None of hat is done yet! I'm just trying to find the best way to start. My
idea is to create an object for each file on the server. (Maybe this isn't a
good plan, if we have 20,000 files?), and then use that to compare to the
actual file on the local machine. If the file is different, copy the local
to the server, update the object and continue...

Here's code:

private class aFile // The class that holds the files...
{

public string fileName = "";

public long fileSize = 0;

public DateTime fileDateTime = System.DateTime.Now;

}

ArrayList fileList; // Holds the Objects

private void getFileList(string theDir) // Function that populates the
objects...

{

int myNum = 0;

for (int i = 0; i < 10; i++)

{

myNum++;

aFile myFile = new aFile();

myFile.fileName = "Test.exe";

myFile.fileSize = myNum;

myFile.fileDateTime = System.DateTime.Now;

fileList.Add(myFile);

}

}

private void button1_Click(object sender, EventArgs e)

{
getFileList(edbxSource.Text);

for (int i = 0; i < fileList.Count - 1; i++)

{

MessageBox.Show((fileList[i] as aFile).fileSize.ToString());

}

}

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Craig Lister wrote:
I'm a newbie..

I'm trying to list all files in a directory, and store them in a aFile
object. As I iterate through each file, I store the size, name, path,
date
etc...

How do I store them once the object is created? I thought they would
simply
be added to an ArrayList, but this doesn't seem to be working.

eg:

for each file {
myFile afile = new myFile();
afile.name=...
...
ArrayList.add (aFile);
}

But this seems to store the same object in all the array slots....

How should this be done?


Well, I suspect your code isn't quite like the above. (In fact, I'm
sure it's not.) My guess is that your code actually creates a new
"myFile" instance *once*, then sets its name multiple times.

However, instead of guessing, it would be best if you could post a
short but complete program that demonstrates the problem. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
this in more detail.

Also, if you could say whether you're using .NET 1.1 or 2.0 that would
help.

Jon

Jan 5 '06 #4
Craig Lister wrote:
Thanks.
OK, I'm using VS 2005 Pro (2.0)

It does seem to work now... But .. is this the best way to do this?
Well, it's the best way to add an object (or rather, a reference) to a
list. As you're using .NET 2.0, you'd be better off using a List<aFile>
instead of an ArrayList, but they're very similar.

A few points though:

1) Does the fileList really need to be a member variable? I'd consider
making your getFileList method return an ArrayList (or List<aFile>)
instead.

2) You'll get a more consistent view of code if you follow the .NET
naming conventions. See http://tinyurl.com/2cun for more information.
Basically, I scan a directory for files of a certain type. I then scan a dir
on a server, and check that the files are all the same. If they are
different, I copy the new version to the server....

None of hat is done yet! I'm just trying to find the best way to start. My
idea is to create an object for each file on the server. (Maybe this isn't a
good plan, if we have 20,000 files?), and then use that to compare to the
actual file on the local machine. If the file is different, copy the local
to the server, update the object and continue...


It's possible that you want to start comparing files while you're still
generating the list of files - in a different thread. You might want to
try a single-threaded version first though, get that working and see
just how it performs.

(You don't want to do any of that heavy work in the UI thread though.
See
http://www.pobox.com/~skeet/csharp/t...winforms.shtml for more
about that.)

Jon

Jan 5 '06 #5
Thanks Jon. I'm going to try what you say below. As for the fileList being a
member variable.. your way sounds better. I can create, populate and return
the ArrayList (or List) from the getFileList method? Sounds better..

Also, will look at the naming conventions... Thanks again.

Craig
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Craig Lister wrote:
Thanks.
OK, I'm using VS 2005 Pro (2.0)

It does seem to work now... But .. is this the best way to do this?


Well, it's the best way to add an object (or rather, a reference) to a
list. As you're using .NET 2.0, you'd be better off using a List<aFile>
instead of an ArrayList, but they're very similar.

A few points though:

1) Does the fileList really need to be a member variable? I'd consider
making your getFileList method return an ArrayList (or List<aFile>)
instead.

2) You'll get a more consistent view of code if you follow the .NET
naming conventions. See http://tinyurl.com/2cun for more information.
Basically, I scan a directory for files of a certain type. I then scan a
dir
on a server, and check that the files are all the same. If they are
different, I copy the new version to the server....

None of hat is done yet! I'm just trying to find the best way to start.
My
idea is to create an object for each file on the server. (Maybe this
isn't a
good plan, if we have 20,000 files?), and then use that to compare to the
actual file on the local machine. If the file is different, copy the
local
to the server, update the object and continue...


It's possible that you want to start comparing files while you're still
generating the list of files - in a different thread. You might want to
try a single-threaded version first though, get that working and see
just how it performs.

(You don't want to do any of that heavy work in the UI thread though.
See
http://www.pobox.com/~skeet/csharp/t...winforms.shtml for more
about that.)

Jon

Jan 5 '06 #6
Craig Lister wrote:
Thanks Jon. I'm going to try what you say below. As for the fileList being a
member variable.. your way sounds better. I can create, populate and return
the ArrayList (or List) from the getFileList method? Sounds better..


Absolutely. And don't worry about the cost of returning what you might
think of as a "large object" - only a reference will be returned. It's
well worth getting to grips with the difference between reference types
and value types early on. The following pages *may* help (there are
others around, of course).

http://www.pobox.com/~skeet/csharp/memory.html
http://www.pobox.com/~skeet/csharp/parameters.html

Jon

Jan 5 '06 #7
Craig Lister wrote:
Here's code:

private class aFile // The class that holds the files...
{
...
}


Just as an aside, I don't know if the aFile class you posted was the
complete class, but you might consider using the Directory.GetFiles
method which returns an array of FileInfo objects. These objects
contain all the information for a file such as date and time,
attributes, length, etc. You may as well use these object rather than
"re-invent the wheel".

Jan 5 '06 #8
Thanks VERY much! Hadn't done the class yet, but was going to reinvent the
wheel... Thanks!

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Craig Lister wrote:
Here's code:

private class aFile // The class that holds the files...
{
...
}


Just as an aside, I don't know if the aFile class you posted was the
complete class, but you might consider using the Directory.GetFiles
method which returns an array of FileInfo objects. These objects
contain all the information for a file such as date and time,
attributes, length, etc. You may as well use these object rather than
"re-invent the wheel".

Jan 5 '06 #9
OK.. My new test function will return an ArrayList (Haven't looked at the
List thing yet). I'd just like to know, how do I call this new function?

Something like:

myNewArray = GetFiles();

Do I need to decalre myNewArray?

private ArrayList GetFiles()

{

ArrayList myArray = new ArrayList();

for (int i = 0; i < 10; i++)

{

ClassFiles myClass = new ClassFiles();

myClass.FileName = i.ToString();

myClass.FileSize = i;

myClass.FileDateTime = System.DateTime.Now;

myArray.Add(myClass);

}

return myArray;

}

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Craig Lister wrote:
Thanks Jon. I'm going to try what you say below. As for the fileList
being a
member variable.. your way sounds better. I can create, populate and
return
the ArrayList (or List) from the getFileList method? Sounds better..


Absolutely. And don't worry about the cost of returning what you might
think of as a "large object" - only a reference will be returned. It's
well worth getting to grips with the difference between reference types
and value types early on. The following pages *may* help (there are
others around, of course).

http://www.pobox.com/~skeet/csharp/memory.html
http://www.pobox.com/~skeet/csharp/parameters.html

Jon

Jan 6 '06 #10
Craig Lister <My*********@thelisters.co.uk> wrote:
OK.. My new test function will return an ArrayList (Haven't looked at the
List thing yet). I'd just like to know, how do I call this new function?

Something like:

myNewArray = GetFiles();

Do I need to decalre myNewArray?


Yes. For instance:

IList myNewArray = GetFiles();

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 6 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by dasod | last post: by
2 posts views Thread by Jared | last post: by
2 posts views Thread by martin | last post: by
reply views Thread by Dennis Bjorklund | last post: by
2 posts views Thread by Aussie Rules | last post: by
7 posts views Thread by Andy | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.