The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

C# Object best practices

Hi,

Does this code look okay to everyone? I'm trying to build an arraylist of arraylist objects so I can databind to a grid.

                int intCount = 0;

                
    
                ArrayList myList = new ArrayList();

                foreach(Productfolio.BusinessObjects.Item it in allItems)
            
                {
                    
                    myList.Add(it);
                    intCount = intCount + 1;

                    if (intCount > 2)
                    {
                        object[] myObjectArray = new object[3];
                        myList.CopyTo(myObjectArray);
                        list.Add( myObjectArray );
                        intCount = 0;
                        myList.Clear();
                    }

            
                }

Much appreciate any thoughts on this.

Thanks!
Kieran
Tuesday, May 30, 2006
 
 
1. Your code builds ArrayList of arrays, but not of ArrayLists.
2. You'll lost last one or two items in the result list if allItems.Count is not divisible by 3.
Andrew Lygin Send private email
Tuesday, May 30, 2006
 
 
If you really want an ArrayList of arrays you can use something like this:

object[] array = null;

for(int i = 0, j = 0; i < allItems.Count; i++, j = i % 3)
{
    if (j == 0)
    {
        array = new object[3];
        list.Add(array);
    }

    array[j] = allItems[i];
}
Andrew Lygin Send private email
Tuesday, May 30, 2006
 
 
If you're using .NET v2, look at BindingList<T> instead. Data binding is much easier.
el
Tuesday, May 30, 2006
 
 
Thanks everyone!

I'm binding to a 3rd party grid but need the results to output like so:

Record1    Record2  Record3
Record4    Record5  Record6
Record7    Record8  Record9

This is why I need to store 3 item objects inside the arraylist to give me this effect in the grid.

All the best
Kieran
Tuesday, May 30, 2006
 
 
So you have BindingList<MyObject>, or arraylist if you prefer, where MyObject has three properties???
el
Wednesday, May 31, 2006
 
 
Below is a class that takes a list and allows you to iterate over in a gridlike way. Instead of using strings as type for the Grid, use Productfolio.BusinessObjects.Item instead. The code belows uses generics so its .NET2.0 only, but should be easy to convert to a nongeneric code if needed.

class Grid<T>
{
    private int numberOfColumns;
    List<T> list;

    public Grid(IEnumerable<T> list, int numberOfColumns)
    {
        this.list = new List<T>(list);
        this.numberOfColumns = numberOfColumns;
    }

    public IList<T> GetRow(int rowIndex)
    {
        int count = Math.Min(numberOfColumns, list.Count - rowIndex * numberOfColumns);
        return list.GetRange(rowIndex * numberOfColumns, count);
    }

    public int NumberOfRows
    {
        get { return (int)Math.Ceiling((decimal)list.Count / (decimal)numberOfColumns); }
    }
}

class Test
{
    static void Main()
    {
        List<string> allItems = new List<string>(new string[] { "Record1", "Record2", "Record3", "Record4", "Record5", "Record6", "Record7", "Record8", "Record9", "Record10" });
       
        Grid<string> grid = new Grid<string>(allItems, 3);
        for (int i = 0; i < grid.NumberOfRows; i++)
        {
            foreach (string record in grid.GetRow(i))
            {
                System.Console.Write(record + " ");
            }
            System.Console.WriteLine();
        }
    }
}
Patric
Wednesday, May 31, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz