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 835843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:53:15+00:00 2026-05-15T04:53:15+00:00

I’m having a little problem changing members of an object in a list using

  • 0

I’m having a little problem changing members of an object in a list using a found index.

So this is the method I am currently working with:

static void addToInventory(ref List<baseItem> myArray, baseItem item, float maxAmount, ref float currentAmount)
{
    if (currentAmount + item.getWeight() <= maxAmount)
    {
        Console.WriteLine("item.Quantity = {0}", item.Quantity);
        if (myArray.Contains(item))
        {
            Console.WriteLine("Item ({0}) already exists", item.Name);
            int id = myArray.IndexOf(item);
            myArray[id].Quantity += item.Quantity;//Change occurs in this line, item.Quantity becomes the same as myArray[id].Quantity
        }
        else
        {
            Console.WriteLine("Adding new item ({0})", item.Name);
            myArray.Add(item);
        }
        currentAmount += item.getWeight();
    }
    else
    {
        Console.WriteLine("Inventory full");
    }
    myArray.Sort();
}

This method takes several parameters including the inventory/list. I check if the item fits in and if it does, I see if there is another item of the same name in the list, find the index, and add more of the item. However, the quantity of the item added suddenly becomes the same as the quantity of the item in the list. For some reason, this also changes the quantity of the item outside of the list. So therefore, instead of quantities adding up like this: 1, 2, 3, 4, they add up like this: 1, 2, 4, 8. How can I make it so that the quantity of the added item does not change?

I’ve just started to learn how to use lists so if there is anything I’m missing, don’t hesitate to criticize. Thanks in advance.

To Mark:
Thanks for the very quick reply!
Sorry about the poor naming (myArray); it used to be an ArrayList. The currentAmount and the maxAmount refer to the current weight in the inventory and the max weight the inventory can hold respectively. Also, I don’t want to just add 1 to the quantity; I want it to add the quantity of the item I pass in. Thanks for the tips. I’ll probably look into using Dictionary instead.

  • 1 1 Answer
  • 1 View
  • 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-15T04:53:16+00:00Added an answer on May 15, 2026 at 4:53 am

    What’s happening here is that myArray[id] and item refer to the same object. List.Contains compares by reference, not by value.

    So it looks like you want to simply do

    if (myArray.Contains(item))
    {
        item.Quantity++;
    }
    

    to indicate that there’s one more of that item in the list.

    However, using a list this way is a fundamentally incorrect approach. You should be using a Dictionary or a Set to achieve O(1) lookups.

    If you took the dictionary route, you’d have something like:

    // a Set might be better -- I don't know what you're using this for
    var myDict = new Dictionary<string, BaseItem>();
    // ...
    if (currentAmount + item.Weight <= maxAmount)
    {
        if (myDict.ContainsKey(item.Name))
        {
            Console.WriteLine("Item already exists");
            item.Quantity++;
        }
        else
        {
            Console.WriteLine("Adding new item");
            myDict[item.Name] = item;
        }
    
        currentAmount += item.Weight;
    }
    else
    {
        Console.WriteLine("Full");
    }
    

    A few other notes:

    • Avoid ref arguments (unless you know what you’re doing). There’s no reason to have the poorly named myArray (myList would be an improvement, but inventory would be spot-on) passed as a reference here.
    • Prefer properties over getXXX() methods.
    • Prefer classes over arbitrary static methods. The method you’ve described here sounds like it belongs to an Inventory class, not as some static method unrelated to a particular instance somewhere. ref float currentAmount should be a member of the class, not an explicit parameter to the method.
    • Run StyleCop over your code. Make it a pre-build event. This will force you to get into good C# style habits, such as naming classes and methods with a capital letter (and using properties over get/set methods too).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am currently running into a problem where an element is coming back from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.