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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exposing an ArrayList 1

Status
Not open for further replies.

Nelviticus

Programmer
Sep 9, 2003
1,819
0
36
GB
I've written a document printing application (Windows forms/C#) that has an ArrayList of DocumentGroup objects, each of which has an ArrayList of Document objects. I want the main application to be able to access each Document in each DocumentGroup. The way I have done this - and I think it's the wrong way - is to make the Documents ArrayList public in the DocumentGroup class. Is there a way to create an accessor so that I can expose it properly?

Apologies if that sounds confusing, I'm not very good at explaining things :(

The application creates a bunch of DocumentGroup objects, each of which creates a bunch of Document objects. I want the main app to have access to the properties & methods of each Document. The Documents are held in an ArrayList in each DocumentGroup, and rather than having a 'get' accessor I've just made the ArrayList public. As you're not really supposed to make a class's variables public I was wondering what the correct way of doing it was.

Regards

Nelviticus
 
D'oh, I've just figured out how to do it - exactly the same way as any other variable, like this:

Code:
public ArrayList Documents
{
    get { return alDocuments; }
}

It's funny how something can bug me for days then as soon as I post something on TT I discover the answer myself!

Nelviticus
 
Yup. Arraylists are objects like any other type.

When I was learning O-O development, that's probably the thing that took me the longest to learn -- that you can pass pretty much anything around. Of course, whether you *should* is a different question!

My only suggestion would be to create a strongly-typed collection of Document objects, rather than relying on ArrayList. If you're using .net v2.0, then you'd use <List> instead to get your strong-typing, but for v1.x, you'll have to write some code. :-(

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Write some code - eek! Actually I don't mind that, it'll make me feel less bad about how much of the hard work Visual Studio and the .Net framework do for me. I'm working in .Net 2.0 and considered using List but went with ArrayList simply because I'd used them before - I'll look into List though, thanks for the suggestion.

Regards

Nelviticus
 
What I do is build my collection class deriving from the ArrayList class. I then use this base ArrayClass Add and Remove and methods to add and remove items from the collection. Also I might end up overriding some of the base methods.

But one efficiency I have noticed is to use the .AddRange method of the ArrayList class. Instead of adding one item at a time, you can add an entire group of items in one bang. I find that this is quite a bit more efficient. You can also use this .addrange method with combo boxes and list boxes.

 
Nelviticus,

Listen to Chip about the List<T> collection.

I've been learning these the past few days and they are no more difficult than ArrayList.

Example:
Code:
private List<Documents> allDocuments;

public ShowAllDocuments()
{
   string label = "";
   foreach(Document d in allDocuments)
   {
      label += d.Title + "\n";
   }
   MessageBox.Show(label);
}

public Document GetDocumentByIndex(int Index)
{
   return allDocuments[Index];
}

They are awesome! They are like ArrayLists on steroids.

You can even create custom collections without messing with all that Add() and Remove() bull-ony.

In fact, I just created a custom DataObject collection that will allow types of DataObjects but nothing else!!! and it was easy as pie!

By using:
Code:
class DataObjectList<T> : List<T> where T : MasterDataObject
{
   ///NEEDS NO MORE CODE!!! USES ALL BASE METHODS JUST DANDY!!
   //By saying "where T : MasterDataObject"
   //I am implying that When someone tries to declare
   //a new custom list they must pass a MasterDataObject
   //or a derivative of MasterDataObject
   // e.g. see next code block
}

now I can:
Code:
DataObjectList<MasterDataObject> list1 = new DataObjectList<MasterDataObject>();
DataObjectList<UserObject> list2 = new DataObjectList<UserObject>();
DataObjectList<CustomerObject> list3 = new DataObjectList<CustomerObject>();
DataObjectList<MasterDataObject> list4 = new DataObjectList<UserObject>();

MasterDataObject o1 = list1[0];
MasterDataObject o1_1 = list2[0]; //These work because all classes derive from MasterDataObject
MasterDataObject o1_2 = list3[0];
MasterDataObject o1_3 = list4[0];

UserObject o2 = list2[0]; // Works because list2 is of UserObjects
CustomerObject o3 = list3[0]; //Same reason

//Cant do this:
UserObject o4 = list3[0]; //Throws exception because list3 is of CustomerObjects

See how easy it is?

Atleast that is what I have learned the past few days - Chip, correct me if I am wrong!

Apologies for my enthusiasm, but as I said.. I just started using these and it cuts my time spent on writing code incredibly.
 
CrashDome - you are correct.

In our case at work, when we (finally) make the transition to 2005, we'll have 500-line custom collection classes that will drop to about 4 lines of code.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Wow, I've been doing too much work! However I think I came on late to the OOP arena so I want to learn how to do it the hard way, because it helps me to understand the concepts of OOP a little better.

Hasn't Java had generics all along? It's interesting that it took Microsoft 4 years to implement it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top