?
Solved

C# Dictionary Errors

Posted on 2012-09-04
6
Medium Priority
?
409 Views
Last Modified: 2012-09-10
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

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();

        }
    }
}

Open in new window

0
Comment
Question by:December2000
  • 3
  • 3
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38363857
let's solve the first problem first: where do you declare the "deposits" variable?
I don't see any such variable declaration? you meant to write accounts  ?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 38363863
sorry, I see it now.
it's inside the loop,you must declare that at a higher level-


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>();
            List<KeyValuePair<string, double>> deposits = new List<KeyValuePair<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);
                    deposits.Add(deposit); // should be ok now

                }
            }

            // 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
 

Author Comment

by:December2000
ID: 38364090
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
0
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!

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38364718
I presume it refers to the variable deposit, which again I cannot see where you declared that one ...
0
 

Assisted Solution

by:December2000
December2000 earned 0 total points
ID: 38369221
@majorbigdeal gave me the answer on a previous question.... I will let the moderator decide the point distribution....

static void Main(string[] args)
        {           
            List<string> accountNames = new List<string>();
            accountNames.Add("A");
            accountNames.Add("B");
            accountNames.Add("C");                                  

            // create a list of deposits           
            List<KeyValuePair<string, double>> deposits = new List<KeyValuePair<string, double>>();            
            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);
                }
            }

            Dictionary<string, double> totals = new Dictionary<string, double>();
            foreach (KeyValuePair<string, double> item in deposits)
            {
                if (totals.ContainsKey(item.Key))
                {
                    totals[item.Key] += item.Value;                 
                }
                else
                {
                    totals.Add(item.Key,item.Value);                 
                }
            }
            foreach (KeyValuePair<String, double> total in totals)
            {
                Console.WriteLine("Account {0}, amount = {1}", total.Key, total.Value);
            }
            Console.ReadLine();
        }
    } 

Open in new window

0
 

Author Closing Comment

by:December2000
ID: 38382352
@majorbigdeal gave me the answer on a previous question.... I will let the moderator decide the point distribution....
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

579 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