Setting a Custom Class as a Global variable

X

Xiazer

I know when a custom class this is the proper syntax
Dim SSheet as New SSched
but I want to know is how to change it globally. I use that same
syntax uptop of the sheet to set it globallly, but it seems like
whenever I access the page or something it resets the variable as a new
variable and all my information is reset.

if I *Dim SSheet as SSched* you know with out the "new" I always get an
error. my question is how do I set a Global Custom Class Variable?
 
C

Chris Marlow

Xiazer,

I'd declare without the New & use the Workbook_Open method to initialise
(Set x =New x).

I'd probably declare it private to the code module & set Property Get/Set
methods up to access it, but that would be doing a belt & braces job.

Regards,

Chris.
 
X

Xiazer

I've Tried the Set, and am playing with the get, but I can get the code
to run but everytime I do it seems like im reseting my global var to a
New.

I have an Options menu(custom form), and I want to set all those to a
custom class global variable on sheet1.

I created a function that when called from by another sheet it will set
a passed class to the global var of sheet1

*sheet1.SetVartoGlobal (Variable)*

then it is supposed to set to passed variable to the global variable of
sheet1. Although I alway get an error saying that the global variable
doesn't equal anything.

What would be the proper way to access a global variable on sheet1 from
custom form code
 
C

Chris Marlow

Hi,

From experience if you amend your code you need to re run the
'Workbook_Open' - as I think changing the code resets the global variables.

You would be able to access a public variable on sheet1 (where sheet1 is the
code name - not the sheet name - i.e. what you can change in the properties
window in VBA) as sheet1.variablename, provided variablename has been
declared as public (or has public get/let/set accessors).

As a toy problem try adding the following to the 'ThisWorkbook' code module
of a new workbook;

Option Explicit

Public dOpenDateTime As Date

Private Sub Workbook_Open()

dOpenDateTime = Now

End Sub

Close it and open it again - type;

? ThisWorkbook.dOpenDateTime

In the immediate window & you should get the date/time you opened the file.

In principal I think this is the basis of what you are trying to do. To add
accessors you would change;

Public dOpenDateTime As Date

To;

Private dOpenDateTime As Date

And add;

Public Property Get OpenDateTime() As Date

OpenDateTime = dOpenDateTime

End Property

Public Property Let OpenDateTime(dNewValue As Date)

dOpenDateTime = dNewValue

End Property

I'm using Let as a Date variable type is not an object - otherwise I'd use
Set & need to use Set in my Workbook_Open routine.

Regards,

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top