Winwaed
04-11-2007, 08:18 PM
According to the documentation, I should be able to find a shape by its name using the Shapes collection's Item method.
I've done this in the past with VB6, but it crashes with C#.
Has anyone managed to find a shape without iterating through the entire collection? (I see in another thread, Wilfried iterates through the collection looking for a matching name).
Eg. something like:
MapPoint.Shape myShape = myMap.Shapes.Item("fred");
Perhaps I'm seeing a variant / string problem?
Richard
Wilfried
04-13-2007, 02:38 AM
Hi Richard,
Almost, but it is get_Item and it has to be a ref to the name like this:
object o = theNameToFind;
Shape s = MP.ActiveMap.Shapes.get_Item(ref o);
Winwaed
04-13-2007, 08:56 AM
Wilfried,
That's what I had originally - but it started to throw an exception!
Is a normal C# string okay, or does it have to be cast as something else? (I'm thinking of the dozen or so different string types found on Windows C++!)
Richard
Wilfried
04-13-2007, 10:58 AM
Hi Richard,
Normal string. I just tryed this in a test project and it works. I paste it here:
Shape s = MP.ActiveMap.Shapes.AddShape(GeoAutoShapeType.geoS hapeRadius, loc, dia, dia);
s.Name = "wilfried";
object o = s.Name;
s = MP.ActiveMap.Shapes.get_Item(ref o);
Console.WriteLine(s.Name);
Winwaed
04-15-2007, 08:26 PM
Hmmm....
I'll have another look at my code tomorrow.
Thanks Wilfried - although the above code doesn't rule out the possibility that the text is a "non-conventional" string - you copy from the name property into the object that you use as the parameter.
Richard
Wilfried
04-17-2007, 05:45 AM
Hi Richard,
This is true. It is a conventional string, but there can be a big difference between this:
string name = "wilfried";
object o = name;
s = MP.ActiveMap.Shapes.get_Item(ref o);
and this:
object o = "wilfried";
s = MP.ActiveMap.Shapes.get_Item(ref o);
Because it is given by reference. So in the first case there is possible a copy of "wilfried" in memory where the address is hold by name. But in second case the pointer points to read only memory. So maybe that is the reason ? If of course the first one gives no exception and the second gives one.