473,379 Members | 1,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Collection Error

HI ive just created my first collection. For arguments sake lets say it is a box with many items in it. I fill the box with items. But then when i come to look in the box, i.e. Box.Item(0).ToString it always returns the last item i put in the box, no matter which index i ask it to look in. I realise it must be something daft im doing. Could someone please help me out.

Regards

James

P.S. I didnt put any code with this to keep it tidy, but if that would help then i can post it up.
Nov 20 '05 #1
8 906
* "=?Utf-8?B?SmFtZXMgUHJvY3Rvcg==?=" <ja**********@ebmis.co.uk> scripsit:
HI ive just created my first collection. For arguments sake lets say
it is a box with many items in it. I fill the box with items. But then
when i come to look in the box, i.e. Box.Item(0).ToString it always
returns the last item i put in the box, no matter which index i ask it
to look in. I realise it must be something daft im doing. Could someone
please help me out. [...] P.S. I didnt put any code with this to keep it tidy, but if that would help then i can post it up.


Yes, please post your code...

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
Public Class ClassKi

Inherits System.Collections.CollectionBas

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

Public Sub RemoveDrug(ByVal Index As Integer
If Index > Count - 1 Or Index < 0 The
MsgBox("Index Not Valid!"
Els
List.RemoveAt(Index
End I
End Su

Public Property Drug(ByVal Index As Integer) As ClassDru
Ge
Return CType(List.Item(Index), ClassDrug
End Ge
Set(ByVal Value As ClassDrug
List.Item(Index) = Valu
End Se
End Propert

End Clas

Public Class ClassDru

Private Shared c_Name As Strin
Private Shared c_TradeName As Strin
Private Shared c_Strength As Strin
Private Shared c_Volume As Strin
Private Shared c_ID As Lon
Private Shared c_BatchNumber As Strin
Private Shared c_ExpiryDate As Dat

Public Shared Property Name() As Strin
Ge
Return c_Nam
End Ge
Set(ByVal Value As String
MsgBox(Value
c_Name = Valu
End Se
End Propert

Public Shared Property TradeName() As Strin
Ge
Return c_TradeNam
End Ge
Set(ByVal Value As String
c_TradeName = Valu
End Se
End Propert

Public Shared Property Strength() As Strin
Ge
Return c_Strengt
End Ge
Set(ByVal Value As String
c_Strength = Valu
End Se
End Propert

Public Shared Property Volume() As Strin
Ge
Return c_Volum
End Ge
Set(ByVal Value As String
c_Volume = Valu
End Se
End Propert

Public Shared Property ID() As Lon
Ge
Return c_I
End Ge
Set(ByVal Value As Long
c_ID = Valu
End Se
End Propert

Public Shared Property BatchNumber() As Strin
Ge
Return c_BatchNumbe
End Ge
Set(ByVal Value As String
c_BatchNumber = Valu
End Se
End Propert

Public Shared Property ExpiryDate() As Dat
Ge
Return c_ExpiryDat
End Ge
Set(ByVal Value As Date
c_ExpiryDate = Valu
End Se
End Propert

Public Overrides Function ToString() As Strin
Dim TradeName As Strin
If Me.TradeName <> "" The
TradeName = " (" & Trim(Me.TradeName) & ")
Els
TradeName = Nothin
End I
Return Trim(Me.Name) & ", " & Trim(Me.Strength) & ", " & Trim(Me.Volume) & Trim(TradeName
End Functio

End Clas

Sub ReadDru
Dim NewKit As New KitClas
Dim Drug As New DrugClas
Drug.Name = "Drug1
NewKit.Drug.Add(Drug
Drug.Name = "Drug2
NewKit.Drug.Add(Drug
Drug.Name = "Drug3
NewKit.Drug.Add(Drug
MsgBox NewKit.Drug(1).Sho
End Su

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

J
Nov 20 '05 #3
"James Proctor" <ja**********@ebmis.co.uk> schrieb
Sub ReadDrug
Dim NewKit As New KitClass
Dim Drug As New DrugClass
Drug.Name = "Drug1"
NewKit.Drug.Add(Drug)
Drug.Name = "Drug2"
NewKit.Drug.Add(Drug)
Drug.Name = "Drug3"
NewKit.Drug.Add(Drug)
MsgBox NewKit.Drug(1).Show
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.

You create *one* drug, set it's name, add it to the list, change the drug's
name, add the same drug again... In the sample you add the same drug 3
times. Solution:

Dim NewKit As New KitClass
Dim Drug As DrugClass

Drug = New DrugClass
Drug.Name = "Drug1"
NewKit.Drug.Add(Drug)

Drug = New DrugClass
Drug.Name = "Drug2"
NewKit.Drug.Add(Drug)

Drug = New DrugClass
Drug.Name = "Drug3"
NewKit.Drug.Add(Drug)

MsgBox NewKit.Drug(1).Show
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4
Thanx, ive tried that, but still when i loop through and read back each item it always displays the last item that i added. I cant understand it. Any more ideas?
Nov 20 '05 #5
"James Proctor" <ja**********@ebmis.co.u> schrieb
Thanx, ive tried that, but still when i loop through and read back
each item it always displays the last item that i added. I cant
understand it. Any more ideas?


What's your current code?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #6
James,
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
In addition to Armin's comment about creating one Drug *Object*. You are
only storing information for a single *Drug*.

You only use the Shared keyword if that field, property, sub, function
should be shared across all instances of the class. If that field or method
is specific to a single instance of the class you do not use Shared. Seeing
as Name is specific to a Drug it cannot be shared by all Drugs!

Try something like:
Private c_Name As String
Private c_TradeName As String
Private c_Strength As String
Private c_Volume As String
Private c_ID As Long
Private c_BatchNumber As String
Private c_ExpiryDate As Date Public Property Name() As String

Public Property TradeName() As String

Public Property Strength() As String

Public Property Volume() As String

Public Property ID() As Long

Public Property BatchNumber() As String

Public Property ExpiryDate() As Date
Also you may want to consider a Constructor in the Drug class:

Public Sub New(ByVal name As String)
Me.Name = name
End Sub

Then when you are creating Drugs, you can pass the name of the drug to the
constructor, instead of setting the value after the fact!

Dim Drug As DrugClass
Drug = New DrugClass("Drug1")

If you pass Name to the constructor of Drug, then you have the option of
making Name readonly, meaning once a Drug is created you cannot change its
name. Of course you can pass Name to the constructor & still have it read
write...

Robin A. Reynolds-Haertle's book "OOP with Microsoft Visual Basic .NET and
Microsoft Visual C# .NET - Step by Step" covers creating classes with
constructors and info on Shared fields.

Hope this helps
Jay

"James Proctor" <ja**********@ebmis.co.uk> wrote in message
news:54**********************************@microsof t.com... 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("Index Not Valid!")
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 <> "" Then
TradeName = " (" & Trim(Me.TradeName) & ")"
Else
TradeName = Nothing
End If
Return Trim(Me.Name) & ", " & Trim(Me.Strength) & ", " & Trim(Me.Volume) & Trim(TradeName) End Function

End Class

Sub ReadDrug
Dim NewKit As New KitClass
Dim Drug As New DrugClass
Drug.Name = "Drug1"
NewKit.Drug.Add(Drug)
Drug.Name = "Drug2"
NewKit.Drug.Add(Drug)
Drug.Name = "Drug3"
NewKit.Drug.Add(Drug)
MsgBox NewKit.Drug(1).Show
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.

JP

Nov 20 '05 #7
Thankyou very much, that works perfectly now, i knew it would be something daft

Cheer

JP
Nov 20 '05 #8
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb
James,
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


In addition to Armin's comment about creating one Drug *Object*. You
are only storing information for a single *Drug*.


Aaah! I didn't see it. :-)
--
Armin

Nov 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter Kwan | last post by:
Hi, I believe I have discovered a bug in Python 2.3. Could anyone suggest a get around? When I tested my existing Python code with the newly released Python 2.3, I get the following warning: ...
3
by: jamie_m_ | last post by:
I have a custom collection ... clFile that INHERITS from NameObjectCollectionBase the problem is, when I try to create an xmlserializer instance i get an error You must implement a default...
3
by: ck | last post by:
Sorry for the cross post. What is wrong with this code?--ado recordset to populate a collection--see reason below Dim myCol2 As New Collection On Error Resume Next Do While Not rs.EOF...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
16
by: Ben Hannon | last post by:
Hi, I'm writting a COM Class in VB.NET to be used in a VB6 project (Tired of the VB6 hassles with cloning and serializing an object). All my classes I need cloneable/serializable are now in a...
3
by: Matt Michael | last post by:
I have a listview control and a collection object right now that I'm trying to pass information to and from. Whenever I click on the checkbox, I want it to remove certain listview items and add...
1
by: holysmokes99 | last post by:
I have run the VB.Net upgrade wizard to get a vb6 project up to VB.Net 1.1. This is a class library that is referenced by another VB6 app. I have exposed it to for COM access. The problem is that...
2
by: Andrus | last post by:
I'm trying to compile myGeneration PropertyCollectionAll.cs file with VCS Express 2005 bot got error Error 1 The type or namespace name 'Collection' could not be found (are you missing a using...
8
by: Tanzen | last post by:
I'm working in visual studio 2005 trying to learn visual basic. Having come from an VB for Access background, I'm finding it a big learning curve. I have been working through several e-books which...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.