C#成员变量范围
我的VSTO插件中有三/四个函数和一些成员变量:C#成员变量范围,c#,dictionary,C#,Dictionary,我的VSTO插件中有三/四个函数和一些成员变量: public void generateClientDict() { clientDict.Add("alcatel-lucent.com", "Alcatel-Lucent"); clientDict.Add("emerson.com", "Emerson"); clientDict.Add("ericsson.com", "Ericsson"); clientDict.Add("
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");
}
成员变量:
private Dictionary<string, string> clientDict;
private Dictionary<string, string> clientHistoryDict;
现在,此函数可用于成员变量。(所有这些都属于同一类)。但这些功能并不:
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);
}
}
我感觉这个错误与函数的这一部分有关,它说:
while (reader.Read())
{
clientDir = reader.GetString(0);
string clientEmail = reader.GetString(1);
clientDict.Add(clientEmail, clientDir);
}
ID是第一列,clientDir是第二列,clientmail是第三列。可能我使用的reader.GetString()
不正确?我在某个地方读到过类似于reader.GetString[“ClientDirName”]
(ClientDirName是列名)的内容,但我不确定该做什么
这可能是导致错误的原因吗?如果是这样,如何正确访问第2列和第3列(提供的ID是第一列)以避免此错误
如果不是的话,还有什么可能导致它
我已经尝试了大量的组合,正如我所说的,如果我在函数中移动字典实例化,它会起作用,但我认为这并不能解决实际问题
非常感谢您提供的任何帮助。我猜您忘了像在
函数中那样实例化clientDict成员变量
由于您在函数中声明了相同的变量并进行了实例化,因此它采用了本地范围(在不使用this
的情况下使用),并且有效
您可以在构造函数中或在声明过程中实例化私有成员
private Dictionary<string, string> clientDict=new Dictionary<string, string>();
private Dictionary<string, string> clientHistoryDict=new Dictionary<string, string>();
private Dictionary clientDict=new Dictionary();
专用词典clientHistoryDict=新词典();
如果您想了解更多有关作用域的信息,请点击链接。我猜您忘记了在构造函数中实例化您的私有clientDict
成员变量
。最好在构造函数中实例化它们。如果调用正在实例化成员的函数
“first”,或者只需执行private Dictionary clientDict=new Dictionary(),则可能会起作用;如果您想了解有关作用域的更多信息,请点击此链接否,在函数
中声明的变量在其他函数
中不可访问。它仅在包含函数的local
范围内。
private Dictionary<string, string> clientDict=new Dictionary<string, string>();
private Dictionary<string, string> clientHistoryDict=new Dictionary<string, string>();