Main Site Documentation

.NetMF Generic Collection Alternatives


#1

Hello guys,
I know that the NetMF doesn’t support generics, and now I’m faced with an issue, and frankly i can’t think of how to solve it, because the solution is buried somewhere in a C# 1.1 book before generics were introduced… and my head just can’t think backwards… so anyway can help please chime in…


//this is  a simple class
    [Serializable]
    public class myClass1
    {       
        public myClass1()
        {

        }
    }

//a collection that derives from the above class.
using System.Collections.Generic;
using System.Collections.ObjectModel;

[Serializable]
public class myClass1Collection : Collection<myClass1>
{  
    /// <summary>
    /// Initializes a new instance of the <see cref="myClass1Collection"/> class.
    /// </summary>
    /// <param name="myList">My list.</param>
    internal myClass1Collection(IList<myClass1> myList)
        : base(myList)
    { }

    /// <summary>
    /// Initializes a new instance of the <see cref="myClass1Collection"/> class.
    /// </summary>
    public myClass1Collection()
    { }

}

now how can i do this in NETMF world?

maybe the answer is using ArrayList … because that is the equivalent to List if I’m not mistaken…

any sample code is greatly appreciate it.

thanks.


#2

You will have to build a custom collection type using either an array or ArrayList or possibly a Hashtable depending on your needs. ArrayList is not equivalent to List. ArrayList yields an array of Object types, not T types. It’s like going back to CS101… :wink:


#3

Here is an EmailAddressCollection I wrote:

public sealed class EmailAddressCollection : IEnumerable
    {
        private ArrayList _emailCollection;

        public int Count { get { return _emailCollection.Count; } }

        public EmailAddressCollection()
        {
            _emailCollection = new ArrayList();
        }

        public void Add(EmailAddress address)
        {
            _emailCollection.Add(address);
        }

        public void AddRange(EmailAddressCollection addresses)
        {
            foreach (EmailAddress address in addresses)
                _emailCollection.Add(address);
        }

        public void Remove(EmailAddress address)
        {
            _emailCollection.Remove(address);
        }

        public void RemoveAt(int index)
        {
            _emailCollection.RemoveAt(index);
        }

        public IEnumerator GetEnumerator()
        {
            return _emailCollection.GetEnumerator();
        }

        public EmailAddress this[int i]
        {
            get { return (EmailAddress)_emailCollection[i]; }
            set { _emailCollection[i] = value; }
        }
    }

Off Topic: Anybody knows why the code tag doesn’t work?


#4

on code tags not behaving - i’ve been busted by special characters before, even directly after the tag. e.g. a period right after a url tag will cause the period to be included in the url. In general, I put extra CRLFs in to make sure tags behave.

You could also simply try editing the post by deleting the text and pasting it back in.


#5

code tags are back!


#6

@ @ godFather89

than you for the code i will take a look at it and let you know if i make a any progress.

again thank you for sharing…

Jay.