Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


URGENT :  How to determine is an object has been initialied

Posted on 2006-04-10
Medium Priority
Last Modified: 2010-04-07
I have a class which I declare as

Dim cls as MyClass

Then later in the program,

Private Sub MySub()
 Set cls = new MyClass

End Sub

In another part of the program,

Private Function MyFunction(id)

  if cls = nothing then
     call MySub
  end if


End Function

THE LINE "IF CLS = NOTHING" is wrong ... how do I correctly frame it ? How do I determine if cls has been properly initialised or has been simply declared as an object ?

Question by:dwe0608
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16415321
 if cls IS nothing then
     call MySub
  end if
LVL 44

Expert Comment

ID: 16415342
Hello dwe0608,

you could use
If cls Is Nothing Then

end if

hope this helps a bit

Accepted Solution

Kaleb2000ac earned 2000 total points
ID: 16417899
Function IsObject will tell you if a variable is an object, bul will return true if it is set to nothing
the other solusions above will return an object required error.

My suggestion is the function TypeName that will return the string "nothing" when de variable has not been set and will return your class name when it has been set.

if TypeName(cls)="YourClassName" then ....

the next code will show you how it works, is a new project with a class added to it.
Dim myvar As Class1
Private Sub Form_Load()
 Debug.Print TypeName(myvar)    'Prints nothing in debug panel
 Set myvar = New Class1
 Debug.Print TypeName(myvar)  'prints Class1 in debug panel
End Sub



Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question