How do you access data in another class in C#?
I need to share data between classes in C#. Sounds easy enough. I have a collection that is loaded with data in a class. Let's say it is defined like this:
public class AppAdmin: IApplicationThingy
{
public ObservableCollection<Data> DataCollection;
No开发者_运维知识库w, in another class, I want to look at DataCollection. Both classes are in the same namespace. AppAdmin.DataCollection
does not work. Can you help?
You have to have an instance of AppAdmin to access DataCollection
var appAdmin = new AppAdmin();
var data = addAdmin.DataCollection;
or if the design permits, you can make DataCollection static
public class AppAdmin: IApplicationThingy
{
public static ObservableCollection<Data> DataCollection;
and then you can access DataCollection as you mention in the question by
var data = AppAdmin.DataCollection;
By the looks of it, you are trying to access an instance member like a static member. Static members are attached to the class/type and and instance member is attached to an object. If you are looking to access the "DataCollection" as you have it above, you will need to create an AppAdmin object first and then you should be able to access it.
Try this.
var aAdmin = new AppAdmin();
var collection = aAdmin.DataCollection;
AppAdmin.DataCollection
is an instance member of AppAdmin
. This means that you need an instance of AppAdmin
to access AppAdmin.DataCollection
for a particular instance.
Thus, at some point you need a reference (be it through a variable of type AppAdmin
or an expression that evaluates to an instance of AppAdmin
) to be able to access AppAdmin.DataCollection
for a particular instance.
So, somehow, someway, you need
AppAdmin appAdmin = // expression that evaluates to an instance of AppAdmin
var dataCollection = appAdmin.DataCollection;
or
var dataCollection =
(expression that evaluates to an instance of AppAdmin).DataCollection
to get a reference to AppAdmin.DataCollection
for a particular instance of AppAdmin
.
Let's put it more simply:
class Dog {
public IEnumerable<DogLeg> Legs { get; set; }
}
A Dog
has Legs
. To be able to get a particular Dog
's Legs
, you need an instance of Dog
to receive the request for its Legs
.
Similarly, an AppAdmin
has a DataCollection
. You need a particular instance of AppAdmin
to receive the request for its DataCollection
.
So, to access an instance member (be it a field, property or method) you need an instance object to receive the request.
You have to instantiate the object and then call the data filed.
AppAdmin aa = new AppAdmin();
aa.DataCollection;
You need to have an instance of the class in order to access its members.
In order to share the data in the instance, you need to pass it around:
// in one object that needs the object
var myAppAdmin = new AppAdmin():
var myData = myAppAdmin.DataCollection;
// call to another object, passing in the class
myOtherClass.GetDataFromAppAdmin(myAppAdmin);
Maybe you want to make it static?
public class AppAdmin: IApplicationThingy
{
static public ObservableCollection<Data> DataCollection;
Depends...
If the data just needs to always be available outside of any given instance of the object, just make it static
:
public static ObservableCollection<Data> DataCollection;
If the data is tied to an instance of the object, then you access it from the instance and not from the class:
var myObj = new AppAdmin();
myObj.DataCollection ...
But keep in mind the difference between static and instance values. It's an important subject to learn. Trying to mix the two often leads to strange bugs.
精彩评论