Computer Science Canada Each instance of a class? |
Author: | Cervantes [ Sun Feb 27, 2005 6:39 pm ] | ||||
Post subject: | Each instance of a class? | ||||
Is there any way for Turing to go through each instance of a class and do something with it? ex. I want to make a button class. It looks something like this (with the procedure's code left out for easier reading)
Right. So when I want to use that in my program, I first make a bunch of instances of the BUTTON class, then I want to (inside my main loop) go through each instance of the BUTTON class and run the checkSelect procedure. Then I want to draw each button. But I don't want to have to resort to doing this:
Thanks in advance, -Cervantes |
Author: | Delos [ Sun Feb 27, 2005 6:50 pm ] | ||||
Post subject: | |||||
Idea 1: - instead of instantiating so many specific vars, why not just use an array of ints/whatever other type you need? I'm sure you can do that in a nice loop...flexible arrays could even make things easier. Also, you might want to add procs within the class that can handle that sort of things...
Idea 2: If you want to keep your specifically named vars... Each time a var is instantiated (in your unavoidable clause), add a reference/pointer/other such Post-It to an array within the class that can then be called by another procedure to do things...
Could work... |
Author: | Tony [ Sun Feb 27, 2005 9:32 pm ] |
Post subject: | |
sorry, no time to read though that. You can dig though GUI class source to see what they are doing there. I'm pretty sure that each new instance (class.initialize) adds a pointer to self to a global, flexable array. GUI.processEvents when loops though that array, processing each instance. Just have to remember to remove the pointer upon disassembly of the instance |
Author: | wtd [ Sun Feb 27, 2005 11:52 pm ] | ||
Post subject: | |||
Why can't you have:
|
Author: | Cervantes [ Mon Feb 28, 2005 10:49 am ] | ||
Post subject: | |||
Given the somewhat limited time I've got, I went straight to wtd's suggestion, as it seemed the simplest.
Nice. But, I'm now confused about how I'm going to make the button do something, when you click on it. Obviously, the code has to be in the program, not the class. But I don't want to do it like Turing's GUI has done it, as that's annoyed me to no end. So far as I know, Turing's GUI won't let you do anything with functions, or with procedures with parameters. Any ideas? -Cervantes P.S. Delos, I don't understand how INIT_all would be any better, because you're way I'd have to initiate the array. This way, I have to initiate the button using a pile of parameters. I don't know, I don't think it's avoidable. ![]() |
Author: | wtd [ Mon Feb 28, 2005 1:12 pm ] |
Post subject: | |
Use a real programming language. ![]() |
Author: | Cervantes [ Mon Feb 28, 2005 7:02 pm ] |
Post subject: | |
wtd wrote: Use a real programming language.
![]() Out of context, Cervantes wrote: I don't know, I don't think it's avoidable. ![]() After three full semesters of no computer science class, I'm back in the class. And we learn Turing. ![]() My thinking right now is I'm going to create a function of the button that will return whether the button is clicked or not. In the main loop of the program that uses the buttons, I'll case through all the buttons and insert procedures if whichever button is true. It means I'll have to go through all the buttons, but at least it's only the second time, and at least it's the last time. ![]() -Cervantes |