xarzu
xarzu

Reputation: 9499

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;

Now, in another class, I want to look at DataCollection. Both classes are in the same namespace. AppAdmin.DataCollection does not work. Can you help?

Upvotes: 0

Views: 11233

Answers (7)

jason
jason

Reputation: 241769

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.

Upvotes: 2

jensendp
jensendp

Reputation: 2135

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;

Upvotes: 3

David
David

Reputation: 219047

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.

Upvotes: 0

Hogan
Hogan

Reputation: 70538

Maybe you want to make it static?

public class AppAdmin: IApplicationThingy
{
     static public ObservableCollection<Data> DataCollection;

Upvotes: 0

Bala R
Bala R

Reputation: 109017

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;

Upvotes: 8

Oded
Oded

Reputation: 499302

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);

Upvotes: 0

zellio
zellio

Reputation: 32524

You have to instantiate the object and then call the data filed.

AppAdmin aa = new AppAdmin();

aa.DataCollection;

Upvotes: 0

Related Questions