Link to home
Start Free TrialLog in
Avatar of aspnetdev
aspnetdev

asked on

Retrieveing values from Dictionary in c#

Hi,
I had a list earlier like this
private static List<string> DList = new List<string>(100);
now i have replaced it with
Dictionary<string,string> DList = new Dictionary<string,string>();
Now i have a function
 int listCount = DList.Count;
for (int i = listCount; i > 0; i--)
 {
       string Value = Get(DList[i - 1]);
}
this use to work good as it was List,,but when i converted it to Dictonary there was exception being thrown saying that "The given Key was not present in the dictionary) can any one say how do i change
string Value = Get(DList[i - 1]); to make it work with dictionary
Thanks
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Dictionaries are not looped through the same way Lists are. You need to get the Enumerator for the dictionary and loop through that:

http://msdn2.microsoft.com/en-us/library/k3z2hhax.aspx
ASKER CERTIFIED SOLUTION
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bob Learned
You don't need to loop through the dictionary, you just need to use a key, since the dictionary's signature is <string, string>.

Example:

string value = DList["key"];

Bob
Avatar of aspnetdev
aspnetdev

ASKER

Thanks ...what i did is as follows : please look at the code sinppet.
But when my configuration file is being updated,the Key and value are being set to same.
For example i set the Key="WindowHt" and Value="300"
but in my config its being saved as Key=''300" and value = "300".
Can you tell what might be going wrong.
Thanks
Configuration cm = ConfigurationManager.OpenExeConfiguration(path);
Dictionary<string, string>.Enumerator enumerator = DList.GetEnumerator();
        while (enumerator.MoveNext())
            {
                try
                {
                  
                     string Value = Get(enumerator.Current.Key);
 
                        try
                        {
                            
                            cm.AppSettings.Settings.Remove(DList[enumerator.Current.Key]);
                        }
                        catch { }
                        if (Value != "")
                        {
                          
                            cm.AppSettings.Settings.Add(DList[enumerator.Current.Key], Value);
                        }
                    
                }
                catch { }
                       
            }
            if (DList.Count > 0)
            {
                cm.Save();
            }
        }

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There is probably something going wrong with the way that you are adding values and keys to your configuration file, but that is hard to tell.

Bob
Show us the code you're using to populate the DList object.
man...im stuck bad...
Yeah, and we have no reason why.

Bob
I think if you could show us the code that populates DList then we could figure it out.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok look at the attached file...also this question
https://www.experts-exchange.com/questions/23214834/Application-State-manager.html
 has attached file which i was using when woking with List<string> now the requirement is to replace it with Dictionary.
1.txt
also i do not want to change my code drastically...just wanted to know why is the Key not being copied on config file instead ...key and value are the same.
your not setting the DirtyList to a STATIC setting... perhaps that is your problem since you DID have a static List set previously?

IE: the culprit could be in how your consuming StateManager
I made the dictionary static too...but that does not help.Look at the attach file ...that is how i am consuming the Statemanager dll.Also i have another file called constants.cs in my statemanager.
2.txt
Thanks Guys...i got the solution...actually if you look at the code in Idle method  i was using
cm.AppSettings.Settings.Add(DirtyList[enumerator.Current.Key.ToString()], Value);
INSTEAD OF
cm.AppSettings.Settings.Add(enumerator.Current.Key, Value);
anyways problem solved...i will keep this thread open as i have more modifications to be done.I have to add multithreading concept on this.Will update you in while.
Thanks
If you are trying to persist, form location and size, then you could use user settings to achieve this with minimal code.

Bob
Hi,
Also can any one tell me how do i add the lock to this code...i mean when a application is using this dll it should be locked...(ie)multi threading.
Thanks,
lock(DirtyList)
{
    ... do something....
}

Any additional questions should probably be asked in a new question thread though.