[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Doesn't exist in current context error in c#

Posted on 2012-09-03
5
Medium Priority
?
807 Views
Last Modified: 2012-09-04
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
Comment
Question by:December2000
  • 3
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1500 total points
ID: 38361913
I don't see where you've declared a variable named deposits. What is supposed to be?
0
 

Author Comment

by:December2000
ID: 38361942
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
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1500 total points
ID: 38361991
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
 

Author Comment

by:December2000
ID: 38362068
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
 

Author Closing Comment

by:December2000
ID: 38364006
Thank you!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question