ss7
ss7

Reputation: 3012

C# Member Variable Scope

I have three/four functions in my VSTO addin and some member variables:

Member Variables:

private Dictionary<string, string> clientDict;
private Dictionary<string, string> clientHistoryDict;

Function that works with those variables:

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");
}

Now this function works with the member variables. (All of these are in the same class). But these functions do not:

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);
        }
}

As I wrote in the commented out line in the functions, unless i declare the dictionaries in those functions I get this error:

I get the error on this line:

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

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.

I had a feeling the error is related this part of the functions where it says:

while (reader.Read())
{
clientDir = reader.GetString(0);
string clientEmail = reader.GetString(1);
clientDict.Add(clientEmail, clientDir);
}

ID is the first column, clientDir is the second column, and clientEmail is the third. Maybe I'm using reader.GetString() incorrectly? I read somewhere I could do something like reader.GetString["ClientDirName"] (ClientDirName is the column name) but I'm not sure what the correct thing to do is.

Could this be causing the error? If so, how can I access the 2nd and 3rd columns (provided ID is the first column) properly to avoid this error.

If not, what else could cause it?

I've tried a ton of combinations, as I said it works if I move the Dictionary instantiations within the functions but I don't think that's solving the actual problem.

Any assistance is much appreciated.

Upvotes: 1

Views: 731

Answers (1)

ANewGuyInTown
ANewGuyInTown

Reputation: 6467

I'm guessing you forgot to instantiate the clientDict member variable like you do in your function.

Since your are declaring the same variable in function and instantiating, it takes the local scope (when using without this) and it works.

You could either instantiate your private member in the constructor or during the declaration itself.

private Dictionary<string, string> clientDict=new Dictionary<string, string>();
private Dictionary<string, string> clientHistoryDict=new Dictionary<string, string>();

Please follow this link, if you want to know more about scope.

Upvotes: 2

Related Questions