Types... classes
BlitzMax Forums/BlitzMax Beginners Area/Types... classes
| ||
Is it just me or has the identifier "Field" become moot? Since you can't declare local/global variables within a type, its functions or methods, a declaration for a variable becomes assumed. Field A:int=0 becomes A:Int=0 Am I off base here? |
| ||
A "Field" is an instance variable for the class/Type. i.e. it has local-scope to an object instance of a type. You can also have Globals declared in the Type, which are "class variables", or "static". That is they have global scope for all references to the type. You can of course declare variables both locally and globally within a Method or Function, within a Type. Just as you would elsewhere. |
| ||
Brucey, Thanks... for some reason I was getting an error when I tried to declare global/local variables in my project... I just tried again in a new file and it works... not sure where I've gone wrong in my main project... A nice little learning curve with Max ;) Cheers. |
| ||
One more thing, if you don't mind my asking, which IDE are you using? |
| ||
I'm currently using the default IDE (on all three platforms). |
| ||
The community edition or the standard one that comes with it? Oh ok, I've been trying out Blide... its pretty nice but missing some features I like... IDEal was really nice (for B3D) but the developer is unfortunately incognito and there are a few bugs. |
| ||
Is this supposed to work like this?Type a Field b=1 Method c() Print b Self.b=2 Print b yes() Print b End Method Function yes() b=3 Print b End Function End Type aa:a=New a aa.c() WaitKey() End I get... 1 2 3 2 should it not be.. 1 2 3 3? Can the function not access the Type fields? I know the function does not like a self declaration... |
| ||
In the function yes(), "b" is a local variable. You may find it useful to add Strict or SuperStrict to the top of your source file. You'll find it will help you with some of your questions. Strict (and SuperStrict) require you to define the scope of the variables you create. So this line : aa:a=New a might change to : Local aa:a=New a :-) |
| ||
A quick explanation of scope: 'scope' means the set of variables that your code can access at any point in time. The way it works in blitzmax is that there's a 'hierarchy' of scope, starting at variables defined in the local environment, then moving down through object fields if you're in a method, to global variables declared in a type, and finally to global variables declared in the main program. The compiler starts at the top of the hierarchy and moves down until it finds the name it's looking for, and uses the value stored in that scope. In a Type, you can define Fields, for which every instance of the type has its own value, and globals, which have only one value for the whole type. Types can contain Methods and Functions. In a Function, the scope includes only global variables and local variables declared inside the function, and there is no 'Self' - defining a function inside a type is just a way of saying it does something relevant to that type. In a method, there is a 'Self' object which corresponds to the object whose method you called. All of that object's fields become local variables inside the method, so you can access them just by their names and without putting 'Self.' in front of them. Here are a couple of examples, which might help you to see what's going on: 'variable scope example 'by cp Global n=1 Function whatisN() Print n 'the only 'n' accessible in this scope is the one declared outside the types End Function Function whatisLocalN() Local n=2 'you can also declare a new local variable called 'n' which overrides the global one. 'the global 'n' is not changed at all and can still be accessed by other functions Print n End Function Type a Global n=3 ' global here means that it is accessible by all functions and methods declared in a Function whatisN() Print n 'in this scope, 'n' refers to the variable declared inside this type End Function End Type Type b Field n Method whatisN() Print n 'in this scope, 'n' refers to the field of the object whose whatisN method we called End Method Method whatisLocalN() Local n=6 'again, you can declare a local variable which overrides any other references to 'n' 'no other n variable is changed by this declaration, and this value disappears when the function finishes Print n End Method End Type b1:b=New b 'create an instance of the type 'b' b1.n=4 b2:b=New b 'and another one b2.n=5 'each of these functions will print the value of 'n' in their local scope whatisN() whatisLocalN() a.whatisN() b1.whatisN() b2.whatisN() b1.whatisLocalN() b2.whatisLocalN() 'you can also access a global variable in a type by using the dot syntax Print a.n 'Print b.n 'but you can't get b.n because each instance of b has a different value of n function names also obey scope rules: 'function scope example 'by cp Type a Function yes() Print "A says yes" no() 'this will call the no function belonging to a, because it is local in a's scope End Function Function no() Print "A says no" End Function End Type Type b Function yes() Print "B says yes" no() 'this will call the no function belonging to b, because it is local in b's scope End Function Function no() Print "B says no" End Function End Type Function yes() Print "yes" no() 'this will call the global no function End Function Function no() Print "no" End Function 'call the yes function belonging to a a.yes() 'call the yes function belonging to b b.yes() 'call the global yes function yes() |
| ||
Thanks Warpy and Brucy, So are functions part of the type schema and not the instance then...ahhhh that would make sense to me, since I would then have to pass the type instance to the function to use the instance fields because it wouldn't know where the instance data is. Cheers |
| ||
Functions are static, methods are instance. Same deal as in C# (read somewhre you've bene using C#, so that should make things clearer). |
| ||
Ya got it Jim... thanks muchly As soon as I realized that I've been slamming away at the code.. should be back where I was (but with enhancements) in a few days. |
| ||
If we look at the Warpy's sample code you see Type b has a whatIsN() method and there is a global whatIsN() function. How can I call the global whatIsN() function from within a b-instance? 'variable scope example 'by cp Global n=1 Function whatisN() Print n 'the only 'n' accessible in this scope is the one declared outside the types End Function Type b Field n Method whatisN() Print n 'in this scope, 'n' refers to the field of the object whose whatisN method we called End Method Method whatisLocalN() Local n=6 'again, you can declare a local variable which overrides any other references to 'n' 'no other n variable is changed by this declaration, and this value disappears when the function finishes Print n End Method '*********************** Method whatisGlobalN() 'how do I call the global whatIsN() function here? End Method '*********************** End Type b1:b=New b 'create an instance of the type 'b' b1.n=4 b1.whatisN() 'will print 4 b1.whatisGlobalN() 'should print 1 |
| ||
IIRC try:Method whatisGlobalN() 'how do I call the global whatIsN() function here? .whatisN() End Method |
| ||
Although a very bad practice to name both a function and method the same (he was just trying to illustrate scope), the difference would be in how you call it:b.WhatIsN() 'calls the function as it is from the base type (non-instance) local b1:b=new b b1.WhatIsN() 'calls the method (as it is instance based) over-riding the function (polymorphism) Cheers |
| ||
I think he wanted to call the global function which stays outside the type and not a base type function. |
| ||
Oh.. I'd have to test that but I believe there is a 'super' keyword that is used to call functions from the next level up... |
| ||
Actually I dont think you can... super only works within class instances to access the base type of extended types...'variable scope example 'by cp mod by _skully Global n=1 Function whatisN() Print "Global N: "+n 'the only 'n' accessible in this scope is the one declared outside the types End Function Type b Field n:Int=6 Method whatisN() Print "Type b N: "+Self.n 'in this scope, 'n' refers to the field of the object whose whatisN method we called End Method Method whatisLocalN() Local n=7 'again, you can declare a local variable which overrides any other references to 'n' 'no other n variable is changed by this declaration, and this value disappears when the function finishes Print "Type b L N: "+n End Method End Type Type c Extends b Field n:Int=9 Method whatisN() Print "Type c N: "+Self.n End Method Method WhatisSuperN() Print Super.whatisN() End Method End Type b1:b=New b 'create an instance of the type 'b' c1:c=New c ' now type c Print "Global N" whatisN() Print "From B" b1.whatisN() b1.whatisLocalN() Print "From C" c1.whatisN() c1.WhatisSuperN() |
| ||
Actually I dont think you can... Did the solution i my first post not work? Maybe you overlooked the small dot? Method whatisGlobalN() 'how do I call the global whatIsN() function here? .whatisN() End Method |
| ||
Well, yes I did overlook the small dot LOL Me needs glasses ;) Thanks for that! and yes it works. |
| ||
Although a very bad practice to name both a function and method the same (he was just trying to illustrate scope), ... I agree, but I was curious if there was some kind of syntax available. And as jsp showed us, it seems there is. Thanks! |