I have a code skeleton that shows two classes below. It's a simple instantiation stored into the instance variable called _valueList
.
public class EvenBetterValueList : GH_Component
{
ValueList _valueList;
protected override void SolveInstance(IGH_DataAccess DA)
{
// firstList and secondList declarations
_valueList = new ValueList(firstList, secondList);
}
}
public class EvenBetterValueListCustomAttributes : GH_ComponentAttributes
{
// Access the instantiated _valueList from above in this class
}
I am interested in accessing this instance , its name _valueList
, in my second class . I have some setters
and getters
in which it would retrieve strings, etc.
Some things I have currently tried is private EvenBetterValueList newOne;
And trying newOne.someGetter
, with some setters and getters declared in my first class, but understandably, newOne
is null
because it's never instantiated which I don't want to do, because that means I'm creating a new instance versus accessing _valueList
in my first class.
Access to a class's instance variables is normally mediated through properties - they form the contract of what a class provides to the outside world and allow it to mediate how much control consumers have over their internals.
public class EvenBetterValueList : GH_Component
{
ValueList _valueList;
public ValueList ValueList
{
get { return _valueList; }
}
}
Once you've exposed ValueList
as a property, then EvenBetterValueListCustomAttributes
will be able to access the value of EvenBetterValueList.ValueList
from the instance of EvenBetterValueList
that it contains.
To access an instantiated variable from another class, it will either need to be marked as public, or exposed via a public property exactly as described in the answer that @48klocs provided (a property is preferred).
In this case, the containing class needs to be instantiated if we're talking about accessing instance-level variables (which exist within the scope of the instantiated class).
Or, in the case of the example in your question, you'd have an instantiated object of type EvenBetterValueList
and at some point, the method SolveInstance
is called which will initialize _valueList
. I notice that method is protected and overridden, so presumably something is happening to make that call. Then, if you have a public property exposing that variable, you can call it via the instance of EvenBetterValueList
.
The disconnect seems to be that you want to access an instance-level variable as if it's class-level (static).
Here is some code to hopefully better visualize my answer (this assumes you have a public property written that exposes _valueList
):
EvenBetterValueList myEvenBetterValueList = new EvenBetterValueList(); // your instance of EvenBetterValueList
...
// some work happens so that within the scope of myEvenBetterValueList, SolveInstance is called,
// which initializes _valueList
// now we can get that object
ValueList theValueList = myEvenBetterValueList.ValueList;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.