Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6873437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:04:06+00:00 2026-05-27T04:04:06+00:00

I have a little problem, in my source code I don’t see why is

  • 0

I have a little problem, in my source code I don’t see why is my ToUpload method give me an extra console.writeLine in the Console window. (write out the beer object)

When I call the ToUpload method, and the beer is in the beers dictionary, it give me an extra Console.WriteLine, where write out the beer object at the Console. And I don’t know why.

This is my output:

  • Borsodi sör 160 4.6 1000
  • Pilsner Urquell 250 4.4 800
  • Soproni Ászok 150, 4.5 900
  • Dreher Classic 200, 5.2 600
  • 125
  • Borsodi sör 160 4.6 475
  • Pilsner Urquell 250 4.4 800
  • Soproni Ászok 150, 4.5 1350
  • Dreher Classic 200, 5.2 600
  • Bratista sör, 230, 4.5 300
  • Soproni Ászok 150, 4.5 450 // this is extra
  • Borsodi sör 160 4.6 100 // and this is

and i want this:

  • Borsodi sör 160 4.6 1000
  • Pilsner Urquell 250 4.4 800
  • Soproni Ászok 150, 4.5 900
  • Dreher Classic 200, 5.2 600
  • 125
  • Borsodi sör 160 4.6 475
  • Pilsner Urquell 250 4.4 800
  • Soproni Ászok 150, 4.5 1350
  • Dreher Classic 200, 5.2 600
  • Bratista sör, 230, 4.5 300

    public void ToUpload(Beer beer, int dl)
    {
        int d = 0;
        Beer s = null;
        // search for beer in beers dictionary
        foreach (var item in beers)
        {
            if (item.Key.Equals(beer))
            {
                d = item.Value;
                s = item.Key;
            }
        }
        // if this beer in the beers, update the value
        if (s != null)
        {
            beers[s] = d + dl;
        }
        // if a new beer, just add to beers
        beers.Add(beer, dl); // IDictionary beers = new Dictionary
    }
    
    public Pub()
    {
        ToUpload(new Beer("Borsodi beer", 160, 4.6), 1000);
        ToUpload(new Beer("Pilsner Urquell", 250, 4.4), 800);
        ToUpload(new Beer("Soproni Ászok", 150, 4.5), 900);
        ToUpload(new Beer("Dreher Classic", 200, 5.2), 600);
    }
    
    static void Main(String[] args)
    {
        Beer b = new Beer("Borsodi beer", 160, 4.6);
        Beer c = new Beer("Bratista beer", 230, 4.5);
        Beer d = new Beer("Soproni Ászok", 150, 4.5);
        Pub pub = new Pub();
        foreach (var item in pub.beers)
        {
            Console.WriteLine("{0} {1}", item.Key, item.Value);
    
        }
    
    
        Console.WriteLine(pub.Elad("Borsodi beer", 125));
    
    
        //pub.ToUpload(b, 2000);
        pub.ToUpload(c, 300); // Don't Write out this beer object
        pub.ToUpload(d, 450); // Write out this beer object the console
        pub.ToUpload(b, 100); // Write out this beer object the console
    
    
    
        foreach (var item in pub.beers)
        {
            Console.WriteLine("{0} {1}", item.Key, item.Value);
    
        }
    
        Console.ReadLine();
    

Here is the Beer class:

public class Beer
{
    string name;
    int price; 
double alcohol;

public string Name { get { return name; } }

public int Price{ get; set; }

public double AlkoholTartalom { get { return alcohol; } }

public Beer(string name, int price, double alcohol)
{
    // ide írja a kódot
    this.name = name;
    this.price = price;
    this.alcohol = alcohol;
}

public override bool Equals(object obj)
{
    if (obj is Beer)
    {
        Beer other = (Beer)obj;
        return this.name == other.name;
    }
    return false;
}

public override string ToString()
{
    return this.Name + " " + this.Price+ " " + this.AlkoholTartalom;
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:04:06+00:00Added an answer on May 27, 2026 at 4:04 am

    You are missing “Else” at the ToUpload method.

    // if this beer in the beers, update the value
    if (s != null)
    {
        beers[s] = d + dl;
    }
    else  // Add this 
        beers.Add(beer, dl); // IDictionary beers = new Dictionary
    

    From what i see, this should fix it.
    You are adding the beer to dictionary, even thou you should just update it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have run into a rather weird little problem. In the following code I
i have little problem with boost::asio library. My app receive and process data asynchronously,
I have a little problem with a Listview. I can load it with listview
I have a little problem with a simple vbScript. The script has to run
I have a little problem with some jquery and http://www.mikage.to/jquery/jquery_history_noc.html The function works great,
I have a little problem where I would like to insert a svn diff
I have this little problem, that I cannot figure out which arguments to pass
I have a little problem with a query. I'm selecting data using the between
I have a little problem about using jQuery (I really do not know jQuery
I have a little problem, I have an array and I want to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.