I have created a dictionary in Class A and would like to return the entire list (Key, Value) in class B. Are there any suggestions on how can I do this? I have tried a number of things from the internet but nothing seems to be working. I am trying to write a method in class A to retrieve the entire dictionary.
public class_A
{
public static Dictionary<string, string> Dict_1 = new Dictionary();
public add_dict(string name, string add)
{
Dict_1.Add(name, add);
}
public dict return_dictionary()
{
return (Dict_1); // I want to have a proper code to return this dictionary.
}
}
If this dictionary is a private member in your class. You can access this Dictionary by an encapuslated method.
and in class B, Create an object and access it
EDIT : As per the edit in the question
You have some propblems in your code.
You can not write a class like this
public class_A. It should bepublic class AYour Dictionary declaration should be like this
Your add_dict method needs a return type. ( I am just adding void, you can have a bool or whatever you wish)
Your method return type should be
Dictionary<string, string>. They you are good.But look into your code, Your
Dict_1property is a public property. So you another class dont even need your return_dictionary method to get the data. It can simply access the data likeSo Encapsulation concept is broken !
Better you should keep your member as private like this. I just changed your method names and variable names to follow some coding/naming conventions for better readability.
This is a good reading to understand Encapsulation http://www.csharp-station.com/Tutorials/lesson19.aspx