Results 1 to 1 of 1

Thread: [RESOLVED] Class's BindingList being instantiated when I reference a different property?!

  1. #1
    Hirsute Mumbler FunkyDexter's Avatar
    Join Date
    Apr 05
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    2,412

    Resolved [RESOLVED] Class's BindingList being instantiated when I reference a different property?!

    I've just hit some really confusing behaviour in C# which I'm hoping someone can explain. I have a class (MarketingGroup) which contains a BindingList (called _Attributes) as one of it's member variables. It's populated from the database and, because I want to avoid populating it if it's not going to be used, I've got this code in it's property:-
    Code:
    public StatefulList<MarketingGroupAttribute> Attributes
            {
                get
                {
                    if (_Attributes == null)
                    { _Attributes = Accessors.MarketingGroup.GetAttributesFor(ID); }  //This method reads the data from the DB
                    return _Attributes;
                }
                set { _Attributes = value; NotifyPropertyChanged("Attributes"); }
            }
    (Stateful list is my own inheritance of Binding List which just adds some functionality to record the persistance state of the items it contains. I'm pretty sure the problems not in there.)

    The get property is the only place in the application where _Attributes is explicitely instantiated.

    I'm using the class to populate a treeview in a form. Here's the code that does that
    Code:
    protected override void Populate()
            {
                TreeNode oppNode = new TreeNode("Opportunity");
                oppNode.Name = "";
                oppNode.Tag = null;
                trvGroups.Nodes.Add(oppNode);
                StatefulList<MarketingGroup> allGroups = TopOut.UltimateMarketing.Model.Accessors.MarketingGroup.GetAllMarketingGroups();
                PopulateChildren(oppNode, allGroups);
            }
    
            private void PopulateChildren(TreeNode parent, StatefulList<MarketingGroup> AllGroups)
            {
                StatefulList<MarketingGroup> children;
                if (parent.Name == "")
                { children = new StatefulList<MarketingGroup>(AllGroups.Where(g => !g.SubGroupID.HasValue).ToList()); }
                else
                { children = new StatefulList<MarketingGroup>(AllGroups.Where(g => g.SubGroupID.Equals(Convert.ToInt32(parent.Name))).ToList()); }
                foreach (MarketingGroup group in children)
                {
                    TreeNode node = new TreeNode(group.Description);  //The problem happens here
                    node.Name = group.ID.ToString();
                    node.Tag = group;
                    parent.Nodes.Add(node);
                    PopulateChildren(node, AllGroups);
                }
            }
    On the marked line of text the _Attributes member of group mysteriously goes from being null to being an empty but instantiated BindingList. It doesn't call the get property so it's not getting a chance to read the data from the DB. As a consequence, when I subequently call the get property, the member doesn't equal null so it doesn't populate. The thing that's really freaking me out is that I'm not even referencing the Attributes Property at that point, I'm referencing a completely different property (description) at that point. It is the first time in the execution path that I'm referencing any property in the class which I'm guessing is somehow causing this but I have no idea why.

    Any ideas?




    edit>Oooh! Just spotted something else. I don't think it is that line of code that's causing it to instantiate. It's actually me interrogating the property using the mouseover in the IDE. If I move the breakpoint around in that routine it doesn't matter where I put it. The first time I hover over it reads null, the second this it reads (Count = 0). Hmm, I guess I can live with that in the IDE but it's gonna make it a pig to track down what's erroneously instantiating it when I run without breakpoints. Grrr.


    edit> Aarggh, forget it. The propblem was simply that the Marketting group I happened to choose genuinely had no attrributes in it. That particular oddity in the ide behaviour (which was genuine) just led me up a garden path that cost about an hour of my life though. Visual Studio, Damn You!!!
    Last edited by FunkyDexter; Oct 25th, 2012 at 10:22 AM.
    When one of my minions says, "Hey, he's just one guy, what can he do?" I say "This"... and shoot them.

    The problem with putting your lair in a volcano is keeping your robot army from melting.

    I know that the human being and the fish can coexist peacefully - George Bush

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •