0

I am working on a .net core project where i have to define dictionary in a class and fetch its value into another class.but i should not use static variable at the the time of dictionary declaration.... here below is my code sample

Public classA{
public Dictionary<string, string> sample1{ get; set; }
public void method()
 {
 sample1= new Dictionary<string, string>();
 sample1.Add("Key", text);
}
}

and in another class

public classB
{
private classA _dataHelper;
public void getvalue()
{
string value = _dataHelper.sample1["Key"];
}
}

here in classB the value im getting from Dictionary is null...how to solve this problem

  • Unless you're not initializing _dataHelper, which in the current sample you're not, the there's no problem. By initializing, I mean instantiating and calling method(). – ProgrammingLlama Mar 31 '17 at 12:15
  • You never initialize your `_dataHelper` variable in `classB`, nor call `method()` on it. I imagine this would be giving you a `NullReferenceException`. – David Mar 31 '17 at 12:16
  • but i am declaring dictionary globally... – Abhishek Brahmachary Mar 31 '17 at 12:44
  • There's no such thing as "Globally", only static ... you are defining instance members in a non-static class which you have to instantiate... – ProgrammingLlama Mar 31 '17 at 15:13

0 Answers0