首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#成员变量范围

C#成员变量范围
EN

Stack Overflow用户
提问于 2015-06-04 23:43:43
回答 1查看 629关注 0票数 1

我的VSTO插件中有三/四个函数和一些成员变量:

成员变量:

代码语言:javascript
复制
private Dictionary<string, string> clientDict;
private Dictionary<string, string> clientHistoryDict;

用于这些变量的函数:

代码语言:javascript
复制
public void generateClientDict()
{
        clientDict.Add("alcatel-lucent.com", "Alcatel-Lucent");
        clientDict.Add("emerson.com", "Emerson");
        clientDict.Add("ericsson.com", "Ericsson");
        clientDict.Add("fortress-technologies.com", "Fortress Technologies");
        clientDict.Add("genesys.com", "Genesys");
        clientDict.Add("hitachi.com", "Hitachi Data Systems");
        clientDict.Add("hp.com", "Hewlett Packard");
        clientDict.Add("lg.com", "LG Electronics"); 
        clientDict.Add("samsung.com", "Samsung");
        clientDict.Add("sap.com", "SAP");
        clientDict.Add("tellabs.com", "Tellabs");
        clientDict.Add("thiel-audio.com", "Thiel Audio");
        clientDict.Add("xerox.com", "Xerox");
        clientDict.Add("zebra.com", "Zebra Technologies");

        clientHistoryDict.Add("3com.com", "3Com- CommWorks");
        clientHistoryDict.Add("3m.com", "3M");
        clientHistoryDict.Add("abis.com", "ABIS");
        clientHistoryDict.Add("acxiom.com", "Acxiom");
        clientHistoryDict.Add("ajusa.com", "AJ-USA");
        clientHistoryDict.Add("akamai.com", "Akamai Technologies");
        clientHistoryDict.Add("alcatel-lucent.com", "Alcatel-Lucent");
        clientHistoryDict.Add("avaya.com", "Avaya");
        clientHistoryDict.Add("beckmancoulter.com", "Beckman Coulter");
        clientHistoryDict.Add("bellsouth.com", "BellSouth");
        clientHistoryDict.Add("bridgevine.com", "Bridgevine");
        clientHistoryDict.Add("casio.com", "Casio");
        clientHistoryDict.Add("cca.com", "CCA");
        clientHistoryDict.Add("ccs.com", "CCS");
        clientHistoryDict.Add("centurylink.com", "CenturyLink");
        clientHistoryDict.Add("chinatelecom.com", "China Telecom");
        clientHistoryDict.Add("cisco.com", "Cisco");
        clientHistoryDict.Add("comcast.com", "Comcast");
        clientHistoryDict.Add("comodo.com", "Comodo");
        clientHistoryDict.Add("comverge.com", "Comverge");
        clientHistoryDict.Add("coriant.com", "Coriant (Spin off from Tellabs)");
        clientHistoryDict.Add("daneelectric.com", "Dane Electric");
        clientHistoryDict.Add("dell.com", "Dell");
        clientHistoryDict.Add("disney.com", "Disney");
        clientHistoryDict.Add("siemens.com", "Efficient Networks- Siemens");
        clientHistoryDict.Add("emc.com", "EMC");
        clientHistoryDict.Add("emergentcommunications.com", "Emergent Communications");
        clientHistoryDict.Add("emerson.com", "Emerson");
        clientHistoryDict.Add("epson.com", "Epson");
        clientHistoryDict.Add("ericsson.com", "Ericsson");
        clientHistoryDict.Add("exigen.com", "Exigen Services");
        clientHistoryDict.Add("frbny.com", "Federal Reverse Bank of New York");
        clientHistoryDict.Add("hometeamsports.com", "Fox Home Team Sports");
        clientHistoryDict.Add("freemansoundlabs.com", "Freeman Sound Labs");
        clientHistoryDict.Add("genesys.com", "Genesys");
        clientHistoryDict.Add("here.com", "HERE, a Nokia Company");
        clientHistoryDict.Add("hp.com", "Hewlett Packard");
        clientHistoryDict.Add("hitachi.com", "Hitachi Data Systems");
        clientHistoryDict.Add("intel.com", "Intel");
        clientHistoryDict.Add("lg.com", "LG Electronics");
        clientHistoryDict.Add("samsung.com", "Samsung");
        clientHistoryDict.Add("sap.com", "SAP");
        clientHistoryDict.Add("subway.com", "Subway");
        clientHistoryDict.Add("tellabs.com", "Tellabs");
        clientHistoryDict.Add("thiel-audio.com", "Thiel Audio");
        clientHistoryDict.Add("xerox.com", "Xerox");
        clientHistoryDict.Add("zebra.com", "Zebra Technologies");
}

现在,这个函数与成员变量一起工作。(所有这些都在同一个班)。但这些职能并不是:

代码语言:javascript
复制
public void populateClientDict(SqlConnection conn)
{
        //Dictionary<string, string> clientDict = new Dictionary<string, string>();  If I don't add this I get an error
        try
        {
            using (conn)
            {
                SqlCommand command = new SqlCommand(
                  @"SELECT ClientDirName, ClientEmailDomain FROM ClientTable;",
                  conn);
                conn.Open();

                SqlDataReader reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        string clientDir = reader.GetString(0);
                        string clientEmail = reader.GetString(1);
                        clientDict.Add(clientEmail, clientDir);
                    }
                }
                else
                {
                    MessageBox.Show("No rows found in ClientTable", "Rows Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                reader.Close();
            }
        }
        catch (InvalidOperationException ex)
        {
            MessageBox.Show(String.Format("Exception while accessing ClientTable: {0}", ex), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        catch (SqlException ex)
        {
            MessageBox.Show(String.Format("Exception while accessing ClientTable: {0}", ex), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
}

public void populateClientHistoryDict(SqlConnection conn)
{
        //Dictionary<string, string> clientHistoryDict = new Dictionary<string, string>(); if I don't add this I get an error
        try
        {
            using (conn)
            {
                SqlCommand command = new SqlCommand(
                  @"SELECT ClientDirName, ClientEmailDomain FROM ClientHistoryTable;",
                  conn);
                conn.Open();

                SqlDataReader reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        string clientDir = reader.GetString(0);
                        string clientEmail = reader.GetString(1);
                        clientHistoryDict.Add(clientEmail, clientDir);
                    }
                }
                else
                {
                    MessageBox.Show("No rows found in ClientHistoryTable", "Rows Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                reader.Close();
            }
        }
        catch (InvalidOperationException ex)
        {
            MessageBox.Show(String.Format("Exception while accessing ClientHistoryTable: {0}", ex), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        catch (SqlException ex)
        {
            MessageBox.Show(String.Format("Exception while accessing ClientHistoryTable: {0}", ex), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
}

正如我在函数中注释掉的行中所写的,除非我声明了这些函数中的字典,否则我会得到以下错误:

我得到了这一行的错误:

clientDict.Add(clientEmail, clientDir); in populateClientDict()和clientHistoryDict.Add(clientEmail, clientDir); in populateClientHistoryDict()

代码语言:javascript
复制
An exception of type 'System.NullReferenceException' occurred in Archive.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

我有一种感觉,错误是与这部分功能有关的,上面写着:

代码语言:javascript
复制
while (reader.Read())
{
clientDir = reader.GetString(0);
string clientEmail = reader.GetString(1);
clientDict.Add(clientEmail, clientDir);
}

ID是第一列,clientDir是第二列,clientEmail是第三列。也许我用错了reader.GetString()?我读到了一些我可以做的事情,比如reader.GetString["ClientDirName"] (ClientDirName是列名),但我不知道该做什么才是正确的。

这可能是导致错误的原因吗?如果是这样的话,如何正确地访问第二列和第三列(如果ID是第一列),以避免此错误。

如果不是,还会有什么原因呢?

我已经尝试了大量的组合,就像我说的,如果我在函数中移动字典实例化的话,它是有效的,但我认为这并不能解决实际的问题。

任何援助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-05 00:00:42

我猜您忘了instantiate clientDict成员variable,就像在function中一样。

由于您在function中声明相同的变量并进行实例化,所以它使用了本地作用域(当使用时不使用this),并且工作正常。

您可以在constructordeclaration本身中实例化您的私有成员。

代码语言:javascript
复制
private Dictionary<string, string> clientDict=new Dictionary<string, string>();
private Dictionary<string, string> clientHistoryDict=new Dictionary<string, string>();

如果您想了解更多关于范围的信息,请按照链接。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30656030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档