-1

I can loop through the values of the Dictionary using a foreach. However, I do not know how to access the Dictionary out-side of a foreach.
code:

Dictionary<string, dynamic> frmControlProp =
    new Dictionary<string, dynamic>();

public void setFrmControlTagProperties(string n, string tagVal)
{
    var dict = tagVal.Split('|')
        .Select(x => x.Split(':'))
        .Where(x => x.Length > 1 && !String.IsNullOrEmpty(x[0].Trim())
         && !String.IsNullOrEmpty(x[1].Trim()))
        .ToDictionary(x => x[0].Trim(), x => x[1].Trim());

    string en = dict["encrypt"];
    string sn = dict["settingName"];

    var conTag = new { Encrypt = en, SettingName = sn };

    frmControlProp.Add(n, new object());
    frmControlProp[n] = conTag;
}

foreach works as expected:

    foreach (var area in frmControlProp.Keys.ToArray())
    {
        var areaname = frmControlProp[area].Encrypt;
        //MessageBox.Show(areaname.ToString());
    }

Not sure how to access the Dictionary outside of a foreach:

public void textBox_LostFocus(object sender, EventArgs e)
{
    var c = (Control)sender;
    string cn = c.Name;

    //var cd = frmControlProp[cn];

    //MessageBox.Show(frmControlProp.Keys.ToArray()..ToString());
}

EDIT
This code worked as expected:

public void textBox_LostFocus(object sender, EventArgs e)
{
    var c = (Control)sender;
    string cn = c.Name.ToString();

    MessageBox.Show(frmControlProp[cn].Encrypt);
}
1

1 Answer 1

2

I do not know how to access the Dictionary out-side of a foreach.

you are doing it yourself here

string en = dict["encrypt"];

But, i think your problem is that you are unable to access it elsewhere. if you are unable to access it somewhere, then you need to assign it to some variable that is accessible through out your form. As i see that you are trying to access it from your form frmControlProp. So, in your function setFrmControlTagProperties you need to assign dictionary to your form.

First of all create a public property that gets Dictionary. and then assign it like this

frmControlProp.Dict = dict;

then you can get it like this

public void textBox_LostFocus(object sender, EventArgs e)
{
    var c = (Control)sender;
    string cn = c.Name;

    //read the values here
    var cd = frmControlProp.Dict.Keys.ToArray();

}
4
  • I believe that I am able to access the FrmControlProp without having to create a public property and then assign it like you suggested... var cd = frmControlProp[cn].ToString(); MessageBox.Show(cn)// as a test produces { Encrypt = true, SettingName = LogEmailTo }. I'm assuming I have to convert that to an Array. I tried var cd = frmControlProp[cn].ToArray(), but then the MessageBox does not Show.
    – dottedquad
    Aug 16, 2013 at 15:25
  • ` public void textBox_LostFocus(object sender, EventArgs e) { var c = (Control)sender; string cn = c.Name.ToString(); MessageBox.Show(frmControlProp[cn].Encrypt); }' That code worked as expected. I thought I had to convert it to an array in-order to access the object values.
    – dottedquad
    Aug 16, 2013 at 15:42
  • i didn't get your question. can you elaborate it a bit?
    – Ehsan
    Aug 16, 2013 at 15:44
  • originally I was not sure how to access an object from within a Dictionary. I thought, the Dictionary had to be converted to an array(.ToArray()) so I could access the object, but I was mistaken. frmControlProp[cn].Encrypt did the trick.
    – dottedquad
    Aug 16, 2013 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.