I am loading Dictionary object with some values and wanted to access it throughout my project, how can I do that. Please see explained below.
public class class1{
class2 cl2 = new class2();
cl2.loadDataFile();
}
public class class3{
string str = "______________________";
class2 cls2 = new class2();
cls2.getStrings(str);
}
public class class2{
protected Dictionary
new Dictionary
public void loadDataFile()
{
//logic to load data from file on certain
//conditions and store it in 'store' Dictionary
object declared above.
}
public string getStrings(sring str)
{
//Here I will access elements of 'store'.
//I get '0' element. How to get elements here.
}
}
Thanks,
Sam HobbsPosted Apr 26, 2012, 5:27 PM
In the code you provided, class2 has the loadDataFile and getStrings methods. So I assume that the store dictionary will be loaded using loadDataFile and then that data will be used by getStrings. Yet you are caling loadDataFile only in class1 and you are calling getStrings only in class3. Vulpes's original suggestion was to make the store dictionary static, but I am not sure that is what you need.
It will really help if you can explain what you need to do. My best guess is that there is something more that we need to understand that your sample code does not tell us.
Perhaps a better solution would be to use derive class1 and class3 from class2, or something like that. Do you understand that? In your original code, class1 and class3 have class2 in them.
So here is a simple question; will there be only one store dictionary for the entire program?
Sam HobbsPosted Apr 26, 2012, 1:30 PM
Now that I look closer at Vulpes's code I think I would not do it that way. I would not move the loadDataFile out; it is better for the object to know how to load and unload its data. I will do a sample later; I have work to do at the moment.
Sam HobbsPosted Apr 26, 2012, 1:18 PM
I strongly disagree that code is the only form of communication. Programmers can communicate without sample code and programmers should be capable of doing that. I would have provided sample code if relevant but I really want to understand what part of what I said was not clear.
Do you understand?
VulpesPosted Apr 26, 2012, 12:56 PM
class2 is no longer needed and so has been removed :
rohit polPosted Apr 26, 2012, 12:35 PM
Sam HobbsPosted Apr 22, 2012, 3:54 PM
If you do that then there is no need to make class1, calss2 or class3 static and there is no need to make any members of the static. Your Main in the Program class in the Program.cs file would do essentially what the main does in the Test class of Vulpes's code except you would not need the Test class (and not the Console.ReadKey of course).
Do you understand or do you need sample code? You can ask questions to get clarification of what I mean if you need to.
VulpesPosted Apr 22, 2012, 5:13 AM
Otherwise if you use one instance to load the data and another instance to get an element of the Dictionary, then the second instance will be looking at an empty Dictionary as it won't be the same object as was populated by the first instance.