Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello community, I have one question. Is there an easy way to add a number based on the number of passes?
For exampe:
first
if (contains == true)
                       {
                           var newPolygon0 =
                               PolygonBuilder.CreatePolygon(seg, polygonDefinition.GetSpatialReference());
                           createOperation.Create(polygLayer, newPolygon0);
                       }

second
if (contains == true)
                 {
                     var newPolygon1 =
                         PolygonBuilder.CreatePolygon(seg, polygonDefinition.GetSpatialReference());
                     createOperation.Create(polygLayer, newPolygon1);
                 }

68th
if (contains == true)
                 {
                     var newPolygon67 =
                         PolygonBuilder.CreatePolygon(seg, polygonDefinition.GetSpatialReference());
                     createOperation.Create(polygLayer, newPolygon67);
                 }

I will be happy for any advice or solutions.
Thank you

What I have tried:

C#
List<Polygon> sortedList = list.OrderByDescending(o => o.Area).ToList();
                   for (int i = 1; i < list.Count; i++)
                   {
                       var contains = GeometryEngine.Instance.Contains(sortedList[0], sortedList[i]);
                       if (contains == true)
                       {
                           var newPolygon =
                               PolygonBuilder.CreatePolygon(seg, polygonDefinition.GetSpatialReference());
                           createOperation.Create(polygLayer, newPolygon);
                       }
Posted
Updated 22-Nov-21 19:59pm
Comments
PIEBALDconsult 22-Nov-21 9:06am    
Maybe in a scripting language, but not in a "real" language.
"Real" languages have better ways of doing things.

See also: Associative Array

If you are handling different 'Layers, each of which contains 'Polygons. I suggest you create classes or data-structures to keep track of them: then, you can easily access summary information using Linq.
using System.Collections.Generic;
using System.Linq;

namespace YourNameSpace
{
    public class LayerManager
    {
        public string Name { get; }
        public List<Layer> Layers { get; }

        public LayerManager(string lmname = "")
        {
            Name = lmname;
            Layers = new List<Layer>();
        }

        public Layer AddLayer(string lname)
        {
            Layer newlayer = new Layer(lname);
            Layers.Add(newlayer);
            return newlayer;
        }

        public void RemovePolygon(Layer layer)
        {
            if (Layers.Contains(layer)) Layers.Remove(layer);
        }

        public IEnumerable<Layer> SortLayersByMaxPolySize()
        {
            return Layers.OrderByDescending(lyr => lyr.MaxPoly.Area);
        }
    }

    public class Layer
    {
        public string Name { get; }

        public List<Polygon> Polygons;

        public Layer(string lname)
        {
            Name = lname;
            Polygons = new List<Polygon>();
        }

        public Polygon MinPoly = null;
        public Polygon MaxPoly = null;

        public Polygon AddPolygon(Polygon poly)
        {
            Polygons.Add(poly);

            if (MinPoly == null)
            {
                MinPoly = poly;
                MaxPoly = poly;
            }
            else if (poly.Area < MinPoly.Area)
            {
                 MinPoly = poly;
            }
            else if (poly.Area > MaxPoly.Area)
            {
                 MaxPoly = poly;
            }

            return poly;
        }

        public void RemovePolygon(Polygon poly)
        {
            if(Polygons.Contains(poly)) Polygons.Remove(poly);
        }
    }

    // modify this to include relevant polygon data
    public class Polygon
    {
        public string Name { get; }

        public double Area { set; get; }

        public Polygon(string pname, double area = 0.0)
        {
            Name = pname;
            Area = area;
        }
    }
}
Usage example in some method:
LayerManager lmanager = new LayerManager("lman 1");

for (int i = 0; i < 5; i++)
{
    Layer newlayer = lmanager.AddLayer($"layer_{i}");

    for (int j = 0; j < 5; j++)
    {
        double area = i * 20 + j * 20;
        newlayer.AddPolygon(new Polygon($"poly_{i},{j}", area));
    }
}

double maxmax = lmanager.Layers.Max(lyr => lyr.MaxPoly.Area);
double minmin = lmanager.Layers.Min(lyr => lyr.MinPoly.Area);

List<Layer> lyrsbymax = lmanager.SortLayersByMaxPolySize().ToList();
 
Share this answer
 
v2
There is no such thing. You cannot just throw a number on the end of a name and call it a variable.

What you should be looking into a any appropriate collection type, like and array or List, then you can use the index into that collection as your "number".

If you're writing that code 68 times, you're doing something very, very wrong.
 
Share this answer
 
v2
Comments
dejf111 22-Nov-21 8:18am    
It is a pity. Thanks.
Dave Kreskowiak 22-Nov-21 8:19am    
Not a pity. It's how compiled languages work.
dejf111 23-Nov-21 4:54am    
I don't write it 68 times it was an example omg
Dave Kreskowiak 23-Nov-21 10:34am    
So what? Writing it more than once is a sign you've got a design problem.
dejf111 24-Nov-21 0:28am    
It was just an example and a question of a possible solution, I solved it completely differently, so you can rest easy: D
You're confusing the name (reference) of a variable with the ".Name" property of a control. If you want a unique "Name property" for each control, you can use: e.g.

newPolygon.Name = $"Poly{i:d2}";

If i = 5, the .Name then would be "Poly05".

Since the compiler doesn't know about them, they function (more or less) as "keys"; and not as identifiers that the compiler can recognize.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900