Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
| Search | Today's Posts | Mark Forums Read
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 11th, 2006, 05:48 AM
Authorized User
 
Join Date: Jun 2003
Location: St. Louis, Mo., USA.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to rfinks
Default How access fields of objects stored in Array List

I've got a fairly simple object / class and have stored several of them in an ArrayList. My question is how do I access the individual fields in the object within the ArrayList.

Class definition:

Public Class Pages
    Public PageNumber As Integer
    Public StartKey As Integer
    Public EndKey As Integer

    Public Sub New(ByVal PageNumber As Integer, ByVal StartKey As Integer, ByVal EndKey As Integer)
        MyBase.New()
        Me.PageNumber = PageNumber
        Me.StartKey = Startkey
        Me.EndKey = EndKey
    End Sub
End Class

Storing objects in ArrayList:

    Dim colArrayList As ArrayList

    colArrayList = New ArrayList

    colArrayList.Add(New Pages(intPageNum, intStartingKey, intEndingKey))

My question is how do I access intStartingKey and intEndingKey.

Thank you
Roger Finks
 
Old July 11th, 2006, 11:13 AM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

To use early binding (so that intellisense will work), use an additional object:
Code:
    Dim colArrayList As ArrayList
    Dim tmpObj As Pages

    colArrayList = New ArrayList

    colArrayList.Add(New Pages(intPageNum, intStartingKey, intEndingKey))

    ' Here is where you gain access:
    tmpObj = colArrayList(<select the item you’re after here>)   ' Now the item in the Array 
                                                                  ' list and tmpObj reference
                                                                  ' the same object.
                                                                  But this is not necessary to gain access to the individual objects’s members. VB will access them just fine (though with late binding) with the following:
Code:
    Dim colArrayList As ArrayList
Code:
    colArrayList = New ArrayList

    colArrayList.Add(New Pages(intPageNum, intStartingKey, intEndingKey))

    colArrayList(<select the item you’re after here>).intStartKey = 25
 
Old July 11th, 2006, 06:00 PM
Authorized User
 
Join Date: Jun 2003
Location: St. Louis, Mo., USA.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to rfinks
Default

Brian, your response works perfectly. I was trying to make it harder than it had to be.

Thanks
Roger




Similar Threads
Thread Thread Starter Forum Replies Last Post
Access Data stored in an array jmss66 VB How-To 4 October 10th, 2006 02:17 PM
Assoc. Array of Objects hmmm PHP How-To 1 June 19th, 2006 11:38 AM
Attaching objects to ole fields in tables william.murray BOOK: Access 2003 VBA Programmer's Reference 0 December 8th, 2004 08:21 AM
Access List Box & stored procedure with parameters nrathod_97 Access VBA 5 September 24th, 2004 08:25 AM
Pass a List/Array to a Stored Procedure in SQL Ser sankar SQL Server 2000 4 November 11th, 2003 05:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.