In C# new
takes as parameter size (length) of array, not the last index (which is length-1). So just change it to
new MapLocation[2];
Вопрос
In Xamarin I have the following class that I have created:
class MapLocation
{
public LatLng Location;
public BitmapDescriptor icon;
public String Snippet;
public String Title;
}
I am trying to add MapLocation elements to this array as follows:
private MapLocation[] MapLocations = new MapLocation[1];
MapLocations[0].Location = new LatLng(-45.227660, 174.212731);
MapLocations[0].Title = 'Test Title';
MapLocations[1].Location = new LatLng(-45.227834, 174.212857);
MapLocations[1].Title = 'Test Title';
I am normally a Visual Basic programmer, and I am not sure as to what is wrong with the above code.
May I have some help to get this code working?
Thanks in advance.
Решение
In C# new
takes as parameter size (length) of array, not the last index (which is length-1). So just change it to
new MapLocation[2];
Другие советы
At first sight, the only wrong I see is that you have defined an array of one item new MapLocation[1], and you're accessing two items (0 and 1)
Looking into above code it seems that you have created array with length = 1. and you are adding two elements into it. This might be the case which causing the problem.
Change the initialization statement like this :
private MapLocation[] MapLocations = new MapLocation[2];