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

Select, Sort subgroup of class objects in collection

Status
Not open for further replies.

CraigBest

Programmer
Aug 1, 2001
545
US
Hi Folks

Continuing on in my attempt to build the Railroad application. I have several collections working of different types of classes (railroad, train, track, etc.) and I'm starting to try to figure out how to work with them now.

The main loop of the program is triggered on a timer to run every 10-20 seconds or so. In that loop the program is supposed to, among other things, look at each section of track in turn and determine if oe or more train is waiting to occupy it, and if so which train has priority (which will award authority to the track).

So - I have a collection of Authority objects in a collection (which are built from a collection of Schedule objects; as the time a train is supposed to leave a station hits a new authority object to the next station is created - but I digress), and what I want to do is look at the Authority collection and select all the objects that have the station in question as a property (which is not the lookup property you guys helped me with when I was creating te collection). I want to get a temporary collection or array of these specific object so I can process which one has the higher priority and update them.

If I were doing this as a Query from a DB I would write the statement as "SELECT Auth_ID FROM Authority WHERE Station_ID = 20", then iterate thru the data table and process it that way.

What are my options here? Create a temporary collection and copy the objects from the primary collection into it? I've read its also possible to create an array where the elements are defined as the same type of class, and then somehow you copy the objects out of the collection to the array?

How does one do something like this? My book is not very helpful in providing examples.

If I can learn to do this I will be able to re-use it very often in this project.

Thanks!


CraigHartz
 
The more complex you start to get the more your going to find you will want to keep them as tables in a database. For the very reason you can query them faster that iterating through them yourself. All of these different tools don't make up for handling a lot of data at once. When dealing with large groups of data tables are still the way to go. You just make program front end to look better and ease of use for the end user.

Create a temporary collection and copy the objects from the primary collection into it?
Yes, you could do that.

I've read its also possible to create an array where the elements are defined as the same type of class, and then somehow you copy the objects out of the collection to the array?
You could do that, but there is no point to switch back and forth between Arrays and Collections. It would really be better to stick with a collection. You have the option of the ListOf you want a type of collection. You can't have two different types of ListOf interact (which generally isn't a problem and preferred) like you could with a generic collection.

Something like this?
Code:
Dim Current As Object
Dim Priority New Collection
For Each Current In AuthorityCollection
    If Current.Station = "InTheMoneyStation" then
        Priority.Add(Current)
    End If
Next

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
oops
Code:
Dim Priority [RED]As[/RED] New Collection

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Hi Sorwen

Thanks - I was beginning to come to that conclusion for myself, it seems things are getting somewhat Unwieldly with all these classes and collections. It did seem to me that working with data tables (which I already know how to work with) would be easier and faster, but I was trying so hard to use OOP techniques! Figured I must be missing something, glad to hear my instincts are still good.

I guess I'm glad to hear that it's OK to still use data tables in an application like this. I wonder, is there still a place to use the classes and collections in what I'm doing, or is is better to just scrap them and go over to data tables? (I don't expect you to answer that). I suppose eventually as there will be a GUI user interface at some point, which will probably need to use similar objects, probably user controls.

Thanks again - I think at this point I'll chalk much of this up to experience and scale back some, use my SQL skills to do most of the tough logic. This has been good practice, I've learned a lot about classes and collections.


CraigHartz
 
It really all depends on what you are doing at any given time. A class is for any time you need to work with an instance of coding. I use class libraries a lot. I have a lot of code I have to reuse between projects since all my work revolves around Access/Excel/SQL/Other remote databases. I've made reusable tools to make that easier because I'm doing the same set of things to that data in 90% of my projects. For emailing on our exchange server and dealing with contacts/calendar/etc I made a set of tools.

I've made a few classes the inherited from panels because I need to extend it abilities. If I rely heavily on tables then I often end up with control classes to help with visual representations. I mean never forget a form is a class being able to instance a form so you can reuse it multiple time easily is extremely helpful. There are many reasons and uses for classes.

Would I use classes in what you are doing? I couldn't say. Now that you've seen some of what you can do with them it is likely you'll find yourself saying "Hey it would help to have a class for that".

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Yes, I can see what you mean. If I ever get to the point where I get the main logic working eventually I will need to build a GUI interface so someone can see whats happening in real time, and direct changes quickly and easily. The eventual goal of the program is to provide a dispatcher with the tools neded to run the railroad, namely to override the timetable and add or restrict trains as needed.

When that time comes, I can see the need for a repeatable user control that displays the status of a block, including which tracks are in it, where any trains are, direction of travel, authority to enter and/or occupancy, and conflicts in authority. Other forms classes will be needed to take track occupancy reports, display status information, write orders that affect the timetable, and more. So all this work in learning about classes will be useful when I get there.

CraigHartz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top