Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. arraylist problem

arraylist problem

Scheduled Pinned Locked Moved Windows Forms
helpjsontutorialquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bapu2889
    wrote on last edited by
    #1

    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.Collections

    Public 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

    B 1 Reply Last reply
    0
    • B bapu2889

      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.Collections

      Public 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

      B Offline
      B Offline
      Ben Fair
      wrote on last edited by
      #2

      You are dealing with an ArrayList that contains BoxOfWidgets instances. The ArrayList is populated with only 3 BoxOfWidgets instances: Cardboard, Metal and Metel. Each BoxOfWidgets instance has it's own internal ArrayList named colWidgets. Perhaps your intention is to iterate over the internal collections of each BoxOfWidgets:

      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
      Next

      Last, I'd also recommend using the Generic List rather than an ArrayList as it will perform better.

      Keep It Simple Stupid! (KISS)

      B 1 Reply Last reply
      0
      • B Ben Fair

        You are dealing with an ArrayList that contains BoxOfWidgets instances. The ArrayList is populated with only 3 BoxOfWidgets instances: Cardboard, Metal and Metel. Each BoxOfWidgets instance has it's own internal ArrayList named colWidgets. Perhaps your intention is to iterate over the internal collections of each BoxOfWidgets:

        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
        Next

        Last, I'd also recommend using the Generic List rather than an ArrayList as it will perform better.

        Keep It Simple Stupid! (KISS)

        B Offline
        B Offline
        bapu2889
        wrote on last edited by
        #3

        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.

        B 1 Reply Last reply
        0
        • B bapu2889

          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.

          B Offline
          B Offline
          Ben Fair
          wrote on last edited by
          #4

          The reason you only see numbers in the ListBox is because the following loop is adding the integer I to the list box on each pass:

          For I As Integer = 0 To colBoxesOfWidgets.Count - 1
          lstWidgets.Items.Add(I).ToString()
          Next

          So, you probably want something like this:

          For I As Integer = 0 To colBoxesOfWidgets.Count - 1
          lstWidgets.Items.Add((DirectCast(colBoxesOfWidgets(I), BoxOfWidgets).boxType)
          Next

          Keep It Simple Stupid! (KISS)

          B 1 Reply Last reply
          0
          • B Ben Fair

            The reason you only see numbers in the ListBox is because the following loop is adding the integer I to the list box on each pass:

            For I As Integer = 0 To colBoxesOfWidgets.Count - 1
            lstWidgets.Items.Add(I).ToString()
            Next

            So, you probably want something like this:

            For I As Integer = 0 To colBoxesOfWidgets.Count - 1
            lstWidgets.Items.Add((DirectCast(colBoxesOfWidgets(I), BoxOfWidgets).boxType)
            Next

            Keep It Simple Stupid! (KISS)

            B Offline
            B Offline
            bapu2889
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups