• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 842
  • Last Modified:

Doesn't exist in current context error in c#

Why am I getting an error on deposits.Add(deposit); doesn't exist in the current context----???

 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)
                {
                    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);
                    KeyValuePair<string, double> deposit = new KeyValuePair<string, double>(accountName, amount);
                    deposits.Add(deposit); // ??? getting error here

                }
            }

            // check again
            foreach (KeyValuePair<string, double> item in accounts)
            {
                Console.WriteLine("Account {0}, amount = {1}", item.Key, item.Value);
            }
            Console.ReadLine();

        }
    }
}

Open in new window

0
December2000
Asked:
December2000
  • 3
  • 2
2 Solutions
 
käµfm³d 👽Commented:
I don't see where you've declared a variable named deposits. What is supposed to be?
0
 
December2000Author Commented:
ahhh good point @kaufmed .... That suppose to be to compute the totals. where would I declare it, under line = Console.ReadLine(); Inside the last loop or ? ?  .... wow you are a sage number 1! Way to go! Congrats
0
 
käµfm³d 👽Commented:
where would I declare it,
Anywhere before its first use will suffice. Is this a carry-over from the previous question (i.e. the one with MajorBigDeal's code)? If so, then you need to include this line in your code:

 List<KeyValuePair<string, double>> deposits = new List<KeyValuePair<string, double>>();

Open in new window

0
 
December2000Author Commented:
Yes it is a carry over...... I would replace List<string> accountNames = new List (accounts.Keys); with the snipet above.  And declare deposits as  const double Deposits???
0
 
December2000Author Commented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now