user2424607
user2424607

Reputation: 295

Accessing variables in a class from another class

I have many classes, call them Class1, Class2, ..., and so on. Each of these classes has two variables, TestDictionary and TestListDictionary, as follows. Each of these classes also call a method DoWork() from another class OtherClass, and passes in a string containing the name of the class ("Class1", "Class2", ...).

public class Class1
{
    public static Dictionary<string, string> TestDictionary = ...
    public static ListDictionary TestListDictionary
    ...
    OtherClass.DoWork(GetType().Name);
}

I want the function DoWork to be able to access the two variables TestDictionary and TestListDictionary. I thought of doing the following:

DoWork(string className)
{
    var newClass = Activator.CreateInstance(Type.GetType(className));
    var newDictionary = newClass.TestDictionary;
    var newListDictionary = newClass.TestListDictionary;
    ...
}

But I am not able to access TestDictionary and TestListDictionary (error "cannot resolve symbol 'TestDictionary'/'TestListDictionary').

Say I cannot pass these two variables to the DoWork function directly for various reasons. How would I access them?

Edit: I tried removing the static keyword and still run into the same error.

Upvotes: 0

Views: 1626

Answers (4)

Adil
Adil

Reputation: 148160

You are trying to access the static member with the object of class. You can access the static member with class instead of its instance. Get the type of the class and do not create instance of the type.

Edit

You can use GetField and GetValue to get the static field i.e. TestDictionary, Note you have to give (assembly-qualified name) that include the namespace for the class name passed in Type.GetType.

var newType  = Type.GetType(className);

var newDictionary = newType.TestDictionary;

var newDictionary = (Dictionary<string, string>)newType.GetField("TestDictionary").GetValue(null);
//newDictionary.Add("1","hello");
//Console.WriteLine(Class1.TestDictionary["1"]);

Upvotes: 2

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112682

The easiert way not using Reflection is to create a Dictionary containing the collections you want to access. The class name serves as key.

public static class Globals
{
    public static readonly Dictionary<string, Dictionary<string, string>> Dictionaries
         = new Dictionary<string, Dictionary<string, string>>();
    public static readonly Dictionary<string, ListDictionary> ListDictionaries
         = new Dictionary<string, ListDictionary>();

}

Now Class1 can add its own dictionaries like this:

Globals.Dictionaries.Add("Class1", TestDictionary);
Globals.ListDictionaries.Add("Class1", TestListDictionary);

And another class can access them with:

Globals.Dictionaries[classname].Add(key, value);
int count = Globals.ListDictionaries[classname].Count;

Upvotes: 0

Asha hegde
Asha hegde

Reputation: 26

try inheriting the classes like this

public abstract class abstractA
    {
        public abstract string  TestDictionary { get; set; }
        public abstract string TestListDictionary { get; set; }
       // public abstract string returnDictionary();
        public void   DoWork()
        {
            Console.WriteLine(this.TestDictionary);
            // do anything with the properties         
        }
    }

    public class classA : abstractA
    {
        public override string TestDictionary { get; set; }
        public override string TestListDictionary { get; set; }
        public classA(string a, string b )
        {
            this.TestDictionary = a;
            this.TestDictionary = b; 
        }

    }

I have used string for making it simple. you could use dictionary or any object you want

Upvotes: 0

chiapa
chiapa

Reputation: 4412

In Class1, a variable defined as static:

public static Dictionary<string, string> TestDictionary = new Dictionary<string,string>();
public static string test = "hey!";

Can be accessed from another class:

var newDictionary = Class1.TestDictionary;
string test = Class1.test;

Upvotes: 0

Related Questions