Declaring an object within an if scope?
BlitzMax Forums/BlitzMax Beginners Area/Declaring an object within an if scope?
| ||
Hello i cannot seem to able to declare an object within a score such as Type Yum method Eat() End Method End Type if Hello = False WhatsUp:Yum = new Yum 'doesnt register' Endif if this isn't possible could i have some alternatives. Last edited 2012 Last edited 2012 Last edited 2012 |
| ||
god damn bump |
| ||
Declare ist in front of your if / switch, if it is not used it will be null and eventually removed by the GC. |
| ||
do you mean the declaration of the type? could you possibly write it how it should? Last edited 2012 |
| ||
[#Edited, because of Derron's adwise below:] Why should this example not work? It works fine... but the scope of the object isn't limited on the IF...ENDIF but on the whole function( in this case MAIN() ) because you did not declare SUPERSTRICT. So it lives also behind ENDIF. Graphics 800,600 Type Yum Field X% Method Eat() Print "Eat it " + X End Method End Type If Hello = False WhatsUp:Yum = New Yum WhatsUp.X:+1 WhatsUp.Eat() EndIf Print "Press any key..." WaitKey() WhatsUp.X:+1 WhatsUp.Eat() Do you later need the object outside the IF scope? Or do you want the object to be NULL outside? If you want to set it null, do this: SUPERSTRICT ... Local Hello% = True If Hello = True Local WhatsUp:Yum = New Yum WhatsUp.X:+1 WhatsUp.Eat() EndIf ... But If you still need it later, you should declare it before IF, because if IF was not true, the object would not be declared: This causes an error in the second part: SUPERSTRICT ... Local Hello%=True If Hello = 123 Local WhatsUp:Yum = New Yum WhatsUp.X:+1 WhatsUp.Eat() EndIf Print "Press any key..." WaitKey() WhatsUp.X:+1 WhatsUp.Eat() in this case this would be better: ... Local WhatsUp:Yum = New Yum If Hello = 123 WhatsUp.X:+1 WhatsUp.Eat WhatsUp=Null EndIf Print "Press any key..." WaitKey() WhatsUp.X:+1 WhatsUp.Eat [#EDIT] I corrected my post, because I was wrong. Thanks Darron. Last edited 2012 |
| ||
To avoid such obstacles... use SUPERSTRICT ... it complains about not knowing "WhatsUp" after the "if"-phrase. why= "local WhatsUp" is done WITHIN the scope of the "if hello...EndIf"-part. If you do not use SuperStrict you will not be able to guarantee what an variable will be in different scopes (I think) - to avoid it, use SuperStrict and you will get compiler warnings if something is not obvious or just unclear to the compiler. Maybe the following modification will show the difference (maybe uncomment the commented code) [bbcode] SuperStrict Graphics 800,600 Type Yum Field X% 'Method New:int(newX:int) 'also possible with params Method New:int() self.x:+1 End Method Method Eat() Print "Eat it " + X End Method End Type runA() print "runB:" runB() Function runA() local Hello:int = false If Hello = False 'register a local var 'WhatsUp only accessible in "IF Hello ...EndIF part" Local WhatsUp:Yum = New Yum WhatsUp.Eat EndIf 'would be UNKNOWN 'Print "Press any key..." 'WaitKey() 'WhatsUp.X:+1 'WhatsUp.Eat End Function Function runB() 'register a local var (nulled atm) 'WhatsUp accessible in whole function "runB" Local WhatsUp:Yum=null local Hello:int = false If Hello = False WhatsUp = New Yum 'doesnt register' WhatsUp.Eat EndIf Print "Press any key..." WaitKey() WhatsUp.X:+1 WhatsUp.Eat End Function [/bbcode] |
| ||
Type manners Field ignore:Int Method Cost:Float() Return 0.0:Float End Method End Type Local you:manners If welcome$ <> polite$ you = New manners you.ignore = True End If If ( you <> Null ) Print you.Cost() |
| ||
sorry for delayed post thanks guys you guys went through alot of effort to write all that. |