PDA

View Full Version : How to detect the presence of an instance of a class



mud2
01-12-2007, 02:07 PM
As part of my latest question...I'm going to create a class to store values of variables. But when I return to my starting form, the instance will be there, containing values. how do I detect the instance of the class created? I can then set it to "Nothing" and in this way delete the data I might have entered.
Nevermind the reasons, just how to detect the presence of an instance.
Thanks!

XLGibbs
01-12-2007, 03:52 PM
It would depend on how you intend to use the class to handle variables. When you create the instance it would be usually on your form initialize (or Form_Load event) Where you would
dim myClass as New yourClassNameHere


and then you would Set myClass = whatever

Out of curiousity, why not just use public variables? Those can be utilized throughout the code and populated as such (as public variables are declared outside a normal routine, but can be called or populated from any of that module.

XLGibbs
01-12-2007, 03:53 PM
As far as detecting it you could also just do a check

If not is Nothing myClassNameHere then
' do stuff meaning it is not empty
Else
'do stuff since it is empty

mud2
01-12-2007, 06:10 PM
Mr. XLGibbs...thanks.
First, i can't get the syntax of the If is nothing...
straight
Now, Why?
I have several forms, based on a few tables. As I enter data in these forms I save it as Private variables in a separate module, then when needed in a different form I can "get" them.
Often I might leave a form to start over again...the form I leave is not the final one. When I start again these Private variables are still there, and show up at various places in following forms.
Now, why a class? I want to "null" out all these variables when I start, or restart. "NULLing the private variables doesn't work, as I have null tests throughout the program. The simplest(?) way is to put the values in an instance of a class, and after use, set that instance to NOTHING Then, when starting again These values would not be there. So:
I start...fill out a few forms and save the data...then change my mind and start over again...now I must remove the data.

But "SETting" a non-existant instance to nothing would probably give me an error? So, first check for the instance. If there, "NOTHING" it. If not, proceed.

OR, Save the data to a temporary table...it's easy to check for a table's presence... but this means rewriting all my saves to Private variables and somehow accessing them as and when needed.

???

XLGibbs
01-12-2007, 06:39 PM
oops syntax was inverted

If object Is nothing then

or

If Not object is nothing then

If the intent is to store variables that are created via another (subsequently inactive) form, you can retain that forms variables by not unloading the form and using

Me.Hide

or

FormName.Hide

to make it invisible while on your other form.

You can have other events trigger a resetting of the variable to nothing, or even a subroutine that does that which can be called by other actions.

You can also store variables in "Invisible" text boxes or lables.

Simply have them off to the side of the used area of the form (or even hidden behind other objects and populate the textboxes or labels with your variables as needed. (Just set their properties to visible = false and they won't be seen on the user side)

Just a thought.

All of them could be stored on a form which is never seen as well. A form could be made purely to carry the data and it could be loaded, but set to visible = false. Throughout your code you can just populate the invisible text boxes and such by referencing the object

HiddenForm.TextBox1.Value = myvariable.

mud2
01-12-2007, 10:21 PM
More thanks to Mr Gibbs....When I run this function, I get "Object Needed"

Public Function Obj_Exists(Obj_Name) As Boolean
MsgBox ("In Obj_Exists, Obj_Name = " & Obj_Name)
If Obj_Name Is Nothing Then
Obj_Exists = False
Else
Obj_Exists = True
End If
End Function


Bed time!

mud2
01-12-2007, 10:40 PM
If I had Mr Gibb's reply carefully:

Change
Public Function Obj_Exists(Obj_Name) As Boolean
To
Public Function Obj_Exists(Obj_Name As Class_To_Hold_Data) As Boolean

And the calling sub has
...
...
Dim Data as Class_To_Hold_Data
Set data = new Class_To_Hold_Data
...
...
Msgbox("Instance Data Is Present " & Obj_Exists(Data))
...

Now I can start rewriting.....
Good night!