views:

275

answers:

5

How would I get the parent class of an object that has a value of null?

For example...

ClassA contains int? i which is not set to any value when the class is created.

Then in some other place in the code I want to pass in i as a parameter to some function. Using i as the only info, I want to be able to figure out that ClassA "owns" i.

The reason for this is because ClassA also contains some other object, and I want to call this other object's value from that same function mentioned in the above paragraph.

Could also be:

public class A
{
    public class B
    {
        public int? i;
        public int? j;
    }

    B classBInstance = new B();
    public string s;
}

{
    ...
    A someClassAInstance = new A();
    ...
    doSomething(someClassAInstance.classBInstance.i);
    ...
}

public static bool doSomething(object theObject)
{
    string s = /* SOMETHING on theObject to get to "s" from Class A */;
    int someValue = (int)theObject;
}
+5  A: 

You can't. Pass an instance of A to doSomething.

Paolo Tedesco
Methods should always directly ask for what they need. They should not ask for something that will let them get what they need.
kyoryu
I will have to disagree with you Kyoryu, passing a delegate as a fallback scenario is perfectly legitimate. The scope of the delegate and cause for its invocation should be clear in the naming though. But you do make a fair point, don't buy the whole chicken if all you might need is a feather.
Florian Doyon
It is being used in a delegate. Basically parsing, and I need to be able to pass in where it is going "i" or "j". Then some of the parsing needs the info from the class that i or j is nested in. There has to be a way somehow... When you create an object does it not reserve memory space? So passing in an object by reference is passing in that memory location. That memory location should be tied to some other memory location which should be able to be resolved to a class somehow...
Nick
@Nick: no, there is no way. You have to change the signature of the delegate.
Paolo Tedesco
+2  A: 

class A is not the Parent (base) of its members. Just their holder.

So you cannot do what you want, passing an int or int? around doe not involve any information about the class.

Henk Holterman
A: 

Cant you use a dictionary or keyvaluepairs instead so that the int is linked to "s" that way? The problem is that an int is not aware of which object owns it.

Oskar Kjellin
+2  A: 

The parameter that is sent to the method doesn't contain any information that you can use to determine which object it originally came from. What's sent to the method is just a copy of the nullable int, boxed in an object.

So what you are asking for is not possible. The only way to do something like that would be to analyse the call stack to find the calling method, then analyse the code in that method to determine where the parameter value was taken from.

Guffa
A: 

Its not possible since you are passing 'i' which is a member of class B. But class B does not hold a reference to an instance of class A. An instance is required to get the value of 's' since its a non-static field.

logicnp