none
object not set to instance of object RRS feed

  • Question

  • Hi I have a class with a List<otherclass> as one of its members.  I have public property in the main class for the list other class but when I try to write to a member of the subclass I get object not set to instance of object.  The main class instanciates without error.
    Thursday, October 2, 2014 4:43 PM

Answers

  • All you did is define it,  and you never instanciated the List<otherclass> as a object within the class that it's a member of.

    So either you instaciate a new List<t> within the class when you instancated the class that holds the List<t>, or you do it outside the holding class before you try the address the List<t> within the holding class.

    If the List<T> is called childeren that is to hold child objects within a class called parrent, then it's this.

    parent.childeren = new List<child>();  // child is class.

    If the class/object is not instanciated to be an object, which lives in memory, then when yiou try to address that class/object  in code that doesn't exist in memory, then what error message are you going to get?

    The link is talking Java, but it applies to any Object Oriented language and any .NET language like C#, VB or others under the .NET envrionment are OO.

    http://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    • Proposed as answer by Rudedog2 Thursday, October 2, 2014 6:09 PM
    • Marked as answer by svt44cobra66 Thursday, October 2, 2014 6:27 PM
    Thursday, October 2, 2014 5:22 PM
  •     public class Child
        {
            public string Name
            {
                get;
                set;
            }
    
            public Child()
            {
            }
        }
    
        public class Parent
        {
            public List<Child> Children
            {
                get;
                set;
            }
    
            public Parent()
            {
                this.Children = new List<Child>();
            }
        }

    It is usually inadvisable to expose a collection as public.  If it is, just for your own code and no one else, then it doesn't make much of a difference.  If it is for a library for use by consumers, then what happens if someone sets the parent.Children property to null?

    Rudy   =^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.com/

    • Marked as answer by svt44cobra66 Thursday, October 2, 2014 6:27 PM
    Thursday, October 2, 2014 6:09 PM
  • public class Parent
     {

           
    private List<child> childern = new List<child>();

           
    public List<child> Childern {
               
    get
               
    {
                   
    return childern;
               
    }
                set
               
    {
                    childern = value
    ;
               
    }
            }
     }

    var parent = new Parent();

    By doing the above, Childern gets automatically instanciated when Parent is instanciated.
     

    • Marked as answer by svt44cobra66 Friday, October 3, 2014 9:38 PM
    Friday, October 3, 2014 5:19 PM

All replies

  • All you did is define it,  and you never instanciated the List<otherclass> as a object within the class that it's a member of.

    So either you instaciate a new List<t> within the class when you instancated the class that holds the List<t>, or you do it outside the holding class before you try the address the List<t> within the holding class.

    If the List<T> is called childeren that is to hold child objects within a class called parrent, then it's this.

    parent.childeren = new List<child>();  // child is class.

    If the class/object is not instanciated to be an object, which lives in memory, then when yiou try to address that class/object  in code that doesn't exist in memory, then what error message are you going to get?

    The link is talking Java, but it applies to any Object Oriented language and any .NET language like C#, VB or others under the .NET envrionment are OO.

    http://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    • Proposed as answer by Rudedog2 Thursday, October 2, 2014 6:09 PM
    • Marked as answer by svt44cobra66 Thursday, October 2, 2014 6:27 PM
    Thursday, October 2, 2014 5:22 PM
  •     public class Child
        {
            public string Name
            {
                get;
                set;
            }
    
            public Child()
            {
            }
        }
    
        public class Parent
        {
            public List<Child> Children
            {
                get;
                set;
            }
    
            public Parent()
            {
                this.Children = new List<Child>();
            }
        }

    It is usually inadvisable to expose a collection as public.  If it is, just for your own code and no one else, then it doesn't make much of a difference.  If it is for a library for use by consumers, then what happens if someone sets the parent.Children property to null?

    Rudy   =^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.com/

    • Marked as answer by svt44cobra66 Thursday, October 2, 2014 6:27 PM
    Thursday, October 2, 2014 6:09 PM
  • ok thanks for the information, was thinking I will probably want to instanciate the new list within the parent class as I will most likely add several other child classes.  So do you instanciate the child class in the constructor of the parent class?

    Thursday, October 2, 2014 6:30 PM
  • ok thanks for the information, i think I asked a second question above before reading this solution

    public parent() is the constructor and public List<Child> Children {get;set;} is the public property.

    Thursday, October 2, 2014 6:33 PM
  • ok thanks for the information, was thinking I will probably want to instanciate the new list within the parent class as I will most likely add several other child classes.  So do you instanciate the child class in the constructor of the parent class?

    It really depends upon how the Parent class needs to be used.  Like I said earlier, it is inadvisable to expose your collection as a public property because it could be set to null, which could throw exceptions.  Here's how it is typically done with Add_Object, Get_Object, and Remove_Object methods added to the class.

        public class Parent
        {
            private Dictionary<string, Child> children;
    
            public int Count {
                get
                {
                    return this.children.Count;
                }
    
            }
    
            public Parent()
            {
                this.children = new Dictionary<string, Child>();
            }
    
            public void Add_Child(Child child)
            {
                this.children.Add(child.Name, child);
            }
    
            public Child Get_Child(String name)
            {
                return this.children[name];
            }
    
            public void Remove_Child(Child child)
            {
                this.children.Remove(child.Name);
            }
    
        }

    Now your collection remains safe from being externally modified.  Your class has complete control of it.  This could also be done with List<T>, but then the Add, Get, and Remove methods become more complicated.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.com/

    Thursday, October 2, 2014 7:34 PM
  • Ok this looks like a good approach, plan to impliment something like this.  Just a another quick question, possibly should start a new thread but I am modifying a large c# program without any code comments and would like to add a progress bar (windows application).  It looks like the way the application was built most of the time consuming activity is occuring in constructors of a classes so there is no corresponding design page.  Any thoughts on how to update the progress bar?  Thanks Paul.
    Friday, October 3, 2014 4:34 PM
  • public class Parent
     {

           
    private List<child> childern = new List<child>();

           
    public List<child> Childern {
               
    get
               
    {
                   
    return childern;
               
    }
                set
               
    {
                    childern = value
    ;
               
    }
            }
     }

    var parent = new Parent();

    By doing the above, Childern gets automatically instanciated when Parent is instanciated.
     

    • Marked as answer by svt44cobra66 Friday, October 3, 2014 9:38 PM
    Friday, October 3, 2014 5:19 PM
  • thanks, this looks like what I was looking for as well.

    Friday, October 3, 2014 9:40 PM
  • By doing the above, Childerngets automatically instanciated when Parent is instanciated.

    I think the above is not true. If you want to do like above you must make an instance of the child in the parent constructor.

     public class Parent
            {
                private List<Child> childern;
    
                public Parent()
                {
                    childern = new List<Child>();
                }          
    
                public List<Child> Childern
                {
                    get
                    {
                        return childern;
                    }
                    set
                    {
                        childern = value;
                    }
                }
            }

    Saturday, October 4, 2014 3:04 AM
  •                     

    By doing the above, Childerngets automatically instanciated when Parent is instanciated.

    -------------------------------------------------------------------------------------------I think the above is not true. If you want to do like above you must make an instance of the child in the parent constructor.

    You flat-out do not know what you are talking about.

    The private children a List<T> of child objects is a backing stor for the Public Children List<T>, which has been instantiated. So therefore,  Children is instantiated when Parent is instantiated, because its backing stor has been instanciated. I use all the time.

    The below is another example of instantiating a class that has private backing objects that the Public Properties that are objects are using. RebateWizardPagesModel gets instantiated, the private backing objects are instanciated too, and therefore, the Public facing Properties that are objects that the private backing objects belong to are instantiated.   

    I have been using this technique since around 2003. Why don't you try it and pull the wool off of your eyes.

    ---------------------------------------------------------------

    using CEEPRebateCore;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    using CEEPRebatePortal.Models.RebateSharedModels;

    namespace CEEPRebatePortal.Models.RebateWizardModels
    {
        public class RebateWizardPagesModel
        {
            private RebateWizardPage1Model page1 = new RebateWizardPage1Model();
            private RebateWizardPage2Model page2 = new RebateWizardPage2Model();
            private RebateWizardPage3Model page3 = new RebateWizardPage3Model();
          
            public RebateWizardPage1Model Page1
            {
                get { return page1; }
                set { page1 = value; }
            }

            public RebateWizardPage2Model Page2
            {
                get { return page2; }
                set { page2 = value; }
            }

            public RebateWizardPage3Model Page3
            {
                get { return page3; }
                set { page3 = value; }
            }

            public string Category
            {
                get { return page1.Category; }
            }

            public string WorkSiteRegion
            {
                get
                {
                    State state = State.FromDatabaseCode(page1.WorkSiteAddress.State);
                    if (state != null)
                    {
                        return state.ToString();
                    }
                    return "---";
                }
            }
        }
    }

    Saturday, October 4, 2014 10:42 AM