Matheno
Matheno

Reputation: 4152

.NET array output

I have a probably very basic question, but I can't figure it out. (almost weekend ;P ) I have this function ReadOpenCalls(int relation) which gets the open calls from a relation.

List<string> messages = new List<string>();

if (inboundSet != null && inboundSet.RecordCount > 0)
{
    inboundSet.MoveFirst();

    do
    {
        messages.Add(inboundSet.Fields["DESCRIPTION"].Value.ToString());
        messages.Add(inboundSet.Fields["PK_R_INBOUNDMESSAGE"].Value.ToString());

        inboundSet.MoveNext();
    }
    while (!inboundSet.EOF);

    return messages;
}

It's called via WCF into a PHP page. Now that all works perfectly fine, my only problem is the output of it:

stdClass Object ( [string] => Array ( [0] => Webservice [1] => 1004 [2] => Webservice [3] => 1005 [4] => Webservice [5] => 1006 [6] => Webservice [7] => 1007 [8] => Webservice [9] => 1008 [10] => Webservice [11] => 1009 [12] => Webservice [13] => 1010 [14] => Webservice [15] => 1011 ) )

And I really want the output to have the "Webservice and ID's" in this case to be together and not all in one big array.

So something like

[0] => Webservice,
       1004
[1] => Webservice,
       1005

Please help me with some examples or a shot in the good direction. I will buy you a beer after ;)

Upvotes: 0

Views: 169

Answers (1)

Jens Kloster
Jens Kloster

Reputation: 11287

Instead of calling the Addmethod twice, like you do here:

 do
 {
    messages.Add(inboundSet.Fields["DESCRIPTION"].Value.ToString());
    messages.Add(inboundSet.Fields["PK_R_INBOUNDMESSAGE"].Value.ToString());
    inboundSet.MoveNext();
 }

Just call it once per iteration, and add both values.

 do
 {
    string desc = inboundSet.Fields["DESCRIPTION"].Value.ToString();
    string inboundMsg = inboundSet.Fields["PK_R_INBOUNDMESSAGE"].Value.ToString()
    messages.Add(desc  +", "+inboundMsg );
    inboundSet.MoveNext();
 }

If the linebreak is required, then do this:

 do
 {
    string desc = inboundSet.Fields["DESCRIPTION"].Value.ToString();
    string inboundMsg = inboundSet.Fields["PK_R_INBOUNDMESSAGE"].Value.ToString()
    messages.Add(desc  +",\n"+inboundMsg );
    inboundSet.MoveNext();
 }

Upvotes: 3

Related Questions