Property of Class out of scope in method

  1. 2 weeks ago

    Richard A

    Mar 13 Pre-Release Testers, Xojo Pro Puerto Vallarta Mexico

    Hi I have a database object aspeDB (MySQLCommunityServer) at top level, same as WebAppName

    Then have a class clRegPrice

    In session open I have Dim RegPrices As New clRegPrice

    In Constructer of clRegPrice I have dim dbASPE as New aspeDB

    In Method of clRegPrice dbASPE is nil, out of scope.

    What am I doing wrong.

  2. Dave S

    Mar 13 San Diego, California USA

    @RichardAlbrecht In session open I have Dim RegPrices As New clRegPrice

    that makes that class in SCOPE only for the duration of the open event.

    It needs to be "dim"d at a public scope level, in a module for example

  3. Richard A

    Mar 13 Pre-Release Testers, Xojo Pro Puerto Vallarta Mexico

    OK, I'll try that

  4. Richard A

    Mar 13 Pre-Release Testers, Xojo Pro Puerto Vallarta Mexico

    @Dave S

    I now have the Class in a Module.

    In Session open I now have
    Dim RegPrices As New mdGlobals.clRegPrice

    Have Database object aspeDB1 as a class in the module also

    In Constructor I have dim dbASPE as New aspeDB1

    Still nil in Method of RegPrices (clRegPrice)

  5. Norman P

    Mar 13 Pre-Release Testers, Xojo Pro we're off to see the wizard th...

    @RichardAlbrecht In Constructor I have dim dbASPE as New aspeDB1

    this is the problem
    when this code is in the constructor the variable only lasts for that method (event) etc
    the DIM makes this a local variable (local meaning ONLY in the event / method the code is in)
    and at the end of this method its destroyed

  6. Richard A

    Mar 13 Pre-Release Testers, Xojo Pro Puerto Vallarta Mexico

    Where should it be?

  7. Norman P

    Mar 13 Pre-Release Testers, Xojo Pro we're off to see the wizard th...
    Edited 2 weeks ago

    "should" really depends on your code and design BUT given how you're using this I would suggest the clRegPrices class have a property on it

        dbASPE as aspeDB

    and then in the clRegPrice Constructer you have

            dbASPE = New aspeDB

    NOTE there is NO DIM on this line because we're not creating this for local use only - the property on the Class will hold the instance

     Class clRegPrices
        Method Constructor()
             dbASPE = New aspeDB
        End Method
    
        Property
              dbASPE as aspeDB
        End Property
    End Class

    EDIT : fixed up my bad code in the constructor

  8. Dave S

    Mar 13 San Diego, California USA

    anything with a DIM will live only as long as the method or event that contains that statement... as soon as the containing method completes, the DIM'd variable "dies"

  9. Richard A

    Mar 13 Pre-Release Testers, Xojo Pro Puerto Vallarta Mexico

    I have
    dbASPE as new aspeDB
    in the constructer but getting compile error
    mdGlobals.clRegPrice.Constructor, line 1
    Can't call something that isn't a function
    dbASPE as new aspeDB

  10. Dave S

    Mar 13 San Diego, California USA

    place the declaration in a public/global location

    then you can say

    dbaspe = NEW aspeDB
  11. Norman P

    Mar 13 Pre-Release Testers, Xojo Pro we're off to see the wizard th...
    Edited 2 weeks ago

    @RichardAlbrecht I have
    dbASPE as new aspeDB
    in the constructer but getting compile error
    mdGlobals.clRegPrice.Constructor, line 1
    Can't call something that isn't a function
    dbASPE as new aspeDB

    my bad ... copy & pasted

    dbASPE as  new aspeDB

    should read

    dbASPE = New aspeDB

    sorry

    EDIT : I fixed up my prior post with the wrong code so others will not make the same mistake

  12. Richard A

    Mar 13 Pre-Release Testers, Xojo Pro Puerto Vallarta Mexico

    By George I think I've got it!!!

    Thanks guys now I have no excuse to play a game waiting for an answer!

    Thanks Again!

  13. Norman P

    Mar 13 Pre-Release Testers, Xojo Pro we're off to see the wizard th...

    I could remove my post and post it back in an hour or two :P

  14. Richard A

    Mar 13 Pre-Release Testers, Xojo Pro Puerto Vallarta Mexico

    OK another scope problem?

    Now in a form that opens the class object is out of scope.

    In session open I have
    Dim RegPrices As New mdGlobals.clRegPrice (mdGlobals is a module)

    So in the form open RegPrices is not available.

    What am I missing?

  15. Dave S

    Mar 13 San Diego, California USA

    Same problem. same solution

    a DIM'd variable lives only as long as the method that DIM'd it

  16. Norman P

    Mar 13 Pre-Release Testers, Xojo Pro we're off to see the wizard th...
    Edited 2 weeks ago

    @RichardAlbrecht In session open I have
    Dim RegPrices As New mdGlobals.clRegPrice (mdGlobals is a module)

    This creates the instance and makes it only usable IN the open event
    As Dave said this is the same issue as before
    If you need to keep the RegPrices for the entire duration of this form then the form should have a property on it to hold this instance
    And in the open event you do

       RegPrices = New mdGlobals.clRegPrice 

    again note the missing DIM

    This is all related to the concept of SCOPE (not the mouth wash)
    Scope has to do with "visibility" or your ability to access properties (methods constants or just about anything else)

    A quick example is code like

    for i as integer = 1 to 10
       dim d as new date
    next
    d.totalseconds = 0

    In this case D does not exist AFTER the loop ends because - its gone "out of scope" and so it has been removed from memory and cannot be used outside the loop

    This same principle applies to a method (event, getter or setter of a computed property, and other nestings of code)
    Every IF THEN ... ELSE ... END IF creates a new scope level in the code following the IF THEN and ELSE portions
    Same for every CASE in a select case statement
    Every time you enter a new method (event etc) there is a new scope level

    So this

    for i as integer = 1 to 10
       if true then 
          dim d as new date
       end if
       d.totalseconds = 0
    next
    

    will fail since d after the END IF no longer exists
    The IF created a new scope level and when that scope level is exited the variable in that level no longer exist

    This notion of SCOPE is vital to understand

  17. Richard A

    Mar 13 Pre-Release Testers, Xojo Pro Puerto Vallarta Mexico

    I would like the object visible to the entire app.

    I currently have it in a module, but in the form is OK, just trying to avoid putting frmApplication. in front of all the class referances.

    But It doesn't seem to work in the module.

    Is it safe to say if code complete can access a value it's out of scope?

  18. Dave S

    Mar 13 San Diego, California USA

    variables that you declare in a module can be Global or Private (there is more but for now :) )
    If you mark it Global then it can be accessed anywhere. If marked Private it can only be accessed by other members of THAT module (these apply not only to variables but to methods as well)
    If they are defined with a DIM statement WITHIN a method (such as Open Event) they exist only for the lifetime of that method and can only be access by that method.

    So SCOPE is like Real Estate, in that there are 3 important aspects ... Location, Location, Location :)

  19. Richard A

    Mar 13 Pre-Release Testers, Xojo Pro Puerto Vallarta Mexico

    DAH! It was marked protected.

    Stupid....

    Now the real work begins.

    Thanks Again

  20. brian f

    Mar 13 Pre-Release Testers, Xojo Pro Chilly California

    @RichardAlbrecht DAH! It was marked protected.

    Stupid....

    Now the real work begins.

    Thanks Again

    Not stupid, just a learning experience :D

    I have them daily ;)

or Sign Up to reply!