Question

if I declare a message array and I also have a MessageEnumerator, how can I add the current message of the enumerator to the array?

Message[] m = null;
MessageEnumerator me = q.GetMessageEnumerator2();
for (int i = 0; i < 10; i++)
    {
        me.MoveNext();
        Array.Resize(ref m, m.Length + 1);
        m[m.Length - 1] = // the current message?
        me.RemoveCurrent();    
     }

any tips at all appreciated

thanks

Était-ce utile?

La solution

Put messages into a list then transform it into an array:

List<Message> ml = new List<Message>();

MessageEnumerator me = q.GetMessageEnumerator2();

for (int i = 0; i < 10; i++)
{
    me.MoveNext();
    ml.Add(me.Current);
}

Message[] m = ml.ToArray();

On another note you should not iterate (use indices) over an IEnumerable, you should instead enumerate this way:

List<Message> ml = new List<Message>();

foreach (Message mx in q.GetMessageEnumerator2())
{
    ml.Add(mx);
}

Message[] m = ml.ToArray();

You can even pour the IEnumerator directly into a new list, but this is so tight it doesn't even make sense anymore:

List<Message> ml = new List<Message>(q.GetMessageEnumerator2());
Message[] m = ml.ToArray();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top