URGENT :  How to determine is an object has been initialied

Posted on 2006-04-10
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 142

    Expert Comment

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

    Expert Comment

    Hello dwe0608,

    you could use
    If cls Is Nothing Then

    end if

    hope this helps a bit
    LVL 1

    Accepted Solution

    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



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now