?
Solved

Traverse a ArrayList  and

Posted on 2007-10-09
9
Medium Priority
?
746 Views
Last Modified: 2008-01-09
Hello experts i have Dataset returned and i create an arraylist of custom objects.
ArrayList looks like this:

1     ABCDfund      45000
1     XYXfund       345600
1     QWERFund      890000
1     ABCDfund      45000
2     ABCDfund      89000
2     XYXfund       34440
2     QWERFund      50000
2     QWERFund      50000


There are around 5000 records. What is the best way to traverse the ArryaList
i need to build a report which needs to be displayed like this.





num      ABCDfund          XYXfund      QWERFund

1             90000         345600        890000
2             89000           34440        100000


I need to build the columns dynamically also since there might be a new fund tomorrow.I am using VS 2003.
0
Comment
Question by:Bkuniyil
  • 4
  • 3
  • 2
9 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 20041371
are you using VB or C#?
0
 
LVL 35

Expert Comment

by:YZlat
ID: 20041383
also what is the type, structure, properties of a custom object?
0
 

Author Comment

by:Bkuniyil
ID: 20041464
I am using C#. The custom object has properties , like ID, FundType, Amount, FundedDate
I have the Arraylist in one format and i need to display the cumulative amounts for the funds with the rows as columns.
Please help.
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 23

Expert Comment

by:Christopher Kile
ID: 20041506
This should do it for you:

'CustomObject has three properties:  sourceNum, fundName, amount

Dim cust as CustomObject
Dim fundDetail As ArrayList

'Fill fundDetail with CustomObjects

Dim rows as New SortedList

For i = 0 to fundDetail.Count - 1
    cust = fundDetail(i)
    if not rows.ContainsKey(cust.sourceNum) then
        rows.Add cust.sourceNum, New SortedList()
    end if
    Dim cols As SortedList = rows.GetByIndex(rows.IndexByKey(cust.sourceNum))
    if not cols.ContainsKey(cust.sourceNum) then
        cols.Add cust.fundName, 0
    end if
    cols.SetByIndex(
            cols.IndexByKey(cust.fundName),
            cols.GetByIndex(cols.IndexByKey(cust.fundName)) + cust.amount)
Next
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 20041575
Note that my VB example is easily converted to C#.  Here it is, using your field names:

'CustomObject has three properties we'll use:  ID, FundType, Amount

CustomObject cust;
ArrayList fundDetail;

// Fill fundDetail with CustomObjects

SortedList rows = New SortedList();

for (i = 0; i < fundDetail.Count; i++)
{
    cust = fundDetail(i)
    if (!rows.ContainsKey(cust.ID))
    {
        rows.Add(cust.ID, new SortedList());
    }
    SortedList cols = rows.GetByIndex(rows.IndexByKey(cust.ID));
    if (!cols.ContainsKey(cust.FundType))
    {
        cols.Add(cust.FundType, 0);
    }
    cols.SetByIndex(
            cols.IndexByKey(cust.FundType),
            cols.GetByIndex(cols.IndexByKey(cust.FundType)) + cust.Amount);
}
0
 

Author Comment

by:Bkuniyil
ID: 20041895
Thank You for the immediate ressponse. I  need some time to understand the code.Does this  add a new column if a new fund is added ??

Thanks again.
0
 

Author Comment

by:Bkuniyil
ID: 20042229
cpkilekofp: A question.

Each cust object will have diff number of columns(for the funds they have) .

How do i display the rest of the funds with an amount 0 , if they do not have that corresponding fund.
0
 
LVL 23

Accepted Solution

by:
Christopher Kile earned 2000 total points
ID: 20048860
You can't unless you have a master list of funds to compare to the list of funds supported by the cust object.  If you have such a list, I would bury it in your business object (cust) so that if cust is asked for a list of FundIDs, it spits out both the FundIDs for which it has transactions as well as the FundIDs for which it does not.  Can you change the business object to do this?  If not, if you can put your master list of FundIDs into any sort of iterable collection, you can initialize your columns with the master list before you apply actual transaction data.  In this modification of the original code I sent you, I use a StringCollection to do just that:

'CustomObject has three properties we'll use:  ID, FundType, Amount

CustomObject cust;
ArrayList fundDetail;
StringCollection masterFundIDList;

// Fill fundDetail with CustomObjects
// Fill masterFundList with master list of FundIDs

SortedList rows = New SortedList();

for (i = 0; i < fundDetail.Count; i++)
{
    cust = fundDetail(i)
    if (!rows.ContainsKey(cust.ID))
    {
        rows.Add(cust.ID, new SortedList());
    }
    SortedList cols = rows.GetByIndex(rows.IndexByKey(cust.ID));

    //  Initialize all columns from the master fund list just once
    if (cols.Count == 0)
    {
        StringEnumerator masterFundEnumerator = masterFundList.GetEnumerator();
        while (masterFundEnumerator.MoveNext())
         {
            cols.Add(masterFundList.Current, 0);
        }
    }
    /*
    If you are worried about transactions with bad FundIDs,
    place validation code here; otherwise, the FundID is assumed
    to be already present in cols.
    */
    cols.SetByIndex(
            cols.IndexByKey(cust.FundType),
            cols.GetByIndex(cols.IndexByKey(cust.FundType)) + cust.Amount);
}
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 20048882
BTW, StringCollection is actually System.Collections.Specialized.StringCollection.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Six Sigma Control Plans

864 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