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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Declare Variant or Object? 1

Status
Not open for further replies.

Brando32

Programmer
Jul 25, 2002
11
US
When using a pointer to point to a Form or a Class or a Module ... which is better to use .... a pointer declared as a Variant or as an Object ??? (And why?) Thanks!

(I originally posted this in the wrong area - my apologies)
 
variant means unkown or anything...

i.e. you could actually do this with a variant.
Code:
Dim var as Variant

var = 12982
Set var = Nothing

Set var = New Adodb.Recordset
Set var = Nothing

Set var = CreateObject("MyDLLName.ClassName")
'etc.

but if you know what you need the pointer/variable for, then the better way is to use the actual object.

i.e. Don't use object use the actual DLL.
Code:
'Bad Way
Dim obj As Object
Set obj = CreateObject("MyDLLName.ClassName")

'Good Way
Dim obj As MyDLLName.ClassName
Set obj = CreateObject("MyDLLName.ClassName")


Casper

There is room for all of gods creatures, "Right Beside the Mashed Potatoes".
 
Casper -

I am still new to this and have never written or referenced DLLs ... what I want to do is call code from Forms, Modules, and Classes generically ie ... have references to them passed as a parameter to a routine that will then use a that reference to call a routine of a pre-defined name from it. Does that make sense? I know that Variants and Objects are pretty generic ... but which would be better to set to say, a Class from within a project (not a DLL)? Thanks
 
To delare a class you have written it would be like this.
Code:
Private [ClassObjectName] As New [Classname]
 
how many clases? The best would be to do this...
Code:
Private Sub MyFunction(ByVal i_sClassName)
    
    Select Case i_sClassName
        Case "Class1"
            DIM obj as MyClasses.Class1
        Case "Class2"
            DIM obj as MyClasses.Class2
        Case "Class2"
            DIM obj as MyClasses.Class2
    End Select
    Set obj = CreateObject(i_sClassName)
    obj.DoSomething
    Set obj = Nothing
End Sub
but you could do this...
Code:
Private Sub MyFunction(ByVal i_sClassName)
    Dim obj as Object
    CallByName i_sClassName, "DoSomething", VbMethod    
End Sub


Casper

There is room for all of gods creatures, "Right Beside the Mashed Potatoes".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top