User-Defined Types and Classes 
Author Message
 User-Defined Types and Classes

Does anyone know if there's a way to declare a user-defined type as an
argument in a class method in VB6?


Wed, 14 Mar 2012 01:19:01 GMT  
 User-Defined Types and Classes

Quote:

> Does anyone know if there's a way to declare a user-defined type as an
> argument in a class method in VB6?

Unless this is a trick question, class methods are just functions as any
other...

--



Wed, 14 Mar 2012 01:58:10 GMT  
 User-Defined Types and Classes


Quote:
> Does anyone know if there's a way to declare a user-defined type as an
> argument in a class method in VB6?

Make sure the UDT is declared Public and change the class method attribute
to 'Friend'.

However, UDTs are a tad probelmatic in VB, as the above will work in most
cases, but awkward in others.

To manage packaged variables/iemts more "universally" convert your UDTs to a
class, or define them in a type library.

"Converting a User Defined Type to a Class Module"
http://www.*-*-*.com/

"TypeLib tutorial"
http://www.*-*-*.com/

"McKinney's WinAPI typelib download"
This package is useful as you can use and abuse his IDLs as examples for
creating your own.
http://www.*-*-*.com/ {*filter*}/html/sourcecode.htm
(download the WinTlbU.zip)

-ralph



Wed, 14 Mar 2012 02:24:42 GMT  
 User-Defined Types and Classes
Hi Ralph,

You correctly recognize the problem is about "Scope".

In order for the UDF to be seen by the Class, I had to define it in the
Class.  Unfortunately then it is not seen in the main program, and if defined
in the main program, its not available to the Class.

I was hoping to coerce the Class-method argument, declared as a Variant, but
when I define the UFD in the main, it produces the error:

"Only user-defined types defined in public object modules can be coerced to
or from a Variant or passed to late-bound functions"

That error suggests that the Variant could be coerced, if I could declare
the main module public, but I haven't figured out how to do that.

X.

Quote:



> > Does anyone know if there's a way to declare a user-defined type as an
> > argument in a class method in VB6?

> Make sure the UDT is declared Public and change the class method attribute
> to 'Friend'.

> However, UDTs are a tad probelmatic in VB, as the above will work in most
> cases, but awkward in others.

> To manage packaged variables/iemts more "universally" convert your UDTs to a
> class, or define them in a type library.

> "Converting a User Defined Type to a Class Module"
> http://www.*-*-*.com/

> "TypeLib tutorial"
> http://www.*-*-*.com/

> "McKinney's WinAPI typelib download"
> This package is useful as you can use and abuse his IDLs as examples for
> creating your own.
> http://www.*-*-*.com/ {*filter*}/html/sourcecode.htm
> (download the WinTlbU.zip)

> -ralph



Wed, 14 Mar 2012 03:35:01 GMT  
 User-Defined Types and Classes
Hi Ralph,

I tried your suggestion of setting the UFD (in the Class) to Public, and
then setting the Class-method to "Friend".  I still get to following error:

"Cannot define a Public user-defined type within a private object module"

X.

Quote:



> > Does anyone know if there's a way to declare a user-defined type as an
> > argument in a class method in VB6?

> Make sure the UDT is declared Public and change the class method attribute
> to 'Friend'.

> However, UDTs are a tad probelmatic in VB, as the above will work in most
> cases, but awkward in others.

> To manage packaged variables/iemts more "universally" convert your UDTs to a
> class, or define them in a type library.

> "Converting a User Defined Type to a Class Module"
> http://www.*-*-*.com/

> "TypeLib tutorial"
> http://www.*-*-*.com/

> "McKinney's WinAPI typelib download"
> This package is useful as you can use and abuse his IDLs as examples for
> creating your own.
> http://www.*-*-*.com/ {*filter*}/html/sourcecode.htm
> (download the WinTlbU.zip)

> -ralph



Wed, 14 Mar 2012 04:03:02 GMT  
 User-Defined Types and Classes
Hi Ralph,

I got it working.  Apparently I missed a combination.

I created a separate standard-module for the main code; this allowed me to
declare the UDT as "Public" [with out getting the afore mentioned compilation
error], and that made it available to the Class-module.

Thanks for your help it's much appreciated!

X.

Quote:



> > Does anyone know if there's a way to declare a user-defined type as an
> > argument in a class method in VB6?

> Make sure the UDT is declared Public and change the class method attribute
> to 'Friend'.

> However, UDTs are a tad probelmatic in VB, as the above will work in most
> cases, but awkward in others.

> To manage packaged variables/iemts more "universally" convert your UDTs to a
> class, or define them in a type library.

> "Converting a User Defined Type to a Class Module"
> http://www.*-*-*.com/

> "TypeLib tutorial"
> http://www.*-*-*.com/

> "McKinney's WinAPI typelib download"
> This package is useful as you can use and abuse his IDLs as examples for
> creating your own.
> http://www.*-*-*.com/ {*filter*}/html/sourcecode.htm
> (download the WinTlbU.zip)

> -ralph



Wed, 14 Mar 2012 04:17:01 GMT  
 User-Defined Types and Classes
xytsrm escribi:

Quote:
> In order for the UDF to be seen by the Class, I had to define it in the
> Class.  Unfortunately then it is not seen in the main program, and if defined
> in the main program, its not available to the Class.

1) You can define it in both projects or...
2) Define it in a class that its Instancing property is set to
GlobalMultiUse

Quote:
> I was hoping to coerce the Class-method argument, declared as a Variant, but
> when I define the UFD in the main, it produces the error:

> "Only user-defined types defined in public object modules can be coerced to
> or from a Variant or passed to late-bound functions"

You can't just pass the UDT, but you can change from:

Public Function TheFunction (nMyUDT as MY_UDT) as Variant

to

Public Function TheFunction (nMyUDTPtr as Long) as Variant
     Dim iMyUDT as MY_UDT

     CopyMemoryRead iMyUDT, nMyUDTPtr, LenB(iMyUDT)
     ' You have teh UDT in iMyUDT

And you call The function:

     Dim iReturnValue as Variant
     Dim iMyUDTPtr as Long

     ' You need to have the UDT in mTheUDTHere
     iMyUDTPtr = VarPtr (mTheUDTHere)
     iReturnValue = TheFunction (iMyUDTPtr)

' API declaration:
Declare Sub CopyMemoryRead Lib "kernel32" Alias _
     "RtlMoveMemory" (Destination As Any, ByVal _
     Source As Long, ByVal Length As Long)

Quote:
> That error suggests that the Variant could be coerced, if I could declare
> the main module public, but I haven't figured out how to do that.

I don't understand what the error is traying to say, but I think it's
not possible.


Wed, 14 Mar 2012 04:55:55 GMT  
 User-Defined Types and Classes
xytsrm escribi:

Quote:
> Hi Ralph,

> I got it working.  Apparently I missed a combination.

> I created a separate standard-module for the main code; this allowed me to
> declare the UDT as "Public" [with out getting the afore mentioned compilation
> error], and that made it available to the Class-module.

But do you have all in the same project, or is this an issue of passing
the UDT from one project to another?


Wed, 14 Mar 2012 04:58:25 GMT  
 User-Defined Types and Classes
It was all in the same project.
Quote:

> xytsrm escribi:
> > Hi Ralph,

> > I got it working.  Apparently I missed a combination.

> > I created a separate standard-module for the main code; this allowed me to
> > declare the UDT as "Public" [with out getting the afore mentioned compilation
> > error], and that made it available to the Class-module.

> But do you have all in the same project, or is this an issue of passing
> the UDT from one project to another?



Wed, 14 Mar 2012 05:31:01 GMT  
 User-Defined Types and Classes


Quote:
> Does anyone know if there's a way to declare a user-defined type as an
> argument in a class method in VB6?

Using UDTs in classes usually doesn't bode well and can be fraught with
problems.

The usual "best" solution is to convert the UDT to a class of its own.

--
Mike



Wed, 14 Mar 2012 08:09:38 GMT  
 User-Defined Types and Classes


Quote:
> Hi Ralph,

> I got it working.  Apparently I missed a combination.

> I created a separate standard-module for the main code; this allowed me to
> declare the UDT as "Public" [with out getting the afore mentioned
compilation
> error], and that made it available to the Class-module.

I should have been more clear of where to define the UDT. Perhaps I should
have used the term "Global". "Global" was essentially deprecated with the
introduction of Public/Private, but is still useful to describe items in bas
modules that you expect to be "in Scope" through out a project.

You have what you need now, but for completeness it should be noted that it
is possible to work with UDTs using a Variant. Here is one way to go about
that ...
http://www.brainbell.com/tutors/Visual_Basic/User_Defined_Types.htm

But pound for pound IMHO a typlib is the best way to go.

Also consider that a UDT by its very nature often forces a data-centric
solution. More often than not the association of various elements into a
single package to be treated as a single 'type' is in fact an object, thus
defining them as such leads to a better object-centric or object oriented
solution.

-ralph



Wed, 14 Mar 2012 11:28:56 GMT  
 User-Defined Types and Classes
xytsrm escribi:

Quote:
> It was all in the same project.

There is no problem then, you just need to declare the UDT in a module,
as public.


Wed, 14 Mar 2012 12:25:49 GMT  
 User-Defined Types and Classes
Guys,

Many thanks for all your suggestions - I'm going to focus on converting the
UDT to a Class.

X.

Quote:

> Does anyone know if there's a way to declare a user-defined type as an
> argument in a class method in VB6?



Thu, 15 Mar 2012 08:04:01 GMT  
 User-Defined Types and Classes
Ralph & MikeD,

It worked, instead of using a UDT I defined a simple class module with the
same data types as the UDT and it's visible to both the main program and
another class.

The only downside appears that for each, let's call it a virtual-UDT, I need
a separate class module - perhaps this is where Class-Collections might come
in handy.

X.

Quote:

> Guys,

> Many thanks for all your suggestions - I'm going to focus on converting the
> UDT to a Class.

> X.


> > Does anyone know if there's a way to declare a user-defined type as an
> > argument in a class method in VB6?



Thu, 15 Mar 2012 23:43:01 GMT  
 User-Defined Types and Classes


Quote:
> Ralph & MikeD,

> It worked, instead of using a UDT I defined a simple class module with the
> same data types as the UDT and it's visible to both the main program and
> another class.

> The only downside appears that for each, let's call it a virtual-UDT, I
> need
> a separate class module - perhaps this is where Class-Collections might
> come
> in handy.

No, let's not call it a virtual-UDT.  That'll just confuse things.  It's a
class, so let's call it a class.

Not exactly sure what you mean. You can create as many instances of a class
as you wish.  Typically, you use a collection if you create multiple
instances. Otherwise, you'd need to use distinct variables for each
instance.

If you were originally talking about 2 or more different UDTs, then yes,
you'd need a separate class module for each of those UDTs. But I don't see
how that's really any different.  If you had 3 UDTs before, you'd simply
have 3 classes.

--
Mike



Fri, 16 Mar 2012 07:20:45 GMT  
 
 [ 22 post ]  Go to page: [1] [2]

 Relevant Pages 

1. User-Defined types in Class Libraries

2. prob w var user defined type as class mem ( prob easy for an exp VB OO)

3. Public user-defined types and classes

4. Passing User Define Types to Classes

5. User defined types in classes?

6. Passing Classes to other classes (using user defined types)

7. User Control - User-defined type not defined

8. Classes, Collections, and User Defined Types

9. Class and User Defined Types

10. Error using user-defined type in parameter of public procedure in a class module

11. Using public user defined types in a class

12. Class Module / User Defined Type

 

 
Powered by phpBB® Forum Software