Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 global variable
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
lufthansa747




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Fri Dec 04, 2009 6:26 pm   Post subject: RE:global variable

You import the variable into the class.
Turing:

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.




PostPosted: 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?
Zren




PostPosted: 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.
Superskull85




PostPosted: 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).
andrew.




PostPosted: 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.
lufthansa747




PostPosted: 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
mirhagk




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Superskull85




PostPosted: Sat Dec 05, 2009 9:35 pm   Post subject: RE:global variable

@ lufthansa747,

Do you mean inheritence?

Turing:
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




PostPosted: 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

Turing:

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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: