Global Variables / Global Objects 
Author Message
 Global Variables / Global Objects

Hi.

I know that it is >bad< to have global objects / global variables etc.
Therefore I am desperately trying to minimise the use of such things.

I have a VB Program that uses an Access Database Table of around 10,000
records. I need to have the RecordCount of the database available in
different forms of my program at different times.

However, a recordcount takes about 3 - 4 seconds to complete.

Therefore I wish to complete the recordcount once and keep the data
resident in memory so that I can access it directly from any form. Is a
global variable acceptable here??

The alternative is to define a property for my database class module
that is filled with the recordcount at the start of the program. Then,
any form can do a Library.BookCount to find the recordcount.

However, I then need to make my library a Global Object so that any
form use the Accessor Function to find the recordcount.

Is this better or worse than a "Standard" global variable?
Does making an object Global defeat the object of Scope??

Is there a better solution?

Many Thanks

David

--
----
David Oxley

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Mon, 19 Aug 2002 03:00:00 GMT  
 Global Variables / Global Objects
What about....

In Library module, have a module-level variable (BookCount).
Also have a:
Public Function GetBookCount
    GetBookCount = BookCount
End Function

Call this from another form.

Is this right?

Quote:

>Hi.

>I know that it is >bad< to have global objects / global variables etc.
>Therefore I am desperately trying to minimise the use of such things.

>I have a VB Program that uses an Access Database Table of around 10,000
>records. I need to have the RecordCount of the database available in
>different forms of my program at different times.

>However, a recordcount takes about 3 - 4 seconds to complete.

>Therefore I wish to complete the recordcount once and keep the data
>resident in memory so that I can access it directly from any form. Is a
>global variable acceptable here??

>The alternative is to define a property for my database class module
>that is filled with the recordcount at the start of the program. Then,
>any form can do a Library.BookCount to find the recordcount.

>However, I then need to make my library a Global Object so that any
>form use the Accessor Function to find the recordcount.

>Is this better or worse than a "Standard" global variable?
>Does making an object Global defeat the object of Scope??

>Is there a better solution?

>Many Thanks

>David

>--
>----
>David Oxley

>Sent via Deja.com http://www.deja.com/
>Before you buy.



Mon, 19 Aug 2002 03:00:00 GMT  
 Global Variables / Global Objects
David

The reason that Global variables are "bad" is because their values can be
modified from anywhere in the application.  What you should be
concentrating on, therefore, is a way to ensure that your RecordCount
value is accessible from anyhere, but can only be changed by the object
which has the authority to do so, presumably your AccessDatabaseTable class
module.

You could do this by changing your whole AccessDatabaseTable class object
to global and giving it a read-only RecordCount property.  However, this is
giving exposure to your whole class for the sake of protecting just one
property, which probably does not make much sense.  Instead, you could
create a separate global class with just one public RecordCount property.
You could then protect the value of RecordCount by restricting access via
Property Let.  The following sample code illustrates a way of allowing a
property of Class2 to be changed only by an object of type Class1:

' Module1:
Option Explicit
Public cl2 As Class2  '  Class to hold the RecordCount property

' Class1 - your DB class that is allowed to change RecordCount property of
Class2
Option Explicit
Public Sub ChangeRecordCount(RC As Long)
    cl2.RecordCount(Me) = 23
End Sub

' Class2  - to hold the RecordCount property
Option Explicit
Private m_RecordCount As Long

Public Property Get RecordCount(byval CallingObject As Object) As Long
    RecordCount = m_RecordCount
End Property

Public Property Let RecordCount(CallingObject As Object, ByVal RC As Long)
    If TypeName(CallingObject) = "Class1" Then
        m_RecordCount = RC
        MsgBox "RecordCount Changed"
    Else
        MsgBox "Invalid Object"
    End If
End Property

' Form1 - for testing
Private Sub Form_Load()
    Dim cl1 As Class1
    Set cl1 = New Class1
    Set cl2 = New Class2

    ' Try setting Cl2.RecordCount from Form1
    cl2.RecordCount(Me) = 23
    Debug.Print "From Form1 " & cl2.RecordCount(Me)

    ' Try setting RecordCOunt via Class1
    cl1.ChangeRecordCount 23
    Debug.Print "From Class1 " & cl2.RecordCount(Me)
End Sub

You could also use a separate method of Class2, instead of using a Property
Let, for setting the value of RecordCount - with the same restriction as to
the calling object.  This would avoid the need to pass an object reference
in the Property Get where it is not in fact used.

Regards,

John.......

Quote:

> Hi.

> I know that it is >bad< to have global objects / global variables etc.
> Therefore I am desperately trying to minimise the use of such things.

> I have a VB Program that uses an Access Database Table of around 10,000
> records. I need to have the RecordCount of the database available in
> different forms of my program at different times.

> However, a recordcount takes about 3 - 4 seconds to complete.

> Therefore I wish to complete the recordcount once and keep the data
> resident in memory so that I can access it directly from any form. Is a
> global variable acceptable here??

> The alternative is to define a property for my database class module
> that is filled with the recordcount at the start of the program. Then,
> any form can do a Library.BookCount to find the recordcount.

> However, I then need to make my library a Global Object so that any
> form use the Accessor Function to find the recordcount.

> Is this better or worse than a "Standard" global variable?
> Does making an object Global defeat the object of Scope??

> Is there a better solution?

> Many Thanks

> David

> --
> ----
> David Oxley

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Mon, 19 Aug 2002 03:00:00 GMT  
 Global Variables / Global Objects

Quote:

> I know that it is >bad< to have global objects / global variables etc.
> Therefore I am desperately trying to minimise the use of such things.

Minimizing Global variables is not a religious issue. It's impossible to
write a real-world application that has no global variables at all. (Global
variables covers objects since object pointers can be held in global variables).

If you are having to go through hoops to avoid using a global variable, it means
a global variable is needed there. Some people put all global variables and constants
in a class, have an object variable as global, instantiate it at app start up and
access all globals through the object. Making it a global module might defeat
some of the purpose.

Quote:
> I have a VB Program that uses an Access Database Table of around 10,000
> records. I need to have the RecordCount of the database available in
> different forms of my program at different times.

Your real issue in having this as a global variable instead of recomputing it
is: how current will the value be? What if the actual recordcount changes because
some records have been added/deleted by users? (Your global, wherever it is placed,
will be wrong.)

Quote:
> Is a global variable acceptable here??

> The alternative is to define a property for my database class module
> that is filled with the recordcount at the start of the program. Then,
> any form can do a Library.BookCount to find the recordcount.

> However, I then need to make my library a Global Object so that any
> form use the Accessor Function to find the recordcount.

> Is this better or worse than a "Standard" global variable?
> Does making an object Global defeat the object of Scope??

> Is there a better solution?

Looks like you are worrying too much. The only really bad situation is when you have
a lot of BAS modules each with their own global variables and UDTs and so on.

--
MikeC

Please reply to the group.

  MikeC.vcf
< 1K Download


Mon, 19 Aug 2002 03:00:00 GMT  
 Global Variables / Global Objects
Globals can be very useful and are needed applications.  Minimize globals if
you only use them in one form.  If you use them in several forms,
considering globals is not the end of the world, if no other solution
presents itself.

Using the Property Get and not the Property Let in your class module would
make the RecordCount property of your class read-only.  I would put this
property with the class because it kinda should be packaged together and a
"standard" global variable with the recordcount seems very barbaric to me.

If you are worried about the instance of the class always{*filter*} around,
you can set it to Nothing once you're done with it or quit the application.

Remi

Quote:


>> I know that it is >bad< to have global objects / global variables etc.
>> Therefore I am desperately trying to minimise the use of such things.

>Minimizing Global variables is not a religious issue. It's impossible to
>write a real-world application that has no global variables at all. (Global
>variables covers objects since object pointers can be held in global
variables).

>If you are having to go through hoops to avoid using a global variable, it
means
>a global variable is needed there. Some people put all global variables and
constants
>in a class, have an object variable as global, instantiate it at app start
up and
>access all globals through the object. Making it a global module might
defeat
>some of the purpose.

>> I have a VB Program that uses an Access Database Table of around 10,000
>> records. I need to have the RecordCount of the database available in
>> different forms of my program at different times.

>Your real issue in having this as a global variable instead of recomputing
it
>is: how current will the value be? What if the actual recordcount changes
because
>some records have been added/deleted by users? (Your global, wherever it is
placed,
>will be wrong.)

>> Is a global variable acceptable here??

>> The alternative is to define a property for my database class module
>> that is filled with the recordcount at the start of the program. Then,
>> any form can do a Library.BookCount to find the recordcount.

>> However, I then need to make my library a Global Object so that any
>> form use the Accessor Function to find the recordcount.

>> Is this better or worse than a "Standard" global variable?
>> Does making an object Global defeat the object of Scope??

>> Is there a better solution?

>Looks like you are worrying too much. The only really bad situation is when
you have
>a lot of BAS modules each with their own global variables and UDTs and so
on.

>--
>MikeC

>Please reply to the group.



Mon, 19 Aug 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Accessing a single global variable/object from Multiple Apartments

2. Accessing a single global variable/object from Multiple Apartments

3. Referencing Global Variable from a variable

4. Global.MPT and Enterprise Global Template

5. Refering to Functions and Subs held in one global template from another global template

6. global.asa and global.asax ?

7. Non-global global recordsets?

8. Access Forms collection, App object or Global object of the Parent application

9. how to show a global variable in a report

10. Use multiple API calls or Global public variable?

11. Global variables in VBA

12. Global Variable

 

 
Powered by phpBB® Forum Software