Computer Science Canada global variable |
Author: | lufthansa747 [ Fri Dec 04, 2009 6:16 pm ] |
Post subject: | global variable |
i know it is a bad way to code but i am too lazy to do it the right way. i need to use a variable inside a class but it has been declaed in the main program how do i accses it |
Author: | Zren [ Fri Dec 04, 2009 6:26 pm ] | ||
Post subject: | RE:global variable | ||
You import the variable into the class.
|
Author: | andrew. [ Fri Dec 04, 2009 7:33 pm ] |
Post subject: | RE:global variable |
I have a question that's kind of related to this. I'm used to OOP in Java, so is there a way I can have Turing OOP have a constructor so that I can set some values when creating the object? Or do I have to use a global variable and/or my own method for initializing the variable? |
Author: | Zren [ Fri Dec 04, 2009 7:43 pm ] |
Post subject: | RE:global variable |
There's no constructor/you have to make your own method. It's lame I know. |
Author: | Superskull85 [ Fri Dec 04, 2009 7:52 pm ] |
Post subject: | RE:global variable |
Well the way you construct an object in Java is by essentially calling a method via the use of the keyword "new." So by creating your own method and than calling you are essentially doing the same thing as you would do in Java for the most part (plus writing one or two extra lines). |
Author: | andrew. [ Sat Dec 05, 2009 12:10 am ] |
Post subject: | RE:global variable |
Yeah, I figured I would have to do that. Oh well, I guess that's why OOP in Turing isn't really a good idea. |
Author: | lufthansa747 [ Sat Dec 05, 2009 8:19 pm ] |
Post subject: | RE:global variable |
ok but what if i want to acsess another class and edit some of its info form a different class how do i do that |
Author: | mirhagk [ Sat Dec 05, 2009 8:49 pm ] |
Post subject: | RE:global variable |
to answer the originally question, wouldn't you import it? I'm pretty sure you can import values from other classes as well, so you could do that i guess. |
Author: | Superskull85 [ Sat Dec 05, 2009 9:35 pm ] | ||
Post subject: | RE:global variable | ||
@ lufthansa747, Do you mean inheritence?
Or do mean reopening a class during run time? You cannot do the latter in Turing, but you can inherit other classes, as shown above. @ mirhagk, Kind of defeats the purpose of classes and objects though. The point of Object Oriented Programming is to treat each object as its own world where nothing else except that object can change the contents of that world. If you allow sharing of data, that has not been asked for, than its like merging two different worlds together where each world does not know for certain if their contents are valid. One world can change the other and vise versa. The best way to retrieve data from an object is to ask for it via a function and not by sharing writable variables. |
Author: | mirhagk [ Mon Dec 07, 2009 12:27 pm ] | ||
Post subject: | RE:global variable | ||
i know that would be the right way, but he asked how to access a global variable within a class. But the proper would to do it would be something like
and in your main program it'd look like the following [syntax="turing"] var mynum:=5 mynum:=double(mynum) [/sytnax] obviously you'd need to change that so it's OOP but that's basically how to do it |