Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.Net How to refer to elements in an array list

Posted on 2010-09-06
3
Medium Priority
?
644 Views
Last Modified: 2013-11-27
I require the correct syntax to enable me retrieve values of elements from an ArrayList.

The ArrayList is a list of Customer objects each of which has IDCustomer, CustomerName and CustomerOrders.

In the forms class I use
Public Class frmPreAlert
  ' Create a new ArrayList to hold the Customer objects.
  Private customerArray As New ArrayList()

I fill the Customer array with:

' Get Customer references
   reader1 = cmd1.ExecuteReader()
   While reader1.Read()
       lngIDCompany = reader1("IDCompany")
       strCompanyName = reader1("CompanyName")
      customerArray.Add(New Customer(reader1("IDCompany"), reader1("CompanyName")))
   End While

Later in the sub I want to read the contents of customerArray by For Each loop:
' Add IDPreAlerts to each Order in Customer object in the ArrayList.
   Dim customer1 As Customer
   For Each customer1 In customerArray

      lngIDCompany = *** This is where I need the value of IDCustomer ***
                     <snip>
   Next customer1

Just can't get on with VS2010 help.
' Customer CLASS

Public Class Customer Inherits [Object]
    Private idComp As Long
    Private custName As String = ""
    Friend custOrders As New ArrayList()

Public Sub New(ByVal idcompany As Long, ByVal customername As String)
        Me.idComp = idcompany
        Me.custName = customername
End Sub

Public Property CustomerName() As String
        Get
            Return Me.custName
        End Get
        Set(ByVal Value As String)
            Me.custName = Value
        End Set
End Property

Public Property CustomerID() As Long
        Get
            Return Me.idComp
        End Get
        Set(ByVal Value As Long)
            Me.idComp = Value
        End Set
End Property

Public ReadOnly Property CustomerOrders() As ArrayList
		Get
			Return Me.custOrders
		End Get
End Property
End Class 'End Customer class


Public Class Order
	Inherits [Object]
	Private ordID As String

Public Sub New(ByVal orderid As String)
		Me.ordID = orderid
End Sub 'New

Public Property OrderID() As String
		Get
			Return Me.ordID
		End Get
		Set(ByVal Value As String)
			Me.ordID = Value
		End Set
End Property
End Class ' End Order class

Open in new window

0
Comment
Question by:TeDeSm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 1000 total points
ID: 33611103
As far as I can see it, you can use:

customer1.CustomerID

(by the way, I would suggest to change your names in the class, because it's very confusing right now

You store the IDCompany into the private variable idComp, which you can retrieve back through CustomerID)
0
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 1000 total points
ID: 33611114
according to your posted code you have public CustomerID property in the Customer class, so the following should work:  For Each cust In customerArray      lngIDCompany = cust.CustomerID    Next cust
0
 

Author Closing Comment

by:TeDeSm
ID: 33611219
Thanks for your help. I do have some renaming to do as the code is being re-used from elsewhere.
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question