Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I want to retrieve data from array.


VB
'Variable for assigning data

    Structure StatementInfo
        Dim strName As String
        Dim strAccNo As String
        Dim strOwnAdd1 As String
        Dim strOwnAdd2 As String
        Dim strOwnAdd3 As String
        Dim strOwnAdd4 As String
        Dim strMaster As ArrayList
    End Structure

    Public Structure Details
        Dim strTarikh As String
        Dim strStatus As String
        Dim strNoRujukan As String
        Dim strKod As String
        Dim strKeterangan As String
        Dim strDebit As String
        Dim strKredit As String
        Dim strBaki As String
    End Structure

    Public strDetails As Details


'Assign value to array (for example)

        strName = "Micheal"
        strAccNo = "46121212"
        strOwnAdd1 = "Micheal"
        strOwnAdd2 = "Micheal"
        strOwnAdd3 = "Micheal"
        strName = "Micheal"
        strucStatementInfo.strMaster = New ArrayList
        For i = 0 To (CInt(txtCount.Text) - 1)
            With strDetails
                .strTarikh = "01-JAN-2013"
                .strStatus = "B"
                .strNoRujukan = "20131004386" & i.ToString
                .strKod = "1101" & i.ToString
                .strKeterangan = "CUKAI TAKSIRAN"
                .strDebit = "124.1" & i.ToString
                .strKredit = "0"
                .strBaki = "124.36"
            End With
            strucStatementInfo.strMaster.Add(strDetails)
        Next

'Successfull to insert into arraylist
'But failed to view data where it only display the data from the last array

            For i = 0 To (strucStatementInfo.strMaster.Count - 1)
                With strucStatementInfo.strMaster(i)
                    strText1 = strDetails.strTarikh
                    strText2 = strDetails.strStatus
                    strText3 = strDetails.strNoRujukan
                    strText4 = strDetails.strKod
                    strText5 = strDetails.strKeterangan
                    strText6 = strDetails.strDebit 
                    strText7 = strDetails.strKredit
                    strText8 = strDetails.strBaki
                 End With
            Next

<pre>

Anyone know how to retrieve data from structure within arraylist?
Posted
Updated 19-Aug-13 20:14pm
v2

1 solution

There are two ways: one is to explicitly cast the object you retrieve from the ArrayList back to a Details structure:
VB
Dim detail As Details = TryCast(strucStatementInfo.strMaster(i), Details)
If detail IsNot Nothing Then
    With detail
        ...
    End With
End If
But the better way is not to use an ArrayList at all: use a generic List instead:
VB
Dim myList As New List(Of Details)()
Because it will only accept objects which are Details structures, and you don't need to cast the object when you take it out becuse the system already knows what it is.

[edit]Lack-of-caffeine induced spelling errors - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
CPallini 20-Aug-13 3:12am    
5.
Luiey Ichigo 20-Aug-13 3:36am    
Hi Mr. Griff,

I already test the try cast, but anyhow it show blue string "'TryCast' operand must be reference type, but 'modGlobal.Details' is a value type"

Anyway, I finally found the way to retrieve the data by using:

For i = 0 To (strucStatementInfo.strMaster.Count - 1)
With strucStatementInfo.strMaster(i)
strText1 = .strTarikh
strText2 = .strStatus
End With
Next


Therefore, the value will be retrieve.. Thank you Mr. Griff for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900