Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I tell if an element in an Array is full or empty???

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
GB
Hi.
my problem is; that I can save an array to a file, then read in that array from the file again, but I have no way of knowing how many elements are used (how many instances of a Customer for example, exist within the customer array).
is there a method like CustomerArray.isObject() for example??????

Otherwise what else can I do to find out? I can't use a counter like I have done every time I create an instance of customer...

Anyway, any and all suggestions and comments are V.welcome!
Oxymoron we are all of us living in the gutter.
But some of us are looking at the stars.
 
I'm not sure what you are asking here.

Since you said you can't use a counter (which you could do if you where reading the customers from the file one by one) I can only assume you are reading the array as a whole and wanting to know how many elements contain actual Customer objects. ie the array has 100 elements but only 10 of them have customers stored in them.

One possible solution is to have the Cusotmer object tell you. Add a boolean field to your Customer object called
Code:
customerExists
or something and set it to true if you have written customer details to it.

That way you can initailise the array so that every element has a customer object in it, but only those objest that have useful data in them have their
Code:
customerExists
flag set to true.

To find the total number of custmers then after reading the array from the file would be a simple matter of looping through all the elements counting those with the flag set to true.

This personally would be my choice, although I am sure there is a simple way of check to see if an array element is an Object that like you suggested (but I wouldnt know how - anyone?), however having the Customer object itself tell you its state feels like better OO design to me (but thats just me) :)

An added bonus of using a
Code:
customerExists
flag would be that you can delete a customer just by setting its flag to false again, thereby flaggin that it is no longer a customer.


I hope this rather long winded reply helps, if however I am totally on the wrong track in relation to your question, please let me know and I'll start again :) ----------------------------------------
There are no onions, only magic
----------------------------------------
 
Isn't an object reference null until you assign it to reference an object? Or is that just a C thing...argh to many languages.

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top