Dawid Jablonski
Dawid Jablonski

Reputation: 583

Get access to static member from array/list of objects

Situation:

ClassA
{
     static string c;
}

 ClassB
 {
     public List<ClassA> Collection;
}

....

ClassB b;

How can I get access to a static member of ClassA having object b of ClassB? Here it is string c.

Upvotes: 0

Views: 735

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156948

You can't get static members from a class instance (so you can't do b.Collection[0].c).

You do have the ability to use reflection to get the type member, but that wouldn't be the best option in my opinion.

I think you'd better create a non-static accessor for the static member:

public string C
{
    get
    {
        return c;
    }
}

Upvotes: 1

Related Questions