User Defined Type

mirghani
mirghani used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You'll want to do a GetObject or CreateObject instead of a New.

GetObject([pathname] [, class])

The GetObject function syntax has thesenamed arguments:

Part Description
pathname Optional; Variant (String). The full path and name of the file containing the object to retrieve. If pathname is omitted, class is required.
class Optional; Variant (String). A string representing theclass of the object.


The classargument uses the syntax appname.objecttype and has these parts:

Part Description
appname Required; Variant (String). The name of the application providing the object.
objecttype Required; Variant (String). The type or class of object to create.


Remarks

Use the GetObject function to access an ActiveX object from a file and assign the object to anobject variable. Use the Set statement to assign the object returned by GetObject to the object variable. For example:

Dim CADObject As Object
Set CADObject = GetObject("C:\CAD\SCHEMA.CAD")

When this code is executed, the application associated with the specified pathname is started and the object in the specified file is activated.

If pathname is a zero-length string (""), GetObject returns a new object instance of the specified type. If the pathname argument is omitted, GetObject returns a currently active object of the specified type. If no object of the specified type exists, an error occurs.

Some applications allow you to activate part of a file. Add an exclamation point (!) to the end of the file name and follow it with a string that identifies the part of the file you want to activate. For information on how to create this string, see the documentation for the application that created the object.

For example, in a drawing application you might have multiple layers to a drawing stored in a file. You could use the following code to activate a layer within a drawing called SCHEMA.CAD:

Set LayerObject = GetObject("C:\CAD\SCHEMA.CAD!Layer3")

If you don't specify the object's class, Automation determines the application to start and the object to activate, based on the file name you provide. Some files, however, may support more than one class of object. For example, a drawing might support three different types of objects: an Application object, a Drawing object, and a Toolbar object, all of which are part of the same file. To specify which object in a file you want to activate, use the optional class argument. For example:

Dim MyObject As Object
Set MyObject = GetObject("C:\DRAWINGS\SAMPLE.DRW", "FIGMENT.DRAWING")

In the example, FIGMENT is the name of a drawing application and DRAWING is one of the object types it supports.

Once an object is activated, you reference it in code using the object variable you defined. In the preceding example, you accessproperties andmethods of the new object using the object variable MyObject. For example:

MyObject.Line 9, 90
MyObject.InsertText 9, 100, "Hello, world."
MyObject.SaveAs "C:\DRAWINGS\SAMPLE.DRW"

Note   Use the GetObject function when there is a current instance of the object or if you want to create the object with a file already loaded. If there is no current instance, and you don't want the object started with a file loaded, use the CreateObject function.

If an object has registered itself as a single-instance object, only one instance of the object is created, no matter how many times CreateObject is executed. With a single-instance object, GetObject always returns the same instance when called with the zero-length string ("") syntax, and it causes an error if the pathname argument is omitted. You can't use GetObject to obtain a reference to a class created with Visual Basic.

Commented:
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)
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
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
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
: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).

Commented:
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))




Author

Commented:
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.

Author

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial