[Gambas-user] Changing the value of a variable or object outside the class
adamnt42 at gmail.com
Mon Apr 17 09:19:24 CEST 2023
On 17/4/23 3:11 pm, Mayost Sharon wrote:
> I checked it doesn't work
> It seems that as T Lee Davidson said a STATIC statement should be added
> With a timer, you're right that it doesn't make sense
> But let's say it will be h_connection
> that you open it once in CLASS1
> And in the rest of the CLASSS you want to perform operations on the h_connction
> such as inserting a record, deleting a record, etc.
> And I want to do it through CLASS2 or CLASS3
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
It doesn't work because you are trying to access a _class_ as if it were
an _object_! For your class2 to access anything in the _Class1 object_
that is its owner it needs a reference to that object, not the class itself.
As for your Connection example, what you are trying to do is code what
is known as "business objects". CLASS1 is the base object that
interacts with the database. CLASS2 and CLASS3 are "business objects"
that interact with the database via the base object. The simplest and
cleanest way to do this is to make CLASS2 and CLASS3 children of the
base class, i.e. they _inherit_ from it. Then they can use anything in
the base class that it exposes. Try and follow the following.
_Class1 (which I will call the "BaseBO"_
' Gambas class file
''' This class is the parent of every Business Object class in the
''' It provides the common methods and infrastructure that allow the
child classes to operate.
Property Read _rowcount As Integer '' Returns
the number of records in the current cursor (_$result).
Property Read _index As Integer '' Returns
the index of the current row in the cursor.
Public _$boname As String '' R/W!
The name of the BO class
Public _$data As Collection '' R/W!
The collection contains all the BOData items for the current object
NOTE this is the proper way to provide a Collection, Gambas provides all
the stuff you need. No need to write your own handlers.
Public _$result As Result '' R/W!
This should be invisible to the BO classes but they need it.
Public _$MetaConn As Connection = Connections["Meta"]
Public _$MainConn As Connection = Connections["Main"]
... thereafter follows about 1200 lines of code to support the child
objects, handle security etc. Probably the interesting one is
'' Returns the Value of the *pn* property where pn is the Property name.
Public Function _get(pn As String) As Variant
Dim an As String
Try an = $meta.Properties[pn].ColumnName ' Don't
worry about this, it maps the Property name to a database column
If Error Then Return Null
_Class2 (here "Seasons")_
' Gambas class file
Property Month As Integer 'month
Property Season As String 'season
Public Function Load(mth As Integer) As Boolean
*Me._$result* = *Me._$MainConn.Find*("seasons", "month = &1", mth)
If *Me._$result.Available* Then
Private Function Month_Read() As Integer
Private Sub Month_Write(Value As Integer)
*Me!Month* = Value
Private Function Season_Read() As String
Private Sub Season_Write(Value As String)
*Me!Season* = Value
I have made bold all the places in the Seasons class where it is
referring to the parent class methods and data.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the User