Question

In a building-navigation program I am working on, my group uses a Room class made out of Tile objects. I want to find a way to data-bind the information of the room into an information bubble that pops up during a mouse enter event.

What I am attempting to do is through the MouseEventArgs e, use MemberwiseClone() to make a Tile object with the same information as the Tile that has the MouseEventHandler on it, and collect the information of the Room through that way.

However, since MemberwiseClone() is a protected method, I need to derive the MouseEventArgs class in order to use it in the class. However, since it is a WPF page, I'm not sure how to derive the other class.

What would be the best way to access the MemberwiseClone() method in this situation?

Was it helpful?

Solution

If you're struggling to understand what protected means, here's a simple example:

class Tile
{
    internal static void Test(Tile t)
    {
        object mc = t.MemberwiseClone();  // works fine!
    }
}

class TypeFromWhichTileDoesNotDerive
{
    internal static void Test(Tile t)
    {
        object mc = t.MemberwiseClone();  // illegal, will not compile
    }
}

As you see, you can call the protected method (inherited from System.Object) on a Tile object t only if you are inside the Tile class (or inside some base class of Tile).

So if you want to take advantage of the MemberwiseClone method (be sure to understand what kind of clone it gives, a "shallow" clone), simply make a method inside the Tile class that calls MemberwiseClone on a Tile. Other code outside the Tile class can then call your method (depending on whether your method is private, internal, protected, public).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top