Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global variable vs. property Let/Get 2

Status
Not open for further replies.

pachad

Programmer
Mar 13, 2003
45
US
I have a value that is calculated when the form loads. This value is required by all the routines in that module (Form1).

I can either store/retrieve the value in a global variable when form loads (i.e. make the variable public for the whole Form1 module), or use property let() and property get() add a new propery to Form1.

Question: Which way is better, global or propery let/get?
 
Given that, under the bonnet, VB actually implements class variables as property Get/Set pairs, there's an argument (although not a strong one) that says you might as well use them yourself.
 
I always use the methods set and get, the reason for this is because lets say you have a variable called voucherAmount, and there is a function that calculates voucherAmount, they will obviously have the same name but if you have a function called setVoucherAmount and getVoucherAmount then these conform to natural language and this also means that you are not getting vars mixed up with other vars, as you access them by module_name.function_name cut a long story short it just makes it easier to manage and read

Binary Intelligence, true or false?
 
If I read the question correctly, a private form level variable will fit the requirement. In terms of encapsulation, that is "better" than a public form level variable.

If you want to expose that value to the other objects & modules in the program there are many ways of doing this
a get/let (or set) pair on the form. Using get/let allows, aslitton1 points out, you to process values through coding when you set & retrieve values (e.g. pouns to pence conversion etc). Like wise, the property can be exposed as read only (omit let/set routine) You will need to be very careful of object lifetimes, as it is easy to get confused. A public form level variable is another way, but that doesn't offer any encapsulation and will suffer from the the same object lifetime issues. Another way would be a private variable in a module (not form) that has a get/let pair.

Choice time!

My order of preference would be (according to requirements)
if visibility required across multiple objects; use a private variable held in a module with a get/let pair, then a private form variable with get/let pair then (and trailing a long way) is a public form variable.

However
OP said:
This value is required by all the routines in that module (Form1).

Private form level variable works for me!

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
litton1,

I did not know that you could use propery let/set routines in a standalone module. I understand your post to mean that I can create a propery let/set statement in a regular .bas module, and then refer to that property as modMyModule.MyProperty.

Thanks, and take a star for the info. In my case, I only need the property to be visible to all the events on a single form, therefore I was not even thinking of using a module (too global).

mattKnight,

... a private form level variable will fit the requirement. In terms of encapsulation, that is "better" than a public form level variable...

Can you please elaborate on what you mean by a private vs a public form level variable.

I was thinking of declaring the variable in the declaration section of the form using the [blue]Private[/blue] keyword to ensure it can not be "seen" outside of the form. Is this what you meant by private?
 
Can you please elaborate on what you mean by a private vs a public form level variable.

I was thinking of declaring the variable in the declaration section of the form using the Private keyword to ensure it can not be "seen" outside of the form. Is this what you meant by private?

Private object/module variable is usable by any function or procedure within that object/module. They are declared by using the private keyword.

Public object/module variable is usable by any function or procedure within the application. They are declared by using the public keyword.

To me, global signifies a variable declared as public. I believe that variables can be declared using the global keyword for backward compatability.

Declaring, using the private keywrod, as you are intending is exactly what I meant!

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Thanks much.

Choice time!

My order of preference would be (according to requirements)
if visibility required across multiple objects; use a private variable held in a module with a get/let pair, then a private form variable with get/let pair then (and trailing a long way) is a public form variable.

I am going to hang that paragraph from my monitor for a few days....
 
thanks and glad to help.
Even though you only want them to stay in scope through one form you could still use the module set/get approach as long as the variables don’t become excessive. There is another good reason to adopt this approach and that is… well I will show you an example. Here is a function that I took from a java program that i wrote then tweaked it to fit into a vb module. I called the module modFormat as it is a module that carry’s all the different formatting that I need for the program. Yes it is long winded but it actually took me about 5mins to do because I do my functions by hand and then port them to whatever language or program I am writing. In this case the function gets passes a string which is a cash value and makes sure that it has 2 decimal places. I hope that you can further see the benefit from this. Sorry it was rushed lol
Code:
Public Function formatNumberToCurrency(numToFormat As String) As String
Dim searchChar As String
Dim stringLength As Integer
Dim i As Integer
Dim stringPos As Integer
Dim tempChar As String
Dim numberOfDecimals As Integer

numberOfDecimals = 0
searchChar = "."
stringLength = Len(numToFormat)

'Loop through the number and see if it contains a decimal
'if it does then how many places, are there one or two
For i = 1 To stringLength
    tempChar = Mid(numToFormat, i, 1)
    If searchChar = tempChar Then
        stringPos = i
        ' incase there is 2 or more decimals
        numberOfDecimals = numberOfDecimals + 1
        'MsgBox stringPos
        Exit For
    End If
Next i

    If numberOfDecimals = 1 Then
        If stringPos + 1 = stringLength Then
            'the number looks like this num.num and not num.num_num so ad a zero
            numToFormat = numToFormat + "0"
        End If
    Else
    numToFormat = numToFormat + ".00"
    
    End If

'MsgBox numToFormat
formatNumberToCurrency = " £" & numToFormat
End Function


Binary Intelligence, true or false?
 
Looks like a good function, but what are you trying to illustrate?
 
Sorry, my point is that when I write another program and I need a formatting function I can go to the correct module that has all the formatting functions (modFormat or modDate or modCalander etc). Hence the organisation is in place to allow the reuse of code easily without going through 5000 lines of code spread across 10 forms, hope this is a bit clearer.

Binary Intelligence, true or false?
 
So i would call that function using a line like this?
Code:
modFormat.formatNumberToCurrency NumberToFormat
 
dim numberToFormat as string
numberToFormat = "10"
modFormat.formatNumberToCurrency( numberToFormat )

Binary Intelligence, true or false?
 
or even
Code:
dim numberToFormat as string
dim formattedNumber as string
 numberToFormat = "10"
formattedNumber  = modFormat.formatNumberToCurrency( numberToFormat )
sorry

Binary Intelligence, true or false?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top