c# - Sharing Dictionary Values -
full disclosure: i'm new c# , not procedural programmer - i'm sql server developer primarily. may mean question either naive or simple. apologies if case.
i've created data processing class number of methods. these methods use elements of same set of metadata drive processing. metadata obtained single line of data returned sql server metadata store.
initially, converted data line variables , passed these around methods explicitly. got messy quickly. moved fields based approach, made things tidier, needs management if metadata structure changes.
to try make things more adaptable, implemented dictionary based approach, using sqldatareader , loop convert column names , values key / value pairs. bit i'm struggling referencing dictionary. can avoid having explicitly pass dictionary object each method now, e.g.
public void main() { // create dictionary , add values dictionary<string, string> mydictionary = new dictionary<string, string>() { {"cat", "miaow"}, {"dog", "woof"}, {"iguana", "grekkkk?"} }; // value dictionary , display messagebox.show(mydictionary["cat"]); // call procedure up(mydictionary); // call procedure calls procedure sh(mydictionary); } public void up(dictionary<string, string> mydictionary) { // value dictionary , display messagebox.show(mydictionary["dog"]); } public void sh(dictionary<string, string> mydictionary) { // call procedure up(mydictionary); } or barking wrong tree completely?
i've seen post:sharing dictionary contents between class instances, attempting understand how use beyond current knowledge level.
edit: here's how i've done it, based on answers jon , rodrigo:
// create empty dictionary dictionary<string, string> mydictionary = new dictionary<string, string>() { }; public void main() { // build dictionary builddictionary(); // value dictionary , display messagebox.show(mydictionary["cat"]); // call procedure up(); // call procedure calls procedure sh(); } public void builddictionary() { // note in implementation uses dynamic process // rather explicitly setting values mydictionary.add("cat", "miaow"); mydictionary.add("dog", "woof"); mydictionary.add("iguana", "grekkk?"); } public void up() { // value dictionary , display messagebox.show(mydictionary["dog"]); } public void sh() { // call procedure up(); } much tidier. both.
iain
in specific case, using member inside own class, should set dictionary private member of class (private default behavior members, implicit).
dictionary<string, string> mydictionary = new dictionary<string, string>() { {"cat", "miaow"}, {"dog", "woof"}, {"iguana", "grekkkk?"} }; public void main() { // create dictionary , add values // value dictionary , display messagebox.show(mydictionary["cat"]); // call procedure up(); // call procedure calls procedure sh(); } public void up() { // value dictionary , display messagebox.show(mydictionary["dog"]); } public void sh() { // call procedure up(); } you should take @ modifiers. more info check here
Comments
Post a Comment