c# - Objects Retrieved from List<T> Appear to Be Copies, Not References -


in code, have class maintains number of lists. we'll focus on 1 of them moment, since it's 1 highlighted problem.

internal list<badge> badges { get; private set; } 

in code, add badge instances list when xml document parsed. later, want update individual instances in list can have data written out xml. because of way data's xml structure differs original file structure, there's hocus-pocus involved, that's largely mapped out. surprise came when attempted update item in list<badge>.

specifically, problematic code here:

// current badge loaded xml data, can update it. var currentbadge = this.gamedata.getcurrentbadge(); 

i valid badge back. surprise, i've come find out, simple test fails:

var result = this.gamedata.badges.indexof(currentbadge); 

result evaluates -1, indicating object doesn't exist in collection. (edit: updating properties on currentbadge has no effect whatsoever on contents of matching item in this.gamedata.badges.) leads me conclude i'm getting copy of object back, , not reference, have expected.

for inquisitive, code retrieve badges gamedata class included below. have sneaking suspicion documented behavior of generic lists, , first time i've stumbled across it. if so, i'm in rude awakening. if it's not, i'd know why objects coming "disconnected" originals.

private badge getcurrentbadge() {     var badgeitem = getcurrentbadgeitem();     if (badgeitem != null)     {         return this.gamedata.getbadgebytext(badgeitem.text);     }     return null; }  private menuoption getcurrentbadgeitem() {     if (!(this.currentitem menuoption &&           (this.currentitem menuoption).islocked))     {         return null;     }      menuoption result = null;     var children = this.currentmenu.children;      (var n = children.count - 1; n >= 0; n--)     {         var child = children[n] menuoption;         if (child == null || !child.islocked)         {             break;         }          if (!child.text.startswith(" "))         {             result = child;             break;         }     }      return result; } 

update: per request, getbadgebytext, comes 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     //         l.text.tolower().startswith(badgetext.tolower())     //         select l).firstordefault();     //return b; } 

as can see, i've tried both , without linq, eliminate culprit. changing implementation had no noticable effect.

and record, objects in application classes. no structs anywhere.

update #2: 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 in case asks it, base class:

internal class gamedataitem {     private string _text;     public string text     {                 {             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 + "\"";     } } 

looks me has menuoption's implementation of equals(object). indexof() method of list<> use equals(object) when deciding return.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -