1

Consider the following code.

public string[][] CalculateDistance(string origin, string destination)
{
        string[][] result = new string[1][];

        string url = "MyUrl"
        string requesturl = url;
        string content = fileGetContents(requesturl);
        JObject o = JObject.Parse(content);
        result[0] = new string[2];
        result[0][0] = (string)o.SelectToken("routes[0].legs[0].distance.text");
        result[0][1] = (string)o.SelectToken("routes[0].legs[0].duration.text");

        string[][] myArray = new string[2][];
        for (int i = 0; i < result.Length; i++)
        {
            string[] innerArray = result[i];
        }
        return result;
}

I'm trying to return a jagged array which I then use on a ListView in a wpf application. If I use Console.WriteLine(innerArray) inside the for loop I get the correct result displayed. However when displayed in the ListView I get

String[][] Array

Can someone please tell me where I'm going wrong. I've never worked with jagged arrays before so I'm finding it really difficult to figure out what I'm doing wrong.

XMAL Code looks like:

<ListView Name="MyList" HorizontalAlignment="Left" Height="315" Margin="1289,425,-435,0" VerticalAlignment="Top" Width="421">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name"
                DisplayMemberBinding="{Binding Time}"
                Width="100"/>
            </GridView>
        </ListView.View>
</ListView>

And the backend to add items to the list I use:

foreach (var items in GetAddress())
{
  MyList.Items.Add(new Distance() { Time = distance.CalculateDistance(items.FromPostCode, items.DestinationPostCode) });
}

Distance Class looks like

public class Distance
{
    public string[][] Time { get; set; }
    //More properties 
}
OmG
  • 18,337
  • 10
  • 57
  • 90
Izzy
  • 6,740
  • 7
  • 40
  • 84

1 Answers1

1

First of all change your list view to something like this to do correct data binding. (use your own optional length and properties.)

<ListView x:Name="MyList" Height="299" Width="497">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Miles" Width="100" DisplayMemberBinding="{Binding Miles}"/>
            <GridViewColumn Header="Mins" Width="100" DisplayMemberBinding="{Binding Mins}"/>
        </GridView>
    </ListView.View>
</ListView>

Here is the sample use with jagged array.

string[][] list = new[] {new[] {"Hello", "Bye"}, new[] {"Hey", "Ho"}, new[] {"Yep", "Nope"}};
MyList.ItemsSource = list.Select(x => new {Miles = x[0], Mins = x[1]});

But I dont understand the reason you are using Jagged array. You are already creating 1 length of that. that doesnt make sense. just use a single array with length of 2. If you need this for something else that you have not shown then you must show it then i will update my answer as well. currently i removed unnecessary parts.

public string[] CalculateDistance(string origin, string destination)
{
    string[] result = new string[2];

    string url = "MyUrl"
    string requesturl = url;
    string content = fileGetContents(requesturl);

    JObject o = JObject.Parse(content);

    result[0] = (string)o.SelectToken("routes[0].legs[0].distance.text");
    result[1] = (string)o.SelectToken("routes[0].legs[0].duration.text");

    return result;
}

And then when you want to fill items. Note that you dont need Distance class too. If you only use it for binding properties then just write new {} to create anonymous type instead which works perfect.

foreach (var items in GetAddress())
{
    var d = distance.CalculateDistance(items.FromPostCode, items.DestinationPostCode);
    MyList.Items.Add(new { Miles = d[0], Mins = d[1] });
}
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
  • Thanks man, it works perfectly! I change `public string[][]` to `public string[]` as it was giving me an error, so you might want to change that in your answer for future readers. The reason I was using jagged array because I thought i'll bind each item to column as you've done with a single array, I didn't know you could :/ – Izzy Oct 21 '15 at 09:23
  • @Code oh yes. i missed that part .fixed it. glad it helped! – M.kazem Akhgary Oct 21 '15 at 09:26