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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:17:36+00:00 2026-05-18T03:17:36+00:00

In my code, I have a class that maintains a number of lists. We’ll

  • 0

In my code, I have a class that maintains a number of lists. We’ll focus on one of them for the moment, since it’s the one that highlighted the problem.

internal List<Badge> Badges { get; private set; }

In the code, I add Badge instances to this list when an XML document is parsed. Later, I want to update the individual instances in the list so I can have the data written back out to XML. Because of the way the data’s XML structure differs from the original file structure, there’s some hocus-pocus involved, but that’s largely mapped out. The surprise came when I attempted to update an item in the List<Badge>.

Specifically, the problematic code is here:

// Get the current badge from the loaded XML data, so we can update it.
var currentBadge = this.GameData.GetCurrentBadge();

I always get a valid badge back. The surprise, as I’ve come to find out, is that this simple test always fails:

var result = this.GameData.Badges.IndexOf(currentBadge);

result always evaluates to -1, indicating that the object doesn’t exist in the collection. (EDIT: Updating the properties on currentBadge has no effect whatsoever on the contents of the matching item in this.GameData.Badges.) Which leads me to conclude that I’m getting a copy of my object back, and not a reference, as I would have expected.

For the inquisitive, the code to retrieve badges from the GameData class is included below. I have a sneaking suspicion that this is a documented behavior of generic lists, and that this is the first time I’ve stumbled across it. If so, I’m in for a very rude awakening. If it’s not, I’d really like to know why my objects are coming back “disconnected” from their originals.

private Badge GetCurrentBadge()
{
    var badgeItem = GetCurrentBadgeItem();
    if (badgeItem != null)
    {
        return this.GameData.GetBadgeByText(badgeItem.Text);
    }
    return null;
}

private MenuOption GetCurrentBadgeItem()
{
    if (!(this.currentItem is MenuOption && 
         (this.currentItem as MenuOption).IsLocked))
    {
        return null;
    }

    MenuOption result = null;
    var children = this.currentMenu.Children;

    for (var n = children.Count - 1; n >= 0; n--)
    {
        var child = children[n] as MenuOption;
        if (child == null || !child.IsLocked)
        {
            break;
        }

        if (!child.Text.StartsWith(" "))
        {
            result = child;
            break;
        }
    }

    return result;
}

UPDATE: Per request, GetBadgeByText, which comes from the GameData class.

internal Badge GetBadgeByText(string badgeText)
{
    foreach (var badge in Badges)
    {
        if (badge.Text.ToLower() == badgeText.ToLower())
        {
            return badge;
        }
    }

    return null;
    // var b = (from l in Badges
    //         where l.Text.ToLower().StartsWith(badgeText.ToLower())
    //         select l).FirstOrDefault();
    //return b;
}

As you can see, I’ve tried it both with and without Linq, just to eliminate that as the culprit. Changing the implementation had no noticable effect.

And for the record, all the objects in this application are CLASSES. No structs anywhere.

UPDATE #2: The Badge class.

internal class Badge
         : GameDataItem
{
    public Badge()
        : base()
    {
    }

    public string AuthId { get; set; }

    public string Category { get; set; }

    public string Description { get; set; }

    public bool IsAccoladePower { get; set; }

    public string RequiredBadges { get; set; }

    public override string ToString()
    {
        return Text;
    }

    internal string ToXml()
    {
        var template = "<Badge value=\"{0}\" title=\"{1}\" category=\"{2}\" authid=\"{3}\" requires=\"{4}\" accolade=\"{5}\" description=\"{6}\" />";
        return string.Format(template,
            this.Value,
            this.Text,
            this.Category,
            this.AuthId,
            this.RequiredBadges,
            this.IsAccoladePower,
            this.Description);
    }
}

And just in case someone asks for it, the base class:

internal class GameDataItem
{
    private string _text;
    public string Text
    {
        get
        {
            return this._text;
        }
        set
        {
            this._text = value.Replace("&lt;", "<")
                         .Replace("&gt;", ">")
                         .Replace("&amp;", "&");
        }
    }
    public string Value { get; set; }
    public override string ToString()
    {
        return Text + "=\"" + Value + "\"";
    }
}
  • 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-18T03:17:37+00:00Added an answer on May 18, 2026 at 3:17 am

    Looks to me like this has something to do with MenuOption‘s implementation of Equals(object). The IndexOf() method of the List<> will use Equals(object) when deciding what to return.

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

Sidebar

Related Questions

I have code that looks like this: class T {}; class container { const
I have code that looks like this: template<class T> class list { public: class
I have a class, which is part of a code library project that was
I'm working on someone's code and they have a constructor that uses: class qwerty
I have this code that describes a service: public class navigation_web extends Service {
I have this code that will check for if the class more-results are in
I have following code snippet that i use to compile class at the run
I have this code that runs but never stops. class A { public static
I have a code piece that I am reviewing (using FindBugs ). public class
I have the following code that creates a serverside object of the xmlhttp class.

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.