1

I have class called GroupItem, i can store any type here say int, string, decimal, datetime etc.., Then, i have GroupItems which will store any groupItem. I'm using an arraylist to store all the groupItem.

public class GroupItem<T>
{
    private string heading;
    private List<T> items = new List<T>();

    public GroupItem() { }

    public string Heading
    {
        get { return heading; }
        set { heading = value; }
    }

    public List<T> Items
    {
        get { return items; }
        set { items = value; }
    }

    public void Add(T value)
    {
        this.items.Add(value);
    }

    public T this[int index]
    {
        get
        {
            return this.items[index];
        }
    }
}

public class GroupItems
{
    private string groupName;
    private List<object> items = new List<object>();

    public string GroupName
    {
        get { return groupName; }
        set { groupName = value; }
    }

    public GroupItems() { }

    public void Add(object value)
    {
        this.items.Add(value);
    }

    public object this[int index]
    {
        get
        {
            return this.items[index];
        }
    }
}

I want to retrieve from GroupItems. How i can get generic item's values in groupItems?

I'm now inserting two items, datetime and int to groupitems. Now i want to retrieve groupitems[2] value but how i can convert this to groupItem without knowing what it is. Even we may get its genericarguments by getType().getGenericarguments()[0]. But how i can create an instance based upon that.

11
  • 1
    Don’t use ArrayList, it’s obsolete. There’s List<T>. Dec 3, 2011 at 9:31
  • I am sure that many CS course about .net are still taught wih .Net 1.1 in mind. Without generics, LINQ or WPF.
    – Minustar
    Dec 3, 2011 at 9:37
  • I agree that if you're using .NET 2.0 or better, List<T> is the choice to make, but in any case ArrayList has not been marked obsolete even in .NET 4.0 (or even in the preview docs for .NET 4.5)
    – madd0
    Dec 3, 2011 at 9:41
  • Konrad, i agree with you. Even i may use List<GroupItem<t>>.. no issues with array list. I want to know after inserting the item, how i can retrieve it? I may used first item with int, second item with decimal and etc.... i want to create an instance based upon the inserted element.
    – VIRA
    Dec 3, 2011 at 9:49
  • 1
    Raj, what do you mean by “How i can get the stored items in group items”?? Please explain what are you really trying to achieve and what's causing any problems to you. Dec 3, 2011 at 10:14

7 Answers 7

1

If the list is storing heterogeneous items, then I would suggest you need a common non-generic interface or base-class. So, say we have

interface IGroupItem {
// the non-generic members, and maybe
// "object Value {get;}" etc, and maybe
// "Type ItemTypr {get;}"
}

You would then have:

class GroupItem<T> : IGroupItem {...}

an you would then use

List<IGroupItem> ...

instead of ArrayList, or, franky, in place of GroupItems {...}

2
  • 1
    Oh my god, i was just tried to use array list. Ignore that. After i store it in groupItems, how i can retrieve?
    – VIRA
    Dec 3, 2011 at 9:54
  • 1
    @Raj seriously, you don't need GroupItems - it adds nothing (over just the list). But if you really want that; implement IEnumerable<IGroupItem> by returning the list's enumerator, and/or add an IGroupItem this[int index] indexer Dec 3, 2011 at 9:58
0

What I'd do is create a generic collection such as:

public class GroupItems<T> : List<GroupItem<T>>
{
}

If you need to extend the basic functionality of a list, you could also extend Collection<T> and override the methods you need:

public class GroupItems<T> : Collection<GroupItem<T>>
{
    protected override void InsertItem(int index, T item)
    {
         // your custom code here
         // ...

         // and the actual insertion
         base.InsertItem(index, item);
    }
}
5
  • 2
    In most scenarios, it might be even more useful to simply be dealing with the lists of GroupItem<T> themselves, rather than via this GropuItems proxy. Dec 3, 2011 at 9:32
  • I'm working in 2.0. Even though if i load likewise then how i can retrieve it? First element may contain GroupItem<string>, second item int and etc.., I want to create an instance based upon what i have inserted in it.
    – VIRA
    Dec 3, 2011 at 9:50
  • @raj the "first item is string, second is int" etc is what I meant by: heterogeneous data (as opposed to homogeneous data, which is where each is the same type) Dec 3, 2011 at 10:00
  • OK, I hadn't understood your question. In this case, I'd go with Marc's solution.
    – madd0
    Dec 3, 2011 at 10:01
  • Yes its heterogeneous data only, but this isnt resolve my question. :(
    – VIRA
    Dec 6, 2011 at 6:27
0

How about just replacing your GroupItems class with List<GroupItem<T>> ?

1
  • guys, its really not about storing... because there are several possibilities to store it. However i want to know how to retrieve it. for example of yours, i have inserted. I want to create an instance of GroupItem<T> which is what saved in first item. Tell me how to retrieve it.
    – VIRA
    Dec 3, 2011 at 9:46
0

Depending on what you do with GroupItem you should either inherit from List/Collection as was offered by other or use a generic collection inside your class e.g.

    class GroupItem<T> 
{
    private List<T> items = new List<T>();

    public void Add(T value)
    {
        items.Add(value);


    }
    public T Get()
    {
       //replace with some logic to detemine what to get
       return items.First();

    }

}
0

There are two situations that could be covered by your question:

  1. You want to simply store a collection of GroupItem's of type T in the class GroupItems.
  2. You want to store a collection of generic GroupItem's of any type in the class GroupItems. To better clarify, I mean that you could store GroupItem<DateTime> or GroupItem<int> in the same GroupItems class.

Here are some ways of going about storing and retrieving for both scenarios:

Same Type

public class GroupItem<T>
{
    // ... Code for GroupItem<T>
}

public class GroupItems<T>
{
    private List<GroupItem<T>> mItems = new List<GroupItem<T>>();

    public void Add(T item)
    {
        mItems.Add(item);
    }

    public T GetItem(int index)
    {
        return mItems[index];
    }
}

Here you will build a collections that contain GroupItem's of the same time, so a collection of GroupItem<DateTime> for example. All the items will be of the same type.

Generic Type

public interface IGroupItem
{
    // ... Common GroupItem properties and methods
}

public class GroupItem<T>
{
    // ... Code for GroupItem<T>
}

public class GroupItems
{
    private List<IGroupItem> mItems = new List<IGroupItem>();

    public void Add(IGroupItem item)
    {
        mItems.Add(item);
    }

    // This is a generic method to retrieve just any group item.
    public IGroupItem GetItem(int index)
    {
        return mItems[index];
    }

    // This is a method that will get a group item at the specified index
    // and then cast it to the specific group item type container.
    public GroupItem<T> GetItem<T>(int index)
    {
        return (GroupItem<T>)mItems[index];
    }
}

Here you will be able to build and maintain a single collection that can contain any GroupItem with any Type. So you could have a GroupItems collection that contains items of GroupItem<DateTime>, GroupItem<int>, etc.

Please note that none of these code examples take into account any erroneous circumstances.

2
  • Thanks, above code looks good. If i have two items as per your example datetime, int. How will i identify second elements T's type? when creating instance?
    – VIRA
    Dec 3, 2011 at 10:13
  • I would imagine you would explicitly define it when creating the instance, i.e.: IGroupItem item = new GroupItem<DateTime>(); and IGroupItem item = new GroupItem<int>();. Dec 3, 2011 at 10:57
0

Consider: you have a collection of items; the items may have any runtime type (string, int, etc.). Because of this, the static type of the collections items must be object.

It seems that you want to be able to retrieve items from the list with strong static typing. That's not possible without a lot of conditional logic (or reflection). For example:

object item = collection[0];
if (item is int)
    //do something with an int
else if (item is string)
    //do something with a string

Now suppose instead of "doing something" with the value of collection[0], we assign the value to a variable. We can do one of two things:

  • use the same variable for both cases, in which case the static type must be object.
  • use separate variables, in which case the static type will be string or int, but outside of the conditional logic, we can't know which variable holds the value of collection[0].

Neither option really solves the problem.

By creating GroupItem<T>, you add a level of indirection to this problem, but the underlying problem is still there. As an exercise, try reworking the example, but starting from "Consider: you have a collection of items; the items are of type GroupItem<T> where T may be any runtime type (string, int, etc.)."

0

Thanks for your inputs.

I have resolved it myself using multiple overloading methods to resolve this.

for example:

private void Print(GroupItem<string> items)
{
///custom coding
}
private void Print(GroupItem<int> items)
{
///custom coding
}

Though its not efficient enough, i want to do in this way as it was .net 2.0.

I'm now improving this in .Net 4.0 with new algorithm.

Thanks a lot for all of your helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.