arraylist problem
-
Hello This is one exercise in OOP lesson ,but still I am bit confuse how to get rid of small widgets, and what I want to do is when form loads it displays small widgets(<= 20) in one listbox and and rest in second listox, any help, In Public Function GetRidOfTheSmallWidgets I have put loop but it's only displays 0,1,2 so how can i add small widgets in one listbox and big ones in second listbox
Imports System.Collections.Generic
Imports System.Text
Imports System.CollectionsPublic Class Form1
Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim colBoxesOfWidgets As New ArrayList() colBoxesOfWidgets.Add(New BoxOfWidgets("Cardboard")) DirectCast(colBoxesOfWidgets(0), BoxOfWidgets).colWidgets.Add(New Widget("The Blue Widget", 12)) DirectCast(colBoxesOfWidgets(0), BoxOfWidgets).colWidgets.Add(New Widget("The Red Widget", 15)) DirectCast(colBoxesOfWidgets(0), BoxOfWidgets).colWidgets.Add(New Widget("The Silver Widget", 6)) DirectCast(colBoxesOfWidgets(0), BoxOfWidgets).colWidgets.Add(New Widget("The Green Widget", 52)) colBoxesOfWidgets.Add(New BoxOfWidgets("Metal")) DirectCast(colBoxesOfWidgets(1), BoxOfWidgets).colWidgets.Add(New Widget("The Gold Widget", 9)) DirectCast(colBoxesOfWidgets(1), BoxOfWidgets).colWidgets.Add(New Widget("The Orange Widget", 115)) DirectCast(colBoxesOfWidgets(1), BoxOfWidgets).colWidgets.Add(New Widget("The Pink Widget", 1)) colBoxesOfWidgets.Add(New BoxOfWidgets("Metel")) DirectCast(colBoxesOfWidgets(2), BoxOfWidgets).colWidgets.Add(New Widget("The Grey Widget", 12)) DirectCast(colBoxesOfWidgets(2), BoxOfWidgets).colWidgets.Add(New Widget("The Black Widget", 15)) DirectCast(colBoxesOfWidgets(2), BoxOfWidgets).colWidgets.Add(New Widget("The White Widget", 19)) DirectCast(colBoxesOfWidgets(2), BoxOfWidgets).colWidgets.Add(New Widget("The Brown Widget", 60)) DirectCast(colBoxesOfWidgets(2), BoxOfWidgets).colWidgets.Add(New Widget("The Peach Widget", 16)) Call GetRidOfTheSmallWidgets(colBoxesOfWidgets) End Sub
** Public Function GetRidOfTheSmallWidgets(ByVal colBoxesOfWidgets As ArrayList) As ArrayList
For I As Integer = 0 To colBoxesOfWidgets.Count - 1 lstWidgets.Items.Add(I).ToString() Next Return (colBoxesOfWidgets) End Function**
End Class
Class BoxOfWidgets
Public -
Hello This is one exercise in OOP lesson ,but still I am bit confuse how to get rid of small widgets, and what I want to do is when form loads it displays small widgets(<= 20) in one listbox and and rest in second listox, any help, In Public Function GetRidOfTheSmallWidgets I have put loop but it's only displays 0,1,2 so how can i add small widgets in one listbox and big ones in second listbox
Imports System.Collections.Generic
Imports System.Text
Imports System.CollectionsPublic Class Form1
Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim colBoxesOfWidgets As New ArrayList() colBoxesOfWidgets.Add(New BoxOfWidgets("Cardboard")) DirectCast(colBoxesOfWidgets(0), BoxOfWidgets).colWidgets.Add(New Widget("The Blue Widget", 12)) DirectCast(colBoxesOfWidgets(0), BoxOfWidgets).colWidgets.Add(New Widget("The Red Widget", 15)) DirectCast(colBoxesOfWidgets(0), BoxOfWidgets).colWidgets.Add(New Widget("The Silver Widget", 6)) DirectCast(colBoxesOfWidgets(0), BoxOfWidgets).colWidgets.Add(New Widget("The Green Widget", 52)) colBoxesOfWidgets.Add(New BoxOfWidgets("Metal")) DirectCast(colBoxesOfWidgets(1), BoxOfWidgets).colWidgets.Add(New Widget("The Gold Widget", 9)) DirectCast(colBoxesOfWidgets(1), BoxOfWidgets).colWidgets.Add(New Widget("The Orange Widget", 115)) DirectCast(colBoxesOfWidgets(1), BoxOfWidgets).colWidgets.Add(New Widget("The Pink Widget", 1)) colBoxesOfWidgets.Add(New BoxOfWidgets("Metel")) DirectCast(colBoxesOfWidgets(2), BoxOfWidgets).colWidgets.Add(New Widget("The Grey Widget", 12)) DirectCast(colBoxesOfWidgets(2), BoxOfWidgets).colWidgets.Add(New Widget("The Black Widget", 15)) DirectCast(colBoxesOfWidgets(2), BoxOfWidgets).colWidgets.Add(New Widget("The White Widget", 19)) DirectCast(colBoxesOfWidgets(2), BoxOfWidgets).colWidgets.Add(New Widget("The Brown Widget", 60)) DirectCast(colBoxesOfWidgets(2), BoxOfWidgets).colWidgets.Add(New Widget("The Peach Widget", 16)) Call GetRidOfTheSmallWidgets(colBoxesOfWidgets) End Sub
** Public Function GetRidOfTheSmallWidgets(ByVal colBoxesOfWidgets As ArrayList) As ArrayList
For I As Integer = 0 To colBoxesOfWidgets.Count - 1 lstWidgets.Items.Add(I).ToString() Next Return (colBoxesOfWidgets) End Function**
End Class
Class BoxOfWidgets
PublicYou are dealing with an
ArrayList
that containsBoxOfWidgets
instances. TheArrayList
is populated with only 3BoxOfWidgets
instances: Cardboard, Metal and Metel. EachBoxOfWidgets
instance has it's own internalArrayList
namedcolWidgets
. Perhaps your intention is to iterate over the internal collections of eachBoxOfWidgets
:For I As Integer = 0 To colBoxesOfWidgets.Count - 1
For J As Integer = 0 To DirectCast(colBoxesOfWidgets[I], BoxOfWidgets).colWidgets.Count - 1
lastWidgets.Items.Add(J).ToString()
Next
NextLast, I'd also recommend using the Generic List rather than an ArrayList as it will perform better.
Keep It Simple Stupid! (KISS)
-
You are dealing with an
ArrayList
that containsBoxOfWidgets
instances. TheArrayList
is populated with only 3BoxOfWidgets
instances: Cardboard, Metal and Metel. EachBoxOfWidgets
instance has it's own internalArrayList
namedcolWidgets
. Perhaps your intention is to iterate over the internal collections of eachBoxOfWidgets
:For I As Integer = 0 To colBoxesOfWidgets.Count - 1
For J As Integer = 0 To DirectCast(colBoxesOfWidgets[I], BoxOfWidgets).colWidgets.Count - 1
lastWidgets.Items.Add(J).ToString()
Next
NextLast, I'd also recommend using the Generic List rather than an ArrayList as it will perform better.
Keep It Simple Stupid! (KISS)
-
hello sir Yes it works but it only displays o,1,2,3 numbers in listbox not name or length of widgets waiting for your kind rep.
The reason you only see numbers in the
ListBox
is because the following loop is adding the integerI
to the list box on each pass:For I As Integer = 0 To colBoxesOfWidgets.Count - 1
lstWidgets.Items.Add(I).ToString()
NextSo, you probably want something like this:
For I As Integer = 0 To colBoxesOfWidgets.Count - 1
lstWidgets.Items.Add((DirectCast(colBoxesOfWidgets(I), BoxOfWidgets).boxType)
NextKeep It Simple Stupid! (KISS)
-
The reason you only see numbers in the
ListBox
is because the following loop is adding the integerI
to the list box on each pass:For I As Integer = 0 To colBoxesOfWidgets.Count - 1
lstWidgets.Items.Add(I).ToString()
NextSo, you probably want something like this:
For I As Integer = 0 To colBoxesOfWidgets.Count - 1
lstWidgets.Items.Add((DirectCast(colBoxesOfWidgets(I), BoxOfWidgets).boxType)
NextKeep It Simple Stupid! (KISS)
Hello sir thanks for your rep. yes it works but still i am not getting all the lists for widgets like it shows box of widgets but it's not showing what's in there in each box so I have tried this and it shows how many widgets are there in each box but not name of that widgets i am getting there but still need to do some thing little to get there so this is what i have done
For J As Integer = 0 To colBoxesOfWidgets.Count - 1
ListBox1.Items.Add(DirectCast(colBoxesOfWidgets(J), _
BoxOfWidgets).colWidgets.Add(J))Next
so i think i need to get class widget to get name and length for each widgets but i am lost :confused: waiting for your kind rep. have a nice day