473,748 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1771
"Craig Lister" <My*********@th elisters.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.Collecti ons.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(str ing 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.fileDate Time = System.DateTime .Now;

fileList.Add(my File);

}

}

private void button1_Click(o bject sender, EventArgs e)

{
getFileList(edb xSource.Text);

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

{

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

}

}

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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.co m> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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.GetFi les
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******** *************@g 47g2000cwa.goog legroups.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.GetFi les
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.FileNam e = i.ToString();

myClass.FileSiz e = i;

myClass.FileDat eTime = System.DateTime .Now;

myArray.Add(myC lass);

}

return myArray;

}

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8253
by: dasod | last post by:
I would like to know if my method to remove list objects is correct in this small test program. It seems to me that there might be a simplier way, but I'm afraid I don't know enough about list iterators and how they are behaving in situations like this. #include <iostream> #include <list> class Test; typedef std::list< Test* > Tlist;
9
1930
by: F. Da Costa | last post by:
Hi, Does anybody know why IE5+ does *not* honour array objects (like a table) across a session? Example: Frame A contains a var tableVar which is set via form Frame B (on init) using top.A.tableVar = document.getElementById("someTable"); As long as Frame B is *not* 'refreshed/ reloaded' witk another page the
2
2762
by: Jared | last post by:
Store List Box values I have a form that has hundreds of records in it. The form has a list box with 10 different options in it. The user is able to select multiple options from the list box. What I need to figure out is, how do I store the options the user selected so when they go to the next record the value isn't lost. Can anyone help?
2
1441
by: martin | last post by:
Hi, I would appreciate knowing the best way to store a two dimensional array, and the bind that array to a dropdownlist. The original array is in the form integer, string and the list will look like this
0
1130
by: Dennis Bjorklund | last post by:
There is no command in psql to list the objects in a schema. The methods I've had so far is \d the_schema.* that shows all objects in detail, not a list. And this is what I've been doing: \d the_schema.<tab><tab>
2
2794
by: Aussie Rules | last post by:
Hi, I am migrating my skills from winforms to webforms, so sorry for the most basic of questions When I add an item into a dropdownlist in winforms, i create an object and add the object to the dropdownlist using code such as Dim oObject As New clsObject oObject.ID = pRow("Style_ID")
5
4718
by: tlyczko | last post by:
I am new to SS2005, and I've just started working on a small test/dev database. I recently read that one should store things like tables, views, constraints, etc. in the *.ndf file rather than in the *.mdf file. Does this make it any easier to transfer/copy files or databases or other items from test/dev to production?? If I have a database already with items in the *.mdf file, how do I
7
1847
by: Andy | last post by:
Hi, I'm trying to search and print any no# of Python keywords present in a text file (say - foo.txt), and getting the above error. Sad for not being able to decipher such a simple problem (I can come up with other ways - but want to fix this one FFS). Any help is appreciated. Thanks!! import keyword, re, sys, string inp = open("foo.txt", "r") words,lines = 0, 0
9
19842
by: Ajinkya | last post by:
Hello friends ! , I am very new to java script.If anyone can help me then I will be very very thankful to his/her. I am using php and mysql in my project and I have one textarea and one list boxes,now whenever user fill any value(email-Id) to textarea and press submit button then value going to listbox. These values are one or multiple , then they goes into list box , In list box these values are store in array . I mean user...
2
5324
by: ihimitsu | last post by:
Hi friends guide me to sorting java list contains multiple list objects
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.