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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beginner: Global Variables, help me please!!!! 2

Status
Not open for further replies.

JANET1979

Vendor
Dec 3, 2002
13
GB
i have declared a global variable at the top of my main form underneath option explicit.

However i get this message no matter where on the form i place the global variable declaration >>

ERROR MESSAGE READS:
constants, fixed length strings, arrays, user defined types and declare statements not allowed as public members of object modules

heres my code:
Option Explicit

Global gCost As Currency

Private Sub CMDend_Click()

Dim iReply As Integer

iReply = MsgBox("Exit the program?", vbYesNo + vbQuestion, "Exit?")

If iReply = vbYes Then End

End Sub




Private Sub cmdReg_Click()

FRMmain.Hide
frmPrvtReg.Show

End Sub



Private Sub form_load()

CBOmodel.AddItem "Hatch Back"
CBOmodel.AddItem "Saloon"
CBOmodel.AddItem "Sports"

End Sub



Private Sub cmdReview_Click()

Dim cModelCost As Currency
Dim cPtx As Currency


'puts values to list options
Select Case CBOmodel.ListIndex

Case 0: cModelCost = 9000
Case 1: cModelCost = 11000
Case 2: cModelCost = 13000

End Select

'data validation, checks if model has been selected
If CBOmodel.Text = "" Then

MsgBox "Must select a model", vbOKOnly, "Model?"

Else

gCost = cModelCost - cPtx


End If

'dowhile CBOmodel.Text != ""

FRMmain.Hide
frmCheckout.Show

End Sub

Private Sub CMDptx_Click()


Const cMaxPtx = 13000
cPtx = 0


'read in the price of old car
cPtx = InputBox("Please enter car worth", "Enter Value")

If cPtx > cMaxPtx Then

MsgBox " Your car is worth more than what we have for sale ", vbOKOnly, "R U Sure?"

End If

End Sub


Many Thanks, Janet
 
As the error message reads...

You can't declare the public variable gCost in the form (object module)

This is because the form (object) may not exist during *all* the time that the program is executing.

The solution...

either declare gCost as a private member (probably not what you want) or
add a module to the project and move the declaration of gCost to it.

In general, however, I would caution against using global variables (as opposed to constants). To use them as a method of passing information between modules breaks principles of Object Orientated Programming.


Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
>>You can't declare the public variable gCost in the form (object module).

Yes you can, you can certainly declare it as Public. You can't declare it as Global though (which is what Janet tried).






Greetings,
Rick
 
Janet
oops

should have applied a little more RTFQ!

Lazyme

Ok edjicate me...

what purpose does the global declaration have. I've (obviously) never used it, and don't know how!





Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Global can only be used in modules, like you said (though it is recommended to use Public there as well, as Global is considered obsolete). Both are the same and both are truly global throughout the entire project.

Public variables in classes and forms (which is actually just a class) etc. can be accessed from outside the class as well (if you have a reference to the class, of course). Classes cannot have Global variables (you yourself already explained why...).

I don't think I'm telling you anything new here, do I ?



Greetings,
Rick
 
You should not use GLOBAL, it is there for backward compatibility. That means you should only have it in code that is preexisting. Instead use PUBLIC in the Basic Module.

Classes can't have global variables but you can make them Global multiuse or Global single use but this is to provide the facility for static functions.

Also as a general rule you should avoid global variables.
 
Ahh Its obsolete... which would explain why I hadn't come across it before.

Thank you both LazyMe and SemperFiDownUnda



Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top