bilal.haider
bilal.haider

Reputation: 318

How can a class identify its own objects in C#

I have a class. In c#, is there some way (which i can implement in a static method) which can identify an object of this class, instantiated by some other (calling) class?

Scenario Actually I have a class of Country, which contains a member Rectangle (having 4 coordinates). I want to write a static method which, when passed a point or vector2d, searches for that point in each of the instantiated objects of Country and can return ref of that object. I have Rectangle.Contains(point) method which tells if that point lies in calling rectangle.

Upvotes: 1

Views: 411

Answers (4)

brgerner
brgerner

Reputation: 4371

I don't know a built-in way to get all objects of a class.
Just using a static dictionary (or another collection type) filled with all objects is the best way I know. Like so:

private static Dictionary<string /*name*/, Country> _countryDict =
  new List<Country>
    {
      new Country("Country1", new Rectangle(100, 200, 100, 100)),
      new Country("Country2", new Rectangle(200, 200, 100, 100))
    }.ToDictionary(c => c.Name);

But I guess you have already thought in this direction.

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 839254

You would have to store all instances of your class in a collection, and search the collection to find the specific object you need.

You could make this collection a static member of your class, but it doesn't need to be. It could instead be stored elsewhere as an ordinary member of another class. For example if you have a CountryFactory that you use to construct your Country objects then each time the factory constructs a country it could also store a reference to that country in a collection inside the factory.

Remember also to remove the country from the collection when you no longer need it, otherwise it won't be garbage collected (or use a WeakReference).

Upvotes: 2

Sam Holder
Sam Holder

Reputation: 32954

Why does it have to be a static method?

To solve the problem you need a collection of all countries that you can loop through and check each one against a point. How you keep that collection is up to you. You could store it in some object. It could be a static list inside the Country class, and as new Countries are created using the constructor they are added to the list, or the list could just be a fixed static list, or it could be in some factory which creates Country objects and store a reference to them as they are created. It probably depends on what fits best with how your countries are created at the moment. One thing to watch out for with keeping a list of objects that are created is that you need to remove them from the list when they are no longer needed so that they can be garbage collected, otherwise the memory from them will never be freed.

You could have a method on Country which you pass a point and it returns whether its rectangle contains that point, or you could expose the rectangle from the country and as it from outside.

Upvotes: 0

brianestey
brianestey

Reputation: 8302

One way to do it would be to have the points have a reference to their parent Rectangle and then Rectangle have a reference to their parent Country. Then the static function would simply be:

public Country FindCountry(Point p)
{
    return p.Rectangle.Country;
}

Of course there could be checks for nulls and such, if needed.

Upvotes: 0

Related Questions