This is the working code:
static class Global
{
private static Dictionary<string, int> emails_compsIDs;
private static Dictionary<string, int> set_emails_compsIDs { set { emails_compsIDs = value; } }
private static DataTable MultiCompReport;
static Global()
{
string e = string.Empty;
Dictionary<string, int> emails_compsIDs = new Dictionary<string, int>();
DataTable t = MySql.ExecuteSelect("SELECT Email, Company_ID FROM potencial_contact WHERE Email != '' AND Email IS NOT NULL");
foreach (DataRow r in t.Rows)
{
int comp_id = Convert.ToInt32(r["Company_ID"]);
e = r["Email"].ToString().Replace("\r", "").Replace("\t", "").Replace("\n", "").Trim().ToLower().Trim();
if (!string.IsNullOrWhiteSpace(e) && !emails_compsIDs.ContainsKey(e))
{
try
{
emails_compsIDs.Add(e, comp_id);
}
catch (Exception ex)
{
throw ex;
}
}
}
if (emails_compsIDs.ContainsKey("linda@andatrade.com"))
Console.WriteLine("Eureka");
set_emails_compsIDs = emails_compsIDs;
}
}
But, when I did just emails_compsIDs = new Dictionary<string, int>();
in order to initialize the dictionay in the constructor the code only filled the 1st row into it.
By creating an instance indise the ctor and then putting the result in my static dictionay it filled all the rows.
The error is:
Which declares a local variable rather than assigning a static field. It should just be:
If that only fills one row, you should step through to find why.