Link to home
Start Free TrialLog in
Avatar of mirghani
mirghani

asked on

User Defined Type

hi,
i'm getting this error while compiling my VB Project:-
'User-defined type not defined'
on the following function:-
Public Sub Instantiate_Object(SourceObj_Name As Object, DestObj_Name As Object)
  Set SourceObj_Name = New DestObj_Name
End Sub
the exact error at 'New DestObj_Name'
actually i want to call this procedure to instantiate an object e.g:-
Instantiate_Object obj_SRBO, SRBO.cSRBO
at general i'm declaring:
Dim obj_SRBO As SRBO.cSRBO
In brief this function will instantiate a dll depend on the parameter of object i'm passing .
Any help?
rgrds.
Meer.
ASKER CERTIFIED SOLUTION
Avatar of twalgrave
twalgrave

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tandrei
tandrei

replace:
>>Set SourceObj_Name = New DestObj_Name
with
Set SourceObj_Name = New Object
Set SourceObj_Name = DestObj_Name

You are receiving this error because you want some sort of a "copy constructor". BUT your variables are both objects, so you should instantiate a new object and only then assign it to the desired value.
When you do something like Set varName = New SomeType, VB expects there to be either a predefined type, either an user defined type, in your case it is a VB type (Object)
Avatar of Guy Hengel [angelIII / a3]
You need to pass the class string:

Public Sub Instantiate_Object(byref SourceObj_Name As Object, "DestObj_Name" As string)
 Set SourceObj_Name = CreateObject("DestObj_Name")
End Sub

and use it like this:
Instantiate_Object obj_SRBO, "SRBO.cSRBO"

CHeers
:Set SourceObj_Name = New Object
:Set SourceObj_Name = DestObj_Name

is a bit pointless, because the first line in effect does nothing at all as its action is overwritten by the second line, the above does:

Create a new object of "object"
Point SourceObj_Name to the new object
Point SourceObj_Name to DestObj_Name
New Object has lost all references so remove it.


Bit immaterial, because "DestObj_Name" is simply a variable name, not an object type which is what is required (see other answers).

If you don't want to pass the destobj as a string (as suggested), but actually want to create a new object based on the OBJECT passed, then use TypeName() with CreateObject(), as in (using original code).

Public Sub Instantiate_Object(SourceObj_Name As Object, DestObj_Name As Object)
 Set SourceObj_Name = CreateObject(TypeName(DestObj_Name))
End Sub


However, you normally copy a Source TO a destination (the original question has this the wrong way round), so the function would become:

Public Sub Instantiate_Object(SourceObj As Object, DestObj As Object)
 Set DestObj = CreateObject(TypeName(SourceObj))
End Sub

BUT, to use this, you would have already had to create the object to be copied into, which is pointless (see my previous comment), to use the above you would have to:

Dim DestObj as Object
Set DestObj = New Object
Instantiate_Object SourceObj, DestObj


So, if you have the Instantiate to a function, you get:

Dim DestObj as Object
Set DestObj = Instantiate_Object(SourceObj)

Public Function Instantiate_Object(SourceObj as Object) as Object
  Set Instantiate_Object = CreateObject(TypeName(SourceObj))
End Function



Of course, now that you know about CreateObject, your final code could be simply:


Dim DestObj as Object
Set DestObj = CreateObject(TypeName(SourceObj))




Avatar of mirghani

ASKER

hi,
i tried to pass the DestObj so it would be created,but unfortunately i got error '429'ActiveX can't create Object.!!!
Any help.
Meer.
hi,Sorry For Delay in replying for the above comments.
>>twalgrave
i didn't use GetObject is that may cause of getting the error i specified in my last comment?
rgrds
Meer.