2015-05-14 46 views
2

全面披露:我是新的C#和不是一般的程序的程序员 - 我是一个SQL Server开发为主。这可能意味着我的问题要么天真要么简单。很抱歉,如果是这样的话。共享字典值

我创建了一个数据处理类的一些方法。这些方法都使用同一组元数据的元素来驱动处理。元数据是从SQL Server元数据存储返回的单行数据中获取的。

起初,我转换这个数据线的变量,并通过这些周围的方法作了明确规定。这很快就变得凌乱了。于是我转向基于Fields的方法,这使得整体更加整洁,但是如果元数据结构发生变化,需要进行管理。

要尽量使事情更加适应,然后我实现了一个基于字典的方法,使用一个SqlDataReader和循环的列名和值以键/值对转换。我正在努力的是参考这本词典。我是否可以避免必须明确地将字典对象传递给每个方法,例如,

public void Main() 
{ 
    // create a dictionary and add values 
    Dictionary<string, string> myDictionary = new Dictionary<string, string>() 
    { 
     {"cat", "miaow"}, 
     {"dog", "woof"}, 
     {"iguana", "grekkkk?"} 
    }; 

    // get a value from the dictionary and display it 
    MessageBox.Show(myDictionary["cat"]); 

    // call another procedure 
    up(myDictionary); 

    // call another procedure that calls another procedure 
    sh(myDictionary); 
} 

public void up(Dictionary<string, string> myDictionary) 
{ 
    // get a value from the dictionary and display it 
    MessageBox.Show(myDictionary["dog"]); 
} 

public void sh(Dictionary<string, string> myDictionary) 
{ 
    // call another procedure 
    up(myDictionary); 
} 

还是我吠叫错了完全的树?

我已经看到了这个帖子:sharing dictionary contents between class instances,而是试图了解如何使用这远远超出了我目前的知识水平。

编辑:下面是我已经做到了,根据来自Jon和罗德里戈的答案:

// create an empty dictionary 
    Dictionary<string, string> myDictionary = new Dictionary<string, string>() 
     { 
     }; 

    public void Main() 
    { 
     // build the dictionary 
     BuildDictionary(); 

     // get a value from the dictionary and display it 
     MessageBox.Show(myDictionary["cat"]); 

     // call another procedure 
     up(); 

     // call another procedure that calls another procedure 
     sh(); 

    } 

    public void BuildDictionary() 
    { 

     // note that in implementation this uses a dynamic process 
     // rather than just explicitly setting values 
     myDictionary.Add("cat", "miaow"); 
     myDictionary.Add("dog", "woof"); 
     myDictionary.Add("iguana", "grekkk?"); 
    } 

    public void up() 
    { 

     // get a value from the dictionary and display it 
     MessageBox.Show(myDictionary["dog"]); 

    } 

    public void sh() 
    { 

     // call another procedure 
     up(); 

    } 

更为整洁。感谢这两个。

伊恩

+1

是字典自然类的状态的一部分?如果是这样,你应该把它变成一个领域。 (我还强烈建议你遵循.NET命名约定,并且给你的方法比'sh'和'up'更有意义的名称,如果这些名称真的具有代表性......)如果字段*超出了你当前的知识范围级别,我建议阅读一本关于C#的入门书 - Stack Overflow对于特定问题非常有用,但不适用于学习核心语言原则。 –

+0

我会+1这个,但看不到一个选项?也许我的noob地位是原因。只是为了回答你的观点,我在剧本中有明确的描述性名字,这些仅仅是为了说明。我也计划投资一本好书。寻找阅读时间将是挑战,一如既往! – iainrobertson

回答

1

在你非常特殊的情况下,如果你只是用你自己的类中的成员,您应该只设置字典作为类的私有成员(私属会员的默认行为,其隐含的)。

Dictionary<string, string> myDictionary = new Dictionary<string, string>() 
     { 
      {"cat", "miaow"}, 
      {"dog", "woof"}, 
      {"iguana", "grekkkk?"} 

     }; 
public void Main() 
{ 

    // create a dictionary and add values 


    // get a value from the dictionary and display it 
    MessageBox.Show(myDictionary["cat"]); 

    // call another procedure 
    up(); 

    // call another procedure that calls another procedure 
    sh(); 

} 

public void up() 
{ 

    // get a value from the dictionary and display it 
    MessageBox.Show(myDictionary["dog"]); 

} 

public void sh() 
{ 

    // call another procedure 
    up(); 

} 

你还应该看看修饰符。欲了解更多信息请here