Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object Instance

Status
Not open for further replies.

sijukumar

Programmer
Nov 28, 2001
3
IN
Hallo all,

I have a doubt regarding object instantiation.

dim ob as new MyObj

here, there is no instance is created until first method or property is called. then wht should ob represent?

dim ob as myobj
set ob = new myobj

here, the instance is created. then i would like to know whether ob contain reference to the standard IUNknown?

in the case of late binding, using CreateObject, whether the var conatin reference to IDispath?

I would like to know the complete steps during instantiation

Sijukumar
 
First suggestion would be read Tedpattison book on Programming COM apps in VB6.

Here is how this goes:
Dim obj as new myobj
This means you already have created a instance by default , when u have entered the scope of prcedure/module.So there is no need for "set" statement.

Dim obj as myobj
set obj = new myobj
also can be
set obj = createobject("myobj")

This means u have just got a interface reference to the myobj which actually would be imyobj, and when u set the obj with "new" or "createobject" you create the actuall instance of the class.Further refer "single use"/"multiuse" instancing to understand this better.

While the above declarations were early binding, the late binding would be
dim obj as object
set obj = createobject("myobj")

So never be under the impression that "createobject" means latebinding. Infact "createobject" would specify the programid for binding while "new" would specify the classid.
It is advised to follow "set" statements b'cas it helps in coding what is called as Just-in-time activation.

So better have a go at TedPattison, since the entire process is out of scope for me to describe here.

Hope this was helpful.



 
Hi,

Eureka!

It's such a relief to finally understand this.

I keep getting asked this question in interview / technical tests and I finally understand.

cheers
 
William -
Best practice is not to do this:
[tab]Dim objMyInstance as New clsMyObject
because you have less control over when the object is created and destroyed. Try doing something like this for more robust code:
[tt]
Public Function MyFunc() as Boolean

Dim objMyInstance as clsMyObject

[tab]On Error Goto MyFunc_ErrHandler
[tab]MyFunc = True 'Assume failure

[tab]Set objMyInstance = New clsMyObject

[tab]'
[tab]' Use your instance here
[tab]'

[tab]MyFunc = False ' return success
[tab]Goto MyFunc_Cleanup

MyFunc_ErrHandler:
[tab]' Log your error. I'd write a common error logging
[tab]' routine to do this

[tab]' Fall through into Cleanup

MyFunc_Cleanup:
[tab]If Not objMyInstance is Nothing Then
[tab][tab]' call any close methods for objMyInstance here
[tab][tab]set objMyInstance = Nothing
[tab]End If

End Function
[/tt]

This is much better because you're explicitly creating and destroying the object instance. If you use the "Dim as New" style, if you access the object variable after setting it to Nothing, a new instance gets created for you by the VB runtime, and you have no control over the state (no methods are called which could set internal state), and you don't know what it's going to do. Especially in a COM+/MTS setting, since the Class_Initialize only gets called once there. Here's an example of this (bad) behavior:
[tt]
'''clsBankAccount
Public Balance as currency
Public Sub Deposit(DepositAmt as Currency)
[tab]Balance = Balance + DepositAmt
End Sub

''' Code that uses the bank account object
Public Sub WorkWithBA
Dim objBA as New clsBankAccount

objBA.Balance = 50 'Set initial balance
objBA.Deposit(100)
Set objBA = Nothing 'Accidentally set it to Nothing
objBA.Deposit(200)
End Sub
[/tt]

In this (contrived, I admit it) example, the Bank Account will have $200 in it before the subroutine ends, not the expected $350. When Deposit is called the second time, a new instance of clsBankAccount is created for you behind the scenes, and it's member variables (Balance) are set to their default values (if they have any), which is 0 for a currency.

Hope this helps, and good luck on the interviews.

Chip H.

 
Dim obj as New ...
does not create an instance of the object until there is a reference made to it. The VB Docs say
"Optional. Keyword that enables implicit creation of an object. If you use New when declaring the object variable, a new instance of the object is created on first reference to it, so you don't have to use the Set statement to assign the object reference."
A test of this proves the docs are right. With this test you get message "After Dim as New" before you get message "Class1 initialized". You do not get message "object is nothing" because the reference to obj causes it to be created. If the object is to be referenced many times, it is more efficient to use Set obj = New in order to eliminate the test for existence of the object before every property and method reference.
Code:
Private Sub command1_Click()
    Dim obj As New Class1
    MsgBox "After Dim as New"
    If obj Is Nothing Then
        MsgBox "object is nothing"
    End If
End Sub
....Class1
Option Explicit
Private Sub Class_Initialize()
    MsgBox "Class1 initialized"
End Sub
Compare Code (Text)
Generate Sort in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top