Can't get a working collection!

T

Thief_

I want to store data pertaining to a widget in a collection. The widget has
many pproperties, so i created the following class and called it
"clsWatchProperties":

Public Class clsWatchProperties

Private mTopicID As Integer

Private mLastPos As Integer

Private mThisPos As Integer

Private mCancelThisItem As Boolean

Private mHasNewReply As Boolean

Public Property TopicID() As Integer

Get

TopicID = mTopicID

End Get

Set(ByVal Value As Integer)

mTopicID = Value

End Set

End Property

Public Property LastPos() As Integer

Get

LastPos = mLastPos

End Get

Set(ByVal Value As Integer)

mLastPos = Value

End Set

End Property

Public Property ThisPos() As Integer

Get

ThisPos = mThisPos

End Get

Set(ByVal Value As Integer)

mThisPos = Value

End Set

End Property

Public Property CancelThisItem() As Boolean

Get

CancelThisItem = mCancelThisItem

End Get

Set(ByVal Value As Boolean)

mCancelThisItem = Value

End Set

End Property

Public Property HasNewReply() As Boolean

Get

HasNewReply = mHasNewReply

End Get

Set(ByVal Value As Boolean)

mHasNewReply = Value

End Set

End Property

End Class



I then (attempted to) create the collection using the following code:

Private WatchedTopics As New Collection

------

Sub Test

....

Dim WatchTopic As New clsWatchProperties

GetData = CleanString(mLink.ToString, CleanupHTML.ID) ' Retrieves
a 5 digit number which is in a string format

Debug.WriteLine("ID: " & GetData)

If Not FindIfExists(GetData, RowCounter) Then ' This line simply
checks if the object exists in the collection. True is returned if it does.

WatchTopic.TopicID = GetData

WatchTopic.LastPos = RowCounter

WatchTopic.HasNewReply = False

WatchedTopics.Add(WatchTopic,
WatchTopic.TopicID.ToString)

ThisItemIsUpdated = True

End If

If WatchedTopics.Item(GetData).HasNewReply Then

........

End If

I want my collection to look like this:

WatchedTopics("01234").LastPos = 100
WatchedTopics("04321").HasNewReply = False
WatchedTopics("09876").CancelThisItem = True

What I'm ACTUALLY getting is:

- WatchedTopics {Length=1} Microsoft.VisualBasic.Collection
(0) "Empty placeholder to adjust for 1 based array" String
- (1) {Microsoft.VisualBasic.Collection.KeyValuePair}
Microsoft.VisualBasic.Collection.KeyValuePair
- Key "107348" {String} Object
String "107348" String
- Value {rnsVBCityForumListener.clsWatchProperties} Object
- [rnsVBCityForumListener.clsWatchProperties]
{rnsVBCityForumListener.clsWatchProperties}
rnsVBCityForumListener.clsWatchProperties
CancelThisItem False Boolean
HasNewReply False Boolean
LastPos 1 Integer
mCancelThisItem False Boolean
mHasNewReply False Boolean
mLastPos 1 Integer
mThisPos 0 Integer
mTopicID 107348 Integer
ThisPos 0 Integer
TopicID 107348 Integer

(This is a copy and paste of the WatchTopics variable and its structure as
seen in the Watch window)


I think I have stuffed up in my code somewhere but can't figger out where.
Please help me.
 
C

Cor Ligthert [MVP]

Thief,

Unlucky enough is the Visual.Basic collection not placed in the VisualBasic
compatible namespace however in the normal namespace. This means that it is
still active. However it is not the nicest collection to use, by instance
does it starts at First as indexer and gives therefore a lot of
complications. This is probably one of the reason that it is seldom used by
visitors/repliers from this newsgroup.

Have a look at all the collections that implement Ilist or/and Icollection
or those that are in System.Data

http://msdn.microsoft.com/library/d...lrfsystemcollectionsicollectionclasstopic.asp

As a second advice. Past in your samples first in a notebook copy them back
and than again in a message. You will see that you get more good answers
because your problems become readable.

I hope this helps,

Cor
 
K

Ken Tucker [MVP]

Hi,

You might be better off using a hashtable instead of a
collection.

http://msdn.microsoft.com/library/d...frlrfsystemcollectionshashtableclasstopic.asp

Ken
-----------------
I want to store data pertaining to a widget in a collection. The widget has
many pproperties, so i created the following class and called it
"clsWatchProperties":

Public Class clsWatchProperties

Private mTopicID As Integer

Private mLastPos As Integer

Private mThisPos As Integer

Private mCancelThisItem As Boolean

Private mHasNewReply As Boolean

Public Property TopicID() As Integer

Get

TopicID = mTopicID

End Get

Set(ByVal Value As Integer)

mTopicID = Value

End Set

End Property

Public Property LastPos() As Integer

Get

LastPos = mLastPos

End Get

Set(ByVal Value As Integer)

mLastPos = Value

End Set

End Property

Public Property ThisPos() As Integer

Get

ThisPos = mThisPos

End Get

Set(ByVal Value As Integer)

mThisPos = Value

End Set

End Property

Public Property CancelThisItem() As Boolean

Get

CancelThisItem = mCancelThisItem

End Get

Set(ByVal Value As Boolean)

mCancelThisItem = Value

End Set

End Property

Public Property HasNewReply() As Boolean

Get

HasNewReply = mHasNewReply

End Get

Set(ByVal Value As Boolean)

mHasNewReply = Value

End Set

End Property

End Class



I then (attempted to) create the collection using the following code:

Private WatchedTopics As New Collection

------

Sub Test

....

Dim WatchTopic As New clsWatchProperties

GetData = CleanString(mLink.ToString, CleanupHTML.ID) ' Retrieves
a 5 digit number which is in a string format

Debug.WriteLine("ID: " & GetData)

If Not FindIfExists(GetData, RowCounter) Then ' This line simply
checks if the object exists in the collection. True is returned if it does.

WatchTopic.TopicID = GetData

WatchTopic.LastPos = RowCounter

WatchTopic.HasNewReply = False

WatchedTopics.Add(WatchTopic,
WatchTopic.TopicID.ToString)

ThisItemIsUpdated = True

End If

If WatchedTopics.Item(GetData).HasNewReply Then

........

End If

I want my collection to look like this:

WatchedTopics("01234").LastPos = 100
WatchedTopics("04321").HasNewReply = False
WatchedTopics("09876").CancelThisItem = True

What I'm ACTUALLY getting is:

- WatchedTopics {Length=1} Microsoft.VisualBasic.Collection
(0) "Empty placeholder to adjust for 1 based array" String
- (1) {Microsoft.VisualBasic.Collection.KeyValuePair}
Microsoft.VisualBasic.Collection.KeyValuePair
- Key "107348" {String} Object
String "107348" String
- Value {rnsVBCityForumListener.clsWatchProperties} Object
- [rnsVBCityForumListener.clsWatchProperties]
{rnsVBCityForumListener.clsWatchProperties}
rnsVBCityForumListener.clsWatchProperties
CancelThisItem False Boolean
HasNewReply False Boolean
LastPos 1 Integer
mCancelThisItem False Boolean
mHasNewReply False Boolean
mLastPos 1 Integer
mThisPos 0 Integer
mTopicID 107348 Integer
ThisPos 0 Integer
TopicID 107348 Integer

(This is a copy and paste of the WatchTopics variable and its structure as
seen in the Watch window)


I think I have stuffed up in my code somewhere but can't figger out where.
Please help me.
 
G

Guest

Below is the best example that I've seen on using a collection class...I got
it from someone on this newsgroup:

Public Class rectangle_Collection
Inherits CollectionBase
#Region "Property interface"
Public Shadows Property item(ByVal iIndex As Integer) As Rectangle
Get
If ((Count = 0) OrElse ((iIndex < 0) And (iIndex > (Count -
1)))) Then
Return (Nothing)
Else
Return (CType(innerlist.Item(iIndex), Rectangle))
End if
End Get
Set(ByVal Value As Rectangle)
If ((Count = 0) OrElse ((iIndex < 0) And (iIndex > (Count -
1)))) Then
Return
Else
innerlist.Item(iIndex) = Value
End If
End Set
End Property

#End Region

#Region "Public methods"

Public Function add(ByVal iRectangle As Rectangle) As Integer
Return innerlist.Add(iRectangle)
End Function

Public Sub addRange(ByVal iRectangle() As Rectangle)
Call innerlist.AddRange(iRectangle)
End Sub

Public Sub addRange(ByVal iRectangleCollection As rectangle_Collection)
Call innerlist.AddRange(iRectangleCollection)
End Sub

Public Function contains(ByVal iRectangle As Rectangle) As Boolean
Return (innerlist.Contains(iRectangle))
End Function

Public Function indexOf(ByVal iRectangle As Rectangle) As Integer
Return (innerlist.IndexOf(iRectangle))
End Function

Public Sub insert(ByVal iIndex As Integer, ByVal iRectangle As
Rectangle)
Call innerlist.Insert(iIndex, iRectangle)
End Sub

Public Sub insertRange(ByVal iIndex As Integer, ByVal iRectangle() As
Rectangle)
Call innerlist.InsertRange(iIndex, iRectangle)
End Sub

Public Sub insertRange(ByVal iIndex As Integer, ByVal
iRectangleCollection As rectangle_Collection)
Call innerlist.InsertRange(iIndex, iRectangleCollection)
End Sub

Public Function lastIndexOf(ByVal iRectangle As Rectangle) As Integer
Return (innerlist.LastIndexOf(iRectangle))
End Function

Public Sub remove(ByVal iRectangle As Rectangle)
Call innerlist.Remove(iRectangle)
End Sub

Public Sub Sort()
MyBase.InnerList.Sort()
End Sub

NOTE: To Trim the Collection:
Public Sub TrimResults(ByVal iElementsToKeep As Integer)
Dim myArray(iElementsToKeep-1) As clsMyClass
MyBase.InnerList.CopyTo(0, myArray, 0, iElementsToKeep)
MyBase.InnerList.Clear()
MyBase.InnerList.SetRange(0, myArray)
End Sub

#End Region
End Class

--
Dennis in Houston


Ken Tucker said:
Hi,

You might be better off using a hashtable instead of a
collection.

http://msdn.microsoft.com/library/d...frlrfsystemcollectionshashtableclasstopic.asp

Ken
-----------------
I want to store data pertaining to a widget in a collection. The widget has
many pproperties, so i created the following class and called it
"clsWatchProperties":

Public Class clsWatchProperties

Private mTopicID As Integer

Private mLastPos As Integer

Private mThisPos As Integer

Private mCancelThisItem As Boolean

Private mHasNewReply As Boolean

Public Property TopicID() As Integer

Get

TopicID = mTopicID

End Get

Set(ByVal Value As Integer)

mTopicID = Value

End Set

End Property

Public Property LastPos() As Integer

Get

LastPos = mLastPos

End Get

Set(ByVal Value As Integer)

mLastPos = Value

End Set

End Property

Public Property ThisPos() As Integer

Get

ThisPos = mThisPos

End Get

Set(ByVal Value As Integer)

mThisPos = Value

End Set

End Property

Public Property CancelThisItem() As Boolean

Get

CancelThisItem = mCancelThisItem

End Get

Set(ByVal Value As Boolean)

mCancelThisItem = Value

End Set

End Property

Public Property HasNewReply() As Boolean

Get

HasNewReply = mHasNewReply

End Get

Set(ByVal Value As Boolean)

mHasNewReply = Value

End Set

End Property

End Class



I then (attempted to) create the collection using the following code:

Private WatchedTopics As New Collection

------

Sub Test

....

Dim WatchTopic As New clsWatchProperties

GetData = CleanString(mLink.ToString, CleanupHTML.ID) ' Retrieves
a 5 digit number which is in a string format

Debug.WriteLine("ID: " & GetData)

If Not FindIfExists(GetData, RowCounter) Then ' This line simply
checks if the object exists in the collection. True is returned if it does.

WatchTopic.TopicID = GetData

WatchTopic.LastPos = RowCounter

WatchTopic.HasNewReply = False

WatchedTopics.Add(WatchTopic,
WatchTopic.TopicID.ToString)

ThisItemIsUpdated = True

End If

If WatchedTopics.Item(GetData).HasNewReply Then

........

End If

I want my collection to look like this:

WatchedTopics("01234").LastPos = 100
WatchedTopics("04321").HasNewReply = False
WatchedTopics("09876").CancelThisItem = True

What I'm ACTUALLY getting is:

- WatchedTopics {Length=1} Microsoft.VisualBasic.Collection
(0) "Empty placeholder to adjust for 1 based array" String
- (1) {Microsoft.VisualBasic.Collection.KeyValuePair}
Microsoft.VisualBasic.Collection.KeyValuePair
- Key "107348" {String} Object
String "107348" String
- Value {rnsVBCityForumListener.clsWatchProperties} Object
- [rnsVBCityForumListener.clsWatchProperties]
{rnsVBCityForumListener.clsWatchProperties}
rnsVBCityForumListener.clsWatchProperties
CancelThisItem False Boolean
HasNewReply False Boolean
LastPos 1 Integer
mCancelThisItem False Boolean
mHasNewReply False Boolean
mLastPos 1 Integer
mThisPos 0 Integer
mTopicID 107348 Integer
ThisPos 0 Integer
TopicID 107348 Integer

(This is a copy and paste of the WatchTopics variable and its structure as
seen in the Watch window)


I think I have stuffed up in my code somewhere but can't figger out where.
Please help me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top