
-----------------------------------
lufthansa747
Fri Dec 04, 2009 6:16 pm

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

-----------------------------------
Zren
Fri Dec 04, 2009 6:26 pm

RE:global variable
-----------------------------------
You import the variable into the class.

var str : string := "what"
class dude
    import str
    export print

    procedure print
        put str
    end print
end dude


var guy : pointer to dude
new dude, guy
guy -> print



-----------------------------------
andrew.
Fri Dec 04, 2009 7:33 pm

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?

-----------------------------------
Zren
Fri Dec 04, 2009 7:43 pm

RE:global variable
-----------------------------------
There's no constructor/you have to make your own method. It's lame I know.

-----------------------------------
Superskull85
Fri Dec 04, 2009 7:52 pm

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).

-----------------------------------
andrew.
Sat Dec 05, 2009 12:10 am

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.

-----------------------------------
lufthansa747
Sat Dec 05, 2009 8:19 pm

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

-----------------------------------
mirhagk
Sat Dec 05, 2009 8:49 pm

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.

-----------------------------------
Superskull85
Sat Dec 05, 2009 9:35 pm

RE:global variable
-----------------------------------
@ lufthansa747,

Do you mean inheritence?

class Foo
    export Hello
    proc Hello (Name : string)
        put "Hello, " + Name + "!"
    end Hello
end Foo

class Foo2
    inherit Foo
    export HelloWorld

    proc HelloWorld ()
        Hello ("world")
    end HelloWorld
end Foo2
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.

-----------------------------------
mirhagk
Mon Dec 07, 2009 12:27 pm

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


function double(num:int):int
  result num*2
end double


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
