December2000
asked on
C# Dictionary Errors
I am getting the two errors below not sure why.... I am super new to dictionaries
1. deposits.Add(deposit); // doesn't exist in current context
2. while (true) //'Program' member names can not ne the same as thier enclosing name
1. deposits.Add(deposit); // doesn't exist in current context
2. while (true) //'Program' member names can not ne the same as thier enclosing name
class Program
{
static void Main(string[] args)
{
// cretae a dictionary where key is a account name and value - account amount
// amout is initially zero
Dictionary<string, double> accounts = new Dictionary<string, double>();
accounts.Add("A", 0);
accounts.Add("B", 0);
accounts.Add("C", 0);
// checked what we have
foreach (KeyValuePair<string, double> item in accounts)
{
Console.WriteLine("Account {0}, amount = {1}", item.Key, item.Value);
}
// enter new amounts:
// We cannot modify the dictionary in foreach loop
// Therefore copy keys to a list:
List<string> accountNames = new List<string>(accounts.Keys);
foreach (string accountName in accountNames)
{
string line;
while (true)// 'Program' member names can not ne the same as thier enclosing name
{
Console.WriteLine(" Please enter new amount for the Account {0} ", accountName);
line = Console.ReadLine();
if (line.StartsWith("Z", StringComparison.CurrentCultureIgnoreCase)) break;
Double amount = Convert.ToDouble(line);
List<KeyValuePair<string, double>> deposits = new List<KeyValuePair<string, double>>();
deposits.Add(deposit); // ??? getting error here-- doesn't exist in current context
}
}
// check again
foreach (KeyValuePair<string, double> item in accounts)
{
Console.WriteLine("Account {0}, amount = {1}", item.Key, item.Value);
}
Console.ReadLine();
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you @ angelIII I am down to on error which is ....
deposits.Add(deposit); //it is saying that it does not exist in the current context
deposits.Add(deposit); //it is saying that it does not exist in the current context
I presume it refers to the variable deposit, which again I cannot see where you declared that one ...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@majorbigdeal gave me the answer on a previous question.... I will let the moderator decide the point distribution....
I don't see any such variable declaration? you meant to write accounts ?