View contents of arraylist when it's declared as public?

p_nivalis

Active member
Joined
Oct 1, 2004
Messages
33
Programming Experience
5-10
When I dim and fill an arraylist in a sub, I can see the contents of it by looking in the locals tab when I'm in the sub it was declared in.

But when I declare the arraylist as Public at the top of the form, it doesnt show up in the locals when I'm in any routine. I made it public so I could access it from many subs.

How can I view its contents when I need to?

Thanks!
 
Declared outside of any sub/function on the form it should be usable by all. Ensure that you have the watch set on the variable @ from where it is declared (i.e. the context is the highest level class such as Form1).

That being said, this is a typically bad way to achieve things. You are obviously trying to access similar data from multiple subs/functions. The way to do this is to use parameters (except in -rare- circumstances). You simply pass the object, or a reference to it, into a sub and its there for use.

What your doing is basically creating a global variable that anything in the program could come along and modify. While it may not seem harmful to you as a solo developer its a bad habit to form as it will (should) be discouraged in team projects. Before any dev. in our shop introduces globals into the project they are required to explain the logic of need to another dev so that alternates can be exposed.

If you've any questions on how to pass the object in and out of your subs/functions fire away, but its the rare case that you should -have- to use a global.
 
Thanks for the advice. I'm certainly open to learning new tips.

What I am doing here is, on program start, I have a sub that reads a long list of data into the array list (i'm working on an IM client of sorts, so in this case it would be user id numbers). Each id number in its own index, and I'm not familiar yet w/arraylists, so I was going to look into also seeing if there was a Key or Tag variable available also for each index, so i could stick the user's name next to the id number as well.

So when the program loads and reads/stores the data in the text file, it's essentially creating a live database in memory which can be altered and used on the fly (renaming a user, etc. etc, we all know how IM clients work ;))

It also needs to contact a server every 5 minutes or so and pass all of these id numbers to it so that the server can return the status of the user, so that I can again do more things with that info.

As it is now, the arraylist matches exactly the format of the array needed by the server to pass all of the id's as parameters.

I know that I technically could just keep saving/reading the info from the original text db, but I feel that all of that file i/o compared to in memory would be kind of ridiculous in many ways.

I also am not really familiar with passing objects like an arraylist and how it would work.

Last thing, I'm still not sure of what you meant in response to my original question, to be able to read the contents in treeview form, of the arraylist that has been publicly declared, from any sub/function. Can you clear things up a bit on that?

Thanks
 
Back
Top