For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > September 2004 > Problem to add items to collection









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem to add items to collection
Kjell

2004-09-01, 8:55 pm

Hi

I'm having a problem to add a user defined type to a collection, can this
not be done ? anyway this is how it looks right now:

In a Module:

Public Type MyPortType
sIP As String
iNo As Integer
End Type

Public PortColl As New Collection
Public xPort As MyPortType

Function TestAddone(ByRef CollItem As MyPortType, ByRef CollKey As String)
As Long
PortColl.Add Item:=CollItem, Key:=CollKey
TestAddone = PortColl.Count
End Function


In a Form:

Private Sub Form1_Load()
Dim X As Long, Y As Long
Dim sKey As String

For Y = 1 To 10
xPort.sIP = "192.168.1.1"
xPort.iNo = Y
sKey = "Port" & Y
X = TestAddone(xPort, sKey)
Next
End Sub

and this is the error message, apparantly raised at line: PortColl.Add
Item:=CollItem, Key:=CollKey

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

Thanks Kjell
Ken Halter

2004-09-01, 8:55 pm

Kjell wrote:
> Hi
>
> I'm having a problem to add a user defined type to a collection, can this
> not be done ? anyway this is how it looks right now:
>
> In a Module:
>
> Public Type MyPortType
> sIP As String
> iNo As Integer
> End Type
>


You can convert that to a "Collection Friendly" class in a matter of
seconds....

Converting a User Defined Type to a Class Module
http://www.khcshome.com/UDT_Class.htm

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep all discussions in the groups..
Kjell

2004-09-01, 8:55 pm

Thanks Ken


Maybe you or someone else can suggest a better way than using a user-defined
type at all

I need a collection of items, like a multi dim array but I've been working a
lot with arrays and heard from more than a few ppl that I should consider
using a collection, the problem is that I cannot seem to make the collection
multi dim like.
(holding several data pieces per element)

Kjell
Ken Halter

2004-09-02, 3:55 pm

Kjell wrote:
> Thanks Ken
>
>
> Maybe you or someone else can suggest a better way than using a user-defined
> type at all


if you refer to the link, you'll see how easy it is to dump those UDTs..

Your UDT....

Public Type MyPortType
sIP As String
iNo As Integer
End Type

....converts to this 3 line class (named clsMyPortType if you want)...

Option Explicit
Public sIP as String
Public iNo as Integer


> I need a collection of items, like a multi dim array but I've been working a
> lot with arrays and heard from more than a few ppl that I should consider
> using a collection, the problem is that I cannot seem to make the collection
> multi dim like.
> (holding several data pieces per element)


See the link.. it shows exactly that... collecting classes (instead of UDTs)

> Kjell


--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep all discussions in the groups..
Tim Baur

2004-09-02, 3:55 pm

Kjell,

Along with Ken's link you may also want to look at the Microsoft series
of articles on the topic of custom collections.

Building The House of Straw:
http://msdn.microsoft.com/library/en-
us/vbcon98/html/ vbconpubliccollectionexamplethehouseofst
raw.asp

Building The House of Sticks:
http://msdn.microsoft.com/library/en-
us/vbcon98/html/ vbconprivatecollectionexamplethehouseofs
ticks.asp

Building The House of Bricks:
http://msdn.microsoft.com/library/en-
us/vbcon98/html/ vbconcreatingyourownclasscollectiontheho
useofbricks.asp

The Benefits of Good Object Oriented Design:
http://msdn.microsoft.com/library/en-
us/vbcon98/html/ vbconthebenefitsofgoodobjectorienteddesi
gn.asp



"=?Utf-8?B?S2plbGw=?=" <Kjell@discussions.microsoft.com> wrote in
news:8BEE37B6-133F-4629-9452-000C50E96A42@microsoft.com:

> Thanks Ken
>
>
> Maybe you or someone else can suggest a better way than using a
> user-defined type at all
>
> I need a collection of items, like a multi dim array but I've been
> working a lot with arrays and heard from more than a few ppl that I
> should consider using a collection, the problem is that I cannot seem
> to make the collection multi dim like.
> (holding several data pieces per element)
>
> Kjell


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com