Welcome to MapForums!

Register, sign in, or use Facebook Connect above to join in and participate in the forum.

When you are signed in, this message, the ads in this row, and the red-underlined link ads all go away.

Subscribe to receive our newsletter.
Subscribe Unsubscribe
Results 1 to 6 of 6

Selecting a Shape by Name in C#

This is a discussion on Selecting a Shape by Name in C# within the MapPoint Desktop Discussion forums, part of the Map Forums category; According to the documentation, I should be able to find a shape by its name using the Shapes collection's Item ...

  1. #1
    Winwaed's Avatar
    Winwaed is offline Mapping-Tools.com Black Belt
    Join Date
    Feb 2004
    Location
    Irving,Texas
    Posts
    1,859
    Blog Entries
    60

    Selecting a Shape by Name in C#

    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
    Winwaed Software Technology LLC
    http://www.winwaed.com
    See http://www.mapping-tools.com for MapPoint Tools

  2. #2
    Wilfried is offline Senior Member Black Belt
    Join Date
    Nov 2004
    Location
    Belgium
    Posts
    2,433

    Re: Selecting a Shape by Name in C#

    Hi Richard,

    Almost, but it is get_Item and it has to be a ref to the name like this:

    Code:
    object o = theNameToFind;
    Shape s = MP.ActiveMap.Shapes.get_Item(ref o);

  3. #3
    Winwaed's Avatar
    Winwaed is offline Mapping-Tools.com Black Belt
    Join Date
    Feb 2004
    Location
    Irving,Texas
    Posts
    1,859
    Blog Entries
    60

    Re: Selecting a Shape by Name in C#

    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
    Winwaed Software Technology LLC
    http://www.winwaed.com
    See http://www.mapping-tools.com for MapPoint Tools

  4. #4
    Wilfried is offline Senior Member Black Belt
    Join Date
    Nov 2004
    Location
    Belgium
    Posts
    2,433

    Re: Selecting a Shape by Name in C#

    Hi Richard,

    Normal string. I just tryed this in a test project and it works. I paste it here:

    Code:
    Shape s = MP.ActiveMap.Shapes.AddShape(GeoAutoShapeType.geoShapeRadius, loc, dia, dia);
    s.Name = "wilfried";
    object o = s.Name;
    s = MP.ActiveMap.Shapes.get_Item(ref o);
    Console.WriteLine(s.Name);

  5. #5
    Winwaed's Avatar
    Winwaed is offline Mapping-Tools.com Black Belt
    Join Date
    Feb 2004
    Location
    Irving,Texas
    Posts
    1,859
    Blog Entries
    60

    Re: Selecting a Shape by Name in C#

    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
    Winwaed Software Technology LLC
    http://www.winwaed.com
    See http://www.mapping-tools.com for MapPoint Tools

  6. #6
    Wilfried is offline Senior Member Black Belt
    Join Date
    Nov 2004
    Location
    Belgium
    Posts
    2,433

    Re: Selecting a Shape by Name in C#

    Hi Richard,

    This is true. It is a conventional string, but there can be a big difference between this:

    Code:
    string name = "wilfried";
    object o = name;
    s = MP.ActiveMap.Shapes.get_Item(ref o);
    and this:

    Code:
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Does an object reside in my Shape or Shape boundaries?
    By hotrdd in forum MapPoint Desktop Discussion
    Replies: 1
    Last Post: 08-04-2005, 10:51 PM
  2. Selecting cities in drivetime
    By Anonymous in forum MapPoint Desktop Discussion
    Replies: 5
    Last Post: 02-02-2005, 05:21 PM
  3. Selecting Shape Based on Name
    By Anonymous in forum MapPoint Desktop Discussion
    Replies: 1
    Last Post: 09-07-2004, 11:01 AM
  4. Selecting a prviously drawn shape
    By CSG in forum MapPoint Desktop Discussion
    Replies: 1
    Last Post: 03-24-2004, 11:08 AM
  5. Selecting pushpins within a shape
    By Anonymous in forum MapPoint Desktop Discussion
    Replies: 1
    Last Post: 09-26-2002, 09:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96