我的VSTO插件中有三个/四个函数和一些成员变量:
会员变量:
private Dictionary<string, string> clientDict;
private Dictionary<string, string> clientHistoryDict;
适用于这些变量的函数:
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");
}
现在这个函数适用于成员变量。 (所有这些都属于同一类)。但这些功能不会:
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);
}
}
正如我在函数中注释掉的那一行中写的,除非我在这些函数中声明字典,否则我会收到此错误:
我在这一行得到错误:
populateClientHict()中的 clientDict.Add(clientEmail, clientDir);
和populateClientHistoryDict()中的clientHistoryDict.Add(clientEmail, clientDir);
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.
我有一种感觉,错误与这部分功能有关:
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是列名),但我不确定要做的是什么。
这会导致错误吗?如果是这样,我如何正确访问第2列和第3列(提供的ID是第一列)以避免此错误。
如果没有,还有什么可能导致它?
我已经尝试了大量的组合,正如我所说,如果我在函数中移动字典实例,但我不认为这解决了实际问题。
非常感谢任何帮助。
答案 0 :(得分:3)
我猜你忘记了instantiate
clientDict
成员variable
,就像你function
中那样。
由于您在function
中声明了相同的变量并进行实例化,因此它采用了本地范围(在没有this
的情况下使用时)并且它可以正常工作。
您可以在constructor
或declaration
内部实例化您的私人会员。
private Dictionary<string, string> clientDict=new Dictionary<string, string>();
private Dictionary<string, string> clientHistoryDict=new Dictionary<string, string>();
如果您想了解有关范围的更多信息,请点击this链接。