Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 37,677 times

Contents

Related Categories

Variables - Objects

Objects

You will find that quite often you need to refer to the same object a number of times. However, if you are refererring to that object using a property that will change, such as frmMain.ActiveForm, once the active form changes, you cannot refer to the object any more.

In order to be able to do this, you can assign a variable to it. You do this in the same way as any other variable, except that you need to use the Set statment before the assignment:

Set ObjectName = Object

You also need to declare the variable as an object:

Dim ObjectName As Object

On occasions, you will know that the object that the variable will point to will always be a form, or a text box etc. If this is the case, you can declare the object as a text box. Visual Basic will create an error when you try to assign a different object to it. To access the objects properties, you simply use the variable name in place of the object:

Set objForm As Form1
' Sets Form1's Caption
objForm.Caption = "hello"

The following code assigns objForm to the ActiveForm in the MDI window (called MDIMain). You will notice that even if the ActiveForm property changes, the variable will still point to the same object:

Dim objForm As Form
' Assign Form
Set objForm = MDIMain.ActiveForm
' Set the caption
objForm.Caption = "Welcome"
' Activate another form
Form1.SetFocus
' Change the old active forms back colour
objForm.BackColor = vbRed
' Activate the old form
objForm.SetFocus

Once you have finished using an object in your code, you should remove the reference to the object. You can do this using the Nothing keyword. The following code sets objForm to refer to Nothing.

Set objForm = Nothing

If you ever try to access a variable that is an object when no object is assigned to it, you will get a run time error:

Err 91
Object variable or With block variable not set

You must always assign an object to the variable before you can use it.

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • new solution

    Posted by jawsper on 17 Apr 2004

    i just found out how i can make my original idea work.

    i read something about the "eval" function in the microsotf script controls, so now ik kan just say: ScriptControl1.Eval(line) and it will fin...

  • Posted by LouisRose on 02 Mar 2004

    Hey cwamsley,

    Welcome to DevFusion!

    This sounds like a control array problem - if you're using VB (and not .NET) that is. You can have several controls with the same name, but with an index, lik...

  • String to Object Continued

    Posted by cwamsley on 01 Mar 2004

    Louis,

    I am working on a somewhat similar problem (to the String to Object comments from Aug 5, 2003) and hope you can suggest a course of action.

    I'm trying to create a simple way to set and mo...

  • Posted by Rde on 01 Mar 2004

    The following lists the public, private, and default visibility
    for the various elements you can declare in your programs.

    Default means declaring without anything proceding (or with Dim
    in the c...

  • Posted by LouisRose on 05 Aug 2003

    I'm glad that you came up with a solution.

    For muti-lingual support you best bet is to use a Resource file. Then the User's language and Locale will be detected automatically and your program will ...