UDT and collection 
Author Message
 UDT and collection

I am trying to add a UDT to a collection.
UDT is declared as public in a module.
But VB6 throws me an error saying that this only works with public
declared UDTs.
I don't understand this because my UDT is declared public.
Or do I really have to use a VarPtr?????????
Thank you.

Option Explicit

Public Type Foo
     myString As String
     myNumber As Long
End Type

Public Sub Main()

     Dim c As Collection
     Set c = New Collection

     Dim f As Foo
     f.myString = "Some text"
     f.myNumber = 4

     c.Add f, "myfoo"

End Sub



Tue, 14 Feb 2012 18:15:26 GMT  
 UDT and collection
Jim Meyers escribi:

Quote:
> I am trying to add a UDT to a collection.
> UDT is declared as public in a module.
> But VB6 throws me an error saying that this only works with public
> declared UDTs.
> I don't understand this because my UDT is declared public.
> Or do I really have to use a VarPtr?????????
> Thank you.

> Option Explicit

> Public Type Foo
>     myString As String
>     myNumber As Long
> End Type

> Public Sub Main()

>     Dim c As Collection
>     Set c = New Collection

>     Dim f As Foo
>     f.myString = "Some text"
>     f.myNumber = 4

>     c.Add f, "myfoo"

> End Sub

I think you can't add an UDT to a collection, why don't you use a class
instead?


Tue, 14 Feb 2012 18:43:04 GMT  
 UDT and collection
Eduardo,

would each var (which would normally be added by saying .Add fMyVar,
"MyVarName") be representend by a new class or would you make an "Add"
method in the class and store all new vars within the class?
I mean would you make the class look like a collection or would you make
an array of classes?

Quote:

> Jim Meyers escribi:
>> I am trying to add a UDT to a collection.
>> UDT is declared as public in a module.
>> But VB6 throws me an error saying that this only works with public
>> declared UDTs.
>> I don't understand this because my UDT is declared public.
>> Or do I really have to use a VarPtr?????????
>> Thank you.

>> Option Explicit

>> Public Type Foo
>>     myString As String
>>     myNumber As Long
>> End Type

>> Public Sub Main()

>>     Dim c As Collection
>>     Set c = New Collection

>>     Dim f As Foo
>>     f.myString = "Some text"
>>     f.myNumber = 4

>>     c.Add f, "myfoo"

>> End Sub

> I think you can't add an UDT to a collection, why don't you use a class
> instead?



Tue, 14 Feb 2012 19:19:41 GMT  
 UDT and collection

Quote:

> I am trying to add a UDT to a collection.

Short answer - can't be done.

Types are not "self-describing" so even if you /could/ get one into a
Collection you wouldn't be able to "do" anything meaningful with it when
you got it back out again.  All the fields you define within the Type
are compiled to memory offsets within the space occupied by your Type;
"clients" of your collection can't tell what sort of thing they've just
retrieved or what any of those offsets might be.

On the other hand, Classes /are/ "self-describing" and /can/ be added to
Collections and, IMHO, generally cause far /less/ problems than Types,
once you get over the initial learning curve.  The fields (and
properties and methods and, indeed the type of the Type itself) are
mapped in such a way that clients of your collection can use them.

Here's my suggestion:

[Foo.cls]
' Here's a Class that replaces your Type

Public Text as String
Public Number as Long

[Module1.bas]
' VB doesn't support passing arguments to constructors
'   (Class_Initialize), so here's a function that simulates acts
'   like a parametrised constructor.

Public Function New_Foo( _
   byVal sText as String _
, byVal lNumber as Long _
) As Foo

    Dim result as Foo
    Set result = New Foo

    result.Text = sText
    result.Number = lNumber

    Set New_Foo = result
End Function

Public Sub Main()

    Dim c As Collection
    Set c = New Collection

    c.Add New_Foo( "some text", 4 )

End Sub

HTH,
    Phill  W.



Tue, 14 Feb 2012 19:29:40 GMT  
 UDT and collection

Quote:
> Eduardo,

> would each var (which would normally be added by saying .Add fMyVar,
> "MyVarName") be representend by a new class or would you make an "Add"
> method in the class and store all new vars within the class?
> I mean would you make the class look like a collection or would you make
> an array of classes?

Search MSDN by title only for "Creating Your Own Collection Classes". Here
is the online version:

Creating Your Own Collection Classes
http://msdn.microsoft.com/en-us/library/aa262340(VS.60).aspx



Tue, 14 Feb 2012 19:31:11 GMT  
 UDT and collection


Quote:
> Public Function New_Foo( _
>    byVal sText as String _
> , byVal lNumber as Long _
> ) As Foo

>     Dim result as Foo
>     Set result = New Foo

>     result.Text = sText
>     result.Number = lNumber

>     Set New_Foo = result
> End Function

Suggestion 2, something with a little less shuffle...

Public Function New_Foo2(ByVal Text As String, ByVal Number As Long) As Foo
  Set New_Foo2 = New Foo
  With New_Foo2
     .Text = Text
     .Number = Number
  End With
End Function

You too, can Foo2...
<g>
LFS



Tue, 14 Feb 2012 19:50:26 GMT  
 UDT and collection
Nobody,
this MSDN sample is really great! Thanks a lot!
Quote:



>> Eduardo,

>> would each var (which would normally be added by saying .Add fMyVar,
>> "MyVarName") be representend by a new class or would you make an "Add"
>> method in the class and store all new vars within the class?
>> I mean would you make the class look like a collection or would you make
>> an array of classes?

> Search MSDN by title only for "Creating Your Own Collection Classes". Here
> is the online version:

> Creating Your Own Collection Classes
> http://msdn.microsoft.com/en-us/library/aa262340(VS.60).aspx



Tue, 14 Feb 2012 20:33:42 GMT  
 UDT and collection


Quote:


> > Eduardo,

> > would each var (which would normally be added by saying .Add fMyVar,
> > "MyVarName") be representend by a new class or would you make an "Add"
> > method in the class and store all new vars within the class?
> > I mean would you make the class look like a collection or would you make
> > an array of classes?

> Search MSDN by title only for "Creating Your Own Collection Classes". Here
> is the online version:

> Creating Your Own Collection Classes
> http://msdn.microsoft.com/en-us/library/aa262340(VS.60).aspx

In those cases where you just have to store UDTs in a VB Collection -
perhaps because of some expansion to legacy code - there is an ugly
work-around. <g>

Create a type library containing the definition of the UDT (as a struct).
Not strickly necessary but helps to maintain consistency and sanity.

Create a class that duplicates your UDT.

Use the VB Class Builder to create a collection class based on the mimic
class.

Go into the collection class and modify the public members to receive and
return a UDT. Within the implementation of these members you will convert
the UDT to a Class for storage, and back again.

Hey, I said it was ugly.

Frankly, what I would do is simply mimic the Interface of a collection class
with a class implementing a custom storage for the UDTs.

-ralph



Tue, 14 Feb 2012 20:49:25 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Sending a UDT OR Collections/Classes through Winsock

2. Passing UDT to collection headaches

3. Problem adding UDT to Collection

4. type/collection/udt

5. Passing UDT to a collection

6. Create collection of UDT

7. collections with UDT

8. Collection objs and UDT

9. storing UDT's in collection - can u ?

10. collection of udt

11. Collection of UDT type variables

12. How to return UDT's in a collection from a function

 

 
Powered by phpBB® Forum Software