-4

So, I'm writing some code for practice and I've come to a point where I want to make a function where I can check a class for objects made from it and then, be able to use the methods and/or data fields I've written on the objects

Like, let me use a kind of blunt example

Dog.CheckForObjectsOfSameClass();

I've tried using a static number to refer to to then refer to the parent of such data, but I can't find a command for that either.

I would post the code I've tried to use before, but it would just make this the more confusing.

Thanks in advance for any answer that can help me solve this doubt

  • Welcome to Stack Overflow. It is unclear what you are asking, it even sounds like an XY problem. Please [edit] your question to include the source code you have, maybe even as a [mcve]. Explain what you are trying to archive and what the exact problem is. – Progman May 26 '19 at 18:45
  • I've read your question multiple times now and I still have no idea what you are trying to do. Unfortunately the *blut example* isn't helping at all. What do you mean by *"check a class for objects made from it"*? – Manfred Radlwimmer May 26 '19 at 18:46
  • With check a class for objects made from it, I made like if there was a command that could check the program it is called on for objects made from an specific class that maybe lives elsewhere in the project file. Like, checking your fridge for apples. you are not looking for one certain apple, but you are only looking to see if there are apples – legitPoodros May 26 '19 at 18:51
  • @legitPoodros You can check https://stackoverflow.com/questions/2262620/how-to-find-the-number-of-objects-in-the-heap and https://stackoverflow.com/questions/1384228/how-can-i-access-java-heap-objects-without-a-reference when you want to look somehow in the heap to look for objects created. Also see the references links/articles/questions of the answers given. – Progman May 26 '19 at 18:58
  • 1
    As Progman mentioned, this sounds like an XY problem (you already settled on a way to solve a problem that you don't explain even tough it might be the wrong way). In this specific case, keeping track of objects that *" live elsewhere in the project"* might mess with the objects lifetime cycle and garbage collection. What exactly are you trying to do? – Manfred Radlwimmer May 26 '19 at 18:58

1 Answers1

2

It sounds like you're asking for something like built-in reference counting. Other than the garbage collector there isn't anything like this in the language, and trying to hook into the garbage collector just to write normal application code would in my opinion be an utterly weird, crazy and plain dreadful thing to do.

As one of the comments suggests, this sounds like an XY problem, where the real solution is to understand why holding a reference to the object you want to access is difficult, and change your coding approach to do this in a more straightforward way. Depending on exactly what you are trying to do, adding a newly created object to a dictionary:

var dogs = new Dictionary<string, Dog>(); var rover = new Dog(); dogs.Add("rover", rover);

using a meaningful key to distinguish which object is which, might be a way of solving the problem.

Tom W
  • 5,108
  • 4
  • 30
  • 52
  • 1
    *"trying to hook into the garbage collector just to write normal application code would in my opinion be an utterly weird, crazy and plain dreadful thing to do"* Agreed - especially since using objects fetched that way would seriously mess with the garbage collection even more. – Manfred Radlwimmer May 26 '19 at 19:01