none
How to add/remove as needed? RRS feed

  • Question

  • I was wondering if there is a better way to handle something like this?

                        case 8:
                            polygon.Locations = new LocationCollection(){
                            new Location(double.Parse(warnPOLSplit[0]), double.Parse(warnPOLSplit[1])),
                            new Location(double.Parse(warnPOLSplit[2]), double.Parse(warnPOLSplit[3])),
                            new Location(double.Parse(warnPOLSplit[4]), double.Parse(warnPOLSplit[5])),
                            new Location(double.Parse(warnPOLSplit[6]), double.Parse(warnPOLSplit[7]))};
                            break;
                        case 10:
                            polygon.Locations = new LocationCollection(){
                            new Location(double.Parse(warnPOLSplit[0]), double.Parse(warnPOLSplit[1])),
                            new Location(double.Parse(warnPOLSplit[2]), double.Parse(warnPOLSplit[3])),
                            new Location(double.Parse(warnPOLSplit[4]), double.Parse(warnPOLSplit[5])),
                            new Location(double.Parse(warnPOLSplit[6]), double.Parse(warnPOLSplit[7])),
                            new Location(double.Parse(warnPOLSplit[8]), double.Parse(warnPOLSplit[9]))};
                            break;
    I have a variable set with the amount I need, but is it possible to make this without using a case statement? This number can get pretty high, and I feel like there should be a better way to handle this.


    Monday, May 27, 2019 1:01 AM

Answers

  • Something like this?
    polygon.Locations = new LocationsCollection();
    for(int i = 0; i < whatever / 2; i++) // 'whatever' is 8 or 10, etc.
    {
       int index = i * 2;
       Location location = new Location(double.Parse(warnPOLSplit[index]), double.Parse(warnPOLSplit[index + 1]));
       polygon.Locations.Add(location);
    }

    • Marked as answer by RomWri Monday, May 27, 2019 1:14 AM
    Monday, May 27, 2019 1:09 AM

All replies

  • Something like this?
    polygon.Locations = new LocationsCollection();
    for(int i = 0; i < whatever / 2; i++) // 'whatever' is 8 or 10, etc.
    {
       int index = i * 2;
       Location location = new Location(double.Parse(warnPOLSplit[index]), double.Parse(warnPOLSplit[index + 1]));
       polygon.Locations.Add(location);
    }

    • Marked as answer by RomWri Monday, May 27, 2019 1:14 AM
    Monday, May 27, 2019 1:09 AM
  • That worked. Thank you so much.

    Monday, May 27, 2019 1:14 AM