Is it possible to assign a procedure to a variable?
Author |
Message |
sam21292
|
Posted: Tue Jan 01, 2008 12:43 pm Post subject: Is it possible to assign a procedure to a variable? |
|
|
I'm doing a car race and I want the user to pick which car they want. In order to do this, I need a variable to assign the procedure of each car to. I just wanted to know if it was possible to assign the procedure to a variable. My program is in my section where I'm asking for help. Here is a link to it:
http://compsci.ca/v3/viewtopic.php?t=16810(its the one all the way at the bottom because I had earlier updates) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Saad
![](http://compsci.ca/v3/uploads/user_avatars/182387547249e7ebb91fb8a.png)
|
Posted: Tue Jan 01, 2008 12:59 pm Post subject: RE:Is it possible to assign a procedure to a variable? |
|
|
Yes it is, however it is a really bad idea.
Why would you want to do it, you could just call the procedure?
Also, what your trying to achieve can be done through parameter passing.
Edit: Also, suggestion, stick to one topic for asking the questions ![Rolling Eyes Rolling Eyes](http://compsci.ca/v3/images/smiles/icon_rolleyes.gif) |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Tue Jan 01, 2008 5:54 pm Post subject: RE:Is it possible to assign a procedure to a variable? |
|
|
This is indeed possible. It's poorly documented, but Turing GUI module relies on this ability (where each button links to a procedure, through a variable).
Now, if you want to represent different cars by different procedures, that would be a bad idea.
A car is more of an object, and would ideally be represented by such. A record of car's paramers would be an acceptable substitute, if you are not familiar with OOP.
Though if you want your car to be able to call upon variable procedures (lets say powerups or special moves/effects), then it would make sense to organize procedures in an array of variables. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
sam21292
|
Posted: Tue Jan 01, 2008 7:12 pm Post subject: RE:Is it possible to assign a procedure to a variable? |
|
|
But what if I have two different types of cars for the user to choose, lets say an SUV and a sports car (which are the selections that I have). In order for the user to choose either one of these, should I use boolean? If not, then what is the easiest way to do so? |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Tue Jan 01, 2008 10:14 pm Post subject: RE:Is it possible to assign a procedure to a variable? |
|
|
what you should have is a concept of an abstract car. The user has "a car", we don't yet know which one it is, but it has everything that any car would have -- acceleration, top speed, etc.
The differences between SUV and a sport car are just those parameters.
The point is, a car accelerates just the same weither it's an SUV, a sport car, or a tank. The only difference is the value of the acceleration. It would be silly to write the same procedure for every type of a car, and just one value. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
sam21292
|
Posted: Wed Jan 02, 2008 7:04 pm Post subject: RE:Is it possible to assign a procedure to a variable? |
|
|
i see... |
|
|
|
|
![](images/spacer.gif) |
|
|