Property of Class out of scope in method

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.

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

OK, I’ll try that

@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)

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

Where should it be?

“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

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”

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

place the declaration in a public/global location

then you can say

dbaspe = NEW aspeDB

[quote=428350:@Richard Albrecht]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[/quote]
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

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!

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

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?

Same problem. same solution

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

[quote=428362:@Richard Albrecht]In session open I have
Dim RegPrices As New mdGlobals.clRegPrice (mdGlobals is a module)[/quote]
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

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?

variables that you declare in a module can be Global or Private (there is more but for now :slight_smile: )
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 :slight_smile:

DAH! It was marked protected.

Stupid…

Now the real work begins.

Thanks Again

[quote=428368:@Richard Albrecht]DAH! It was marked protected.

Stupid…

Now the real work begins.

Thanks Again[/quote]

Not stupid, just a learning experience :smiley:

I have them daily :wink: