How do you store a list of Objects ?

  • Thread starter Thread starter Craig Lister
  • Start date Start date
C

Craig Lister

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?
 
Craig Lister said:
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
 
Craig said:
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
 
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 as aFile).fileSize.ToString());

}

}
 
Craig said:
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/threads/winforms.shtml for more
about that.)

Jon
 
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
 
Craig said:
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
 
Craig said:
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".
 
Thanks VERY much! Hadn't done the class yet, but was going to reinvent the
wheel... Thanks!
 
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;

}
 
Craig Lister said:
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();
 
Back
Top