Creating an object with methods problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • !NoItAll
    Contributor
    • May 2006
    • 297

    Creating an object with methods problem

    Ok - stuck on something I clearly don't understand well....

    In the code below (which I just wrote here - it closely mirrors what my actual code is...) the problem is with the line:

    Thing.ThingObje ct.Add(MyObject )

    It says: "Object reference not set to an instance of an object"

    Yes - it is "nothing" before I try to use the Add method so I must be doing something basic and wrong.
    The object I am trying to add is fine - I can tell you it is correct, it's just that I apparently cannot use the .add method on the Thing.ThingObje ct until it is properly created - which I have apparently not done.
    I'm hoping this is just a syntactical thing.....

    Code:
    Private Sub DoThing()
    
          Dim MyObject as New Object
    
          MyObject = MakeMyObject
    
          Dim Thing as New ListOfThings
    
          Thing.ThingObject.Add(MyObject)
    
    End Sub
    
    Partial Public Class ListOfThings
       Inherits Object
       Private vThingObject as ArrayOfThings
    
       Public Property ThingObject as ArrayOfThings
    
           Get
               return vThingObject
           End Get
           Set
                vThingObject = Value
           End Set
       End Property
    
    End Class
    
    Public Class ArrayOfThings
         Inherits System.Collections.Generic.List(of Objects)
    End Class
  • phvfl
    Recognized Expert New Member
    • Aug 2007
    • 173

    #2
    Hi,

    You are creating an instance of the ListOfThings class - but the member vThingObject is not having an instance created. i.e. Thing is not nothing but Thing.ThingObje ct is nothing.

    Assuming that ArrayOfThings has a parameterless constructor add the following to the ListOfThings class:
    Code:
    Public Sub New()
     vThingObject = New ArrayOfThings()
    End Sub

    Comment

    • !NoItAll
      Contributor
      • May 2006
      • 297

      #3
      Yup - that was it. I found that, for simplicity, all I needed to do was to declare the private variable within the class with the NEW constructor.
      Thanks phvfl!

      Comment

      Working...