Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collection Problem

Status
Not open for further replies.

WJProctor

Programmer
Jun 20, 2003
151
GB
Hi there, ive just written a class which for arguments sake is a box, which contains items. I have filled the box with items and using checks i have been able to see the correct items are going in, but then when i go Box.Item(1).ToString it always gives the last item that i entered what ever index i put in. Any ideas why this might be happening and what im doing wrong??

Regards

JP

James Proctor

 
What kind of collection object are you using? Some of them don't think that the order of items in the collection are important, others do.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Just a standard VB.net collection. It doesnt matter though, any index i choose always returns the last item added. It must not be creating the list correctly or something. Anymore help would be great

Cheers

James Proctor

 
I don't know what type of collection the VB.NET compatability layer uses -- try picking one from System.Collections

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thought id post the code, than maybe someone can help me out. I hope some one can.

Public Class ClassKit

Inherits System.Collections.CollectionBase

Public Sub AddDrug(ByVal NewDrug As ClassDrug)
List.Add(NewDrug)
End Sub

Public Sub RemoveDrug(ByVal Index As Integer)
If Index > Count - 1 Or Index < 0 Then
MsgBox(&quot;Index Not Valid!&quot;)
Else
List.RemoveAt(Index)
End If
End Sub

Public Property Drug(ByVal Index As Integer) As ClassDrug
Get
Return CType(List.Item(Index), ClassDrug)
End Get
Set(ByVal Value As ClassDrug)
List.Item(Index) = Value
End Set
End Property

End Class

Public Class ClassDrug

Private Shared c_Name As String
Private Shared c_TradeName As String
Private Shared c_Strength As String
Private Shared c_Volume As String
Private Shared c_ID As Long
Private Shared c_BatchNumber As String
Private Shared c_ExpiryDate As Date

Public Shared Property Name() As String
Get
Return c_Name
End Get
Set(ByVal Value As String)
MsgBox(Value)
c_Name = Value
End Set
End Property

Public Shared Property TradeName() As String
Get
Return c_TradeName
End Get
Set(ByVal Value As String)
c_TradeName = Value
End Set
End Property

Public Shared Property Strength() As String
Get
Return c_Strength
End Get
Set(ByVal Value As String)
c_Strength = Value
End Set
End Property

Public Shared Property Volume() As String
Get
Return c_Volume
End Get
Set(ByVal Value As String)
c_Volume = Value
End Set
End Property

Public Shared Property ID() As Long
Get
Return c_ID
End Get
Set(ByVal Value As Long)
c_ID = Value
End Set
End Property

Public Shared Property BatchNumber() As String
Get
Return c_BatchNumber
End Get
Set(ByVal Value As String)
c_BatchNumber = Value
End Set
End Property

Public Shared Property ExpiryDate() As Date
Get
Return c_ExpiryDate
End Get
Set(ByVal Value As Date)
c_ExpiryDate = Value
End Set
End Property

Public Overrides Function ToString() As String
Dim TradeName As String
If Me.TradeName <> &quot;&quot; Then
TradeName = &quot; (&quot; & Trim(Me.TradeName) & &quot;)&quot;
Else
TradeName = Nothing
End If
Return Trim(Me.Name) & &quot;, &quot; & Trim(Me.Strength) & &quot;, &quot; & Trim(Me.Volume) & Trim(TradeName)
End Function

End Class

Sub ReadDrug
Dim NewKit As New KitClass
Dim NewDrug As DrugClass
NewDrug = New DrugClass
NeDrug.Name = &quot;Drug1&quot;
NewKit.Drug.Add(NewDrug)
NewDrug = New DrugClass
NewDrug.Name = &quot;Drug2&quot;
NewKit.Drug.Add(NewDrug)
NewDrug = New DrugClass
NewDrug.Name = &quot;Drug3&quot;
NewKit.Drug.Add(NewDrug)
MsgBox NewKit.Drug(1).ToString
End Sub

As i said if i loop through watching it load the class it seems to be adding the correct data, but then when i loop through to read the class back it always displays the last item added.

Thanks for your help.


James Proctor

 
Might I ask why you're trying to implement your own collection class for this instead of using, say, an ArrayList?

[tt]Sub ReadDrug
Dim NewKit As New ArrayList
Dim NewDrug As DrugClass
NewDrug = New DrugClass
NeDrug.Name = &quot;Drug1&quot;
NewKit.Add(NewDrug)
NewDrug = New DrugClass
NewDrug.Name = &quot;Drug2&quot;
NewKit.Add(NewDrug)
NewDrug = New DrugClass
NewDrug.Name = &quot;Drug3&quot;
NewKit.Add(NewDrug)
MsgBox CType(NewKit(1), DrugClass).ToString
End Sub[/tt]

I think that implementing a collection by inheriting from [tt]CollectionBase[/tt] requires more work than you're doing to get it right.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top