-1

I have seen at stackexchange some answers with C++ but I need the answer at C#, please.

I must use a loop to add several MapPolylines to a map. If I don't declare a new one the last MapPolyLine is the only one that is shown.

for (int i = 0; i< collections.Count(); i++)
{

// PseudoCode (Locs is a LocationCollection)

MapPolyline mapl(+i) = new MapPolyline();
                        mapl(+i).Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Orange);
                        mapl(+i).StrokeThickness = 2;
                        mapl(+i).Opacity = 0.8;
                      
                        mapl(+i).Locations = locs;
                        mymap.Children.Add(mapl+i);

}

Thanks

2
  • Variable names must be known at compile time. If you want to map names at runtime, use a Map ("pun" intended) Commented Nov 2, 2022 at 18:48
  • Of course @knittl, I was thinking at code and not at compiler. Anyway you mentioned one thing I had never heard before ("There's an extra bit (pun intended) of compiler magic at work" as someone explains). Sorry, but my code was ok, but the order by of my sql was incorrect, My bad! Commented Nov 2, 2022 at 20:56

1 Answer 1

0

No need to use multiple variables mapl(+i). Just use one - mapl. Every iteration in the loop you will create a new object which you then add to some collection:

mymap.Children.Add(mapl);

So you will really have several of them. If you need to distinguish them by number, just add a field to the MapPolyline class, say, Id, and then just set it to i inside the loop:

mapl.Id = i;
Sign up to request clarification or add additional context in comments.

1 Comment

Thks @Dmitry Arestov, you are right and my code was right also. The issue was the "order by" of my sql. My bad, sorry. Anyway your idea of extending MapPolyLine to incorporate Id is very useful for the future. I was getting only one mappolyline and I thought BingMaps was overriding it at each iteration. Lesson learnt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.