Are variables passed as parameters local?
BlitzMax Forums/BlitzMax Beginners Area/Are variables passed as parameters local?
| ||
I couldn't find anything in the help, so I'm a bit unsure. If I have a type, with a method inside it, say Type Person Method doSomething(x:Int = 0) end type Is the X variable local? I assume so but I can't explicitly declare it as Method doSomething(local x:Int = 0) so I just want to be 100% on the matter. Cheers. |
| ||
check it outGlobal x:Int=99 Type Person Field x:Int Method doSomething(x:Int = 0) Print x Print self.x EndMethod EndType p:person=New person p.x=-5 p.dosomething(3) |
| ||
It is possible to be acceded to the global variable X from doSomething? |
| ||
It is possible to be acceded to the global variable X from doSomething? No, since doSomething is a method, and x has been declared as field; thus, it substitutes the Global X variable. [EDIT] It IS possible, by preceeding a period to the global variable name: .X Check the below post by Beaker [/EDIT] But you can if you refer to X from within a Function in the type: function DoSomethingElse() print x end function 'and call it with Person.DoSomethingElse 'accordingly with the code in the 2nd post, it will print 99Anyway bare in mind that if you declare a function which accepts X as parameter, then the Global X value will be ignored again. Anyway, the best way to try these concepts is to practice yourself with it. And a good programmig habit is to differentiate such variables so that there are no misunderstanding. Sergio. |
| ||
You can access an external global like this:Strict Global x = 1 Type thing Method meth(x=2) DebugLog "method "+x DebugLog "external global "+(.x) End Method Function func(x=3) DebugLog "function "+x End Function End Type Local th:thing = New thing th.meth() thing.func() DebugStop End |
| ||
wow the access for the global is (.variable) I was hoping for a word instead of a symbol, but hey at least it is possible. Why isn't SELF a command anymore? |
| ||
Why isn't SELF a command anymore? ummm... it is. |
| ||
oh ya, but then it should highlight it when I type "self.x" |
| ||
it does seem to highlight but not if there's a . seperator after it. |