CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2010
    Posts
    7

    [RESOLVED] Help instantiating objects embedded in object

    I am having some real issues with this one. I have a class which contains two other objects...but I cannot seem to instantiate those sub objects. Here is the code for my class:
    Code:
       
    Option Explicit On
    Public Class Boot
        'A collection of Heads and Shells
    #Region "Private Variables"
        Private _oHead As New WQS_BusinessEntity.Head
        Private _oShell As New WQS_BusinessEntity.Shell
        Private _iBootID As Integer
        Private _iBootTypeID As Integer
        Private _iVesselID As Integer
    
    
    
    #End Region
    
    #Region "Constructors"
        Public Sub New()
    
        End Sub
        Public Sub New(ByVal iBootID As Integer, ByVal iBootTypeID As Integer, ByVal iVesselID As Integer)
            _oHead = New WQS_BusinessEntity.Head
            _oShell = New WQS_BusinessEntity.Shell
            _iBootID = iBootID
            _iBootTypeID = iBootTypeID
            _iVesselID = iVesselID
    
        End Sub
    #End Region
    
    #Region "Public Properties"
        Public Property oHead() As Head
            Get
                Return _oHead
            End Get
            Set(ByVal oValue As Head)
                _oHead = oValue
            End Set
        End Property
    
        Public Property oShell() As Shell
            Get
                Return _oShell
            End Get
            Set(ByVal oValue As Shell)
                _oShell = oValue
            End Set
        End Property
    
        Public Property iBootID() As Integer
            Get
                Return _iBootID
            End Get
            Set(ByVal iValue As Integer)
                _iBootID = iValue
            End Set
        End Property
        Public Property iBootTypeID() As Integer
            Get
                Return _iBootTypeID
            End Get
            Set(ByVal iValue As Integer)
                _iBootTypeID = iValue
            End Set
        End Property
        Public Property iVesselID() As Integer
            Get
                Return _iVesselID
            End Get
            Set(ByVal iValue As Integer)
                _iVesselID = iValue
            End Set
        End Property
    #End Region
    End Class
    
    
    Public Class BootSet
        Inherits Collections.ArrayList
    
        Public Shadows Function Add(ByVal oValue As Boot) As Integer
            If Not oValue Is Nothing Then
                MyBase.Add(oValue)
              
            End If
        End Function
    
        Default Public Shadows Property Item(ByVal iIndex As Integer) As Boot
            Get
                Return CType(MyBase.Item(iIndex), Boot)
    
            End Get
            Set(ByVal Value As Boot)
                MyBase.Item(iIndex) = Value
            End Set
        End Property
    
    End Class

    When I am using my Boot Object, I do so Here in my DAL:

    Code:
      Dim oRow As DataRow
            Dim oSet As New WQS_BusinessEntity.BootSet
            Dim iCount As Integer = 0
            'Dim iTable As Integer
            'iTable = oDS.Tables.Count - 1
            For Each oRow In oDS.Tables(1).Rows
                Dim oItem As New WQS_BusinessEntity.Boot
             
                With oItem
    
                  
                    'Boot Data
                    .iBootID = oDS.Tables(0).Rows(iCount).Item("BootID")
                    .iBootTypeID = oDS.Tables(1).Rows(iCount).Item("BootFunctionTypeId")
                    .iVesselID = oDS.Tables(1).Rows(iCount).Item("VesselId")
                    '1 head, 1 shell per boot now.
    
                    'Head Data
                    oItem.oHead.iHeadID = ZeroIfNull(oDS.Tables(1).Rows(iCount).Item("HeadId"))
                    oItem.oHead.iVesselID = ZeroIfNull(oDS.Tables(1).Rows(iCount).Item("VesselId"))
                    oItem.oHead.iMaterialID = ZeroIfNull(oDS.Tables(1).Rows(iCount).Item("HeadMaterialId"))
                    oItem.oHead.iLocationID = ZeroIfNull(oDS.Tables(1).Rows(iCount).Item("HeadLocationId"))
                    oItem.oHead.strMaterial = oDS.Tables(1).Rows(iCount).Item("HeadMaterialName")
                    oItem.oHead.dDiameter = ZeroIfNull(oDS.Tables(1).Rows(iCount).Item("HeadDiameter"))
                    oItem.oHead.dCurrentThickness = ZeroIfNull(oDS.Tables(1).Rows(iCount).Item("HeadCurrentThickness"))
                    oItem.oHead.dNominalThickness = ZeroIfNull(oDS.Tables(1).Rows(iCount).Item("HeadNominalThickness"))
                    oItem.oHead.strShape = oDS.Tables(1).Rows(iCount).Item("HeadShapeName")
                    oItem.oHead.iShapeID = ZeroIfNull(oDS.Tables(1).Rows(iCount).Item("HeadShapeId"))
                    oItem.oHead.strLocation = EmptyIfNull(oDS.Tables(1).Rows(iCount).Item("HeadLocationOnVesselName"))
                    'Shell(Data)
                    oItem.oShell.iShellID = ZeroIfNull(oDS.Tables(1).Rows(iCount).Item("ShellId"))
                    oItem.oShell.iVesselID = ZeroIfNull(oDS.Tables(1).Rows(iCount).Item("VesselId"))
                    oItem.oShell.iMaterialID = ZeroIfNull(oDS.Tables(1).Rows(iCount).Item("ShellMaterialId"))
                    oItem.oShell.strMaterial = EmptyIfNull(oDS.Tables(1).Rows(iCount).Item("ShellMaterialName"))
                    oItem.oShell.dDiameter = ZeroIfNull(oDS.Tables(1).Rows(iCount).Item("ShellDiameter"))
                    oItem.oShell.dCurrentThickness = ZeroIfNull(oDS.Tables(1).Rows(iCount).Item("ShellCurrentThickness"))
                    oItem.oShell.dNominalThickness = ZeroIfNull(oDS.Tables(1).Rows(iCount).Item("ShellNominalThickness"))
                    Select Case oDS.Tables(1).Rows(iCount).Item("ShellOrientation")
                        Case 0.0
                            oItem.oShell.cOrientation = "H"
                        Case 90.0
                            oItem.oShell.cOrientation = "V"
                        Case Else
                            'default to horizontal
                            oItem.oShell.cOrientation = "H"
                    End Select
    
                End With
                'Add the Boot to the BootSet
                oSet.Add(oItem)
                iCount = iCount + 1
    
            Next
    
            Return oSet
        End Function
    However, when I place a breakpoint on the 'Return oSET' line, and add a watch, this is what I see:
    Code:
    -		oSet	Count = 5	WQS_BusinessEntity.BootSet
    -		(0)	{WQS_BusinessEntity.Boot}	Object
    -		WQS_BusinessEntity.Boot	{WQS_BusinessEntity.Boot}	WQS_BusinessEntity.Boot
    		_iBootID	220	Integer
    		_iBootTypeID	0	Integer
    		_iVesselID	30	Integer
    +		_oHead	Count = 0	WQS_BusinessEntity.Head
    +		_oShell	Count = 0	WQS_BusinessEntity.Shell
    		iBootID	220	Integer
    		iBootTypeID	0	Integer
    		iVesselID	30	Integer
    +		oHead	Count = 0	WQS_BusinessEntity.Head
    +		oShell	Count = 0	WQS_BusinessEntity.Shell
    +		(1)	{WQS_BusinessEntity.Boot}	Object
    +		(2)	{WQS_BusinessEntity.Boot}	Object
    +		(3)	{WQS_BusinessEntity.Boot}	Object
    +		(4)	{WQS_BusinessEntity.Boot}	Object
    Notice the count for my two objects, Ohead, and oShell, is zero. Im not sure why this doesnt instantiate properly, as I believe is what is going on. I could really use some help!

  2. #2
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Arrow Re: Help instantiating objects embedded in object

    How are the following defined?
    WQS_BusinessEntity.Head
    WQS_BusinessEntity.Shell

    I suspect you defined these with some type of list inheritance, which is causing the output to display COUNT. I do not see that you actually add any items to the head/shell lists, just assign values to the other properties. So in that regard, it will always display COUNT=0, but if you expand the node in your watch, I bet it shows values for the other properties (assuming there are values).

    For example: iHeadID, iVesselID, etc.
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

  3. #3
    Join Date
    Jan 2010
    Posts
    7

    Re: Help instantiating objects embedded in object

    Thank you for responding. Yes, I do see values for the other properties of the boot. I have been working with the code a bit, and have simplified it to illustrate the issue I am having.

    So, I have three classes. One is called Head,One is Shell and one is boot.
    The boot as you saw is to contain a headset, and a shellset, and has a couple of other properties. The headset and shellset can contain one to many heads/shells.

    Here is my simplified head class:
    Code:
    Option Explicit On
    Public Class Head
        Inherits BusinessEntity.HeadSet
    #Region "Private Variables"
        Private _iVesselID As Integer
        Private _iHeadID As Integer
    #End Region
    
    #Region "Constructors"
        Public Sub New()
    
        End Sub
        Public Sub New(ByVal iVesselID As Integer, ByVal iHeadID As Integer)
            _iVesselID = iVesselID
            _iHeadID = iHeadID
        End Sub
    #End Region
    
    #Region "Public Properties"
        Public Property iVesselID() As Integer
            Get
                Return _iVesselID
            End Get
            Set(ByVal iValue As Integer)
                _iVesselID = iValue
            End Set
        End Property
        Public Property iHeadID() As Integer
            Get
                Return _iHeadID
            End Get
            Set(ByVal iValue As Integer)
                _iHeadID = iValue
            End Set
        End Property
    #End Region
    
    End Class
    Public Class HeadSet
        Inherits Collections.ArrayList
    
        Public Shadows Function Add(ByVal oValue As Head) As Integer
            If Not oValue Is Nothing Then
                MyBase.Add(oValue)
            End If
        End Function
    
        Default Public Shadows Property Item(ByVal iIndex As Integer) As Head
            Get
                Return CType(MyBase.Item(iIndex), Head)
    
            End Get
            Set(ByVal Value As Head)
                MyBase.Item(iIndex) = Value
            End Set
        End Property
    
    End Class
    and here is my Shell class
    Code:
    Option Explicit On
    Public Class Shell
        Inherits BusinessEntity.ShellSet
    
    #Region "Private Variables"
        Private _iVesselID As Integer
        Private _iShellID As Integer
    #End Region
    
    #Region "Constructors"
        Public Sub New()
    
        End Sub
        Public Sub New(ByVal iVesselID As Integer, ByVal iShellID As Integer)
            _iVesselID = iVesselID
            _iShellID = iShellID
        End Sub
    #End Region
    
    #Region "Public Properties"
        Public Property iVesselID() As Integer
            Get
                Return _iVesselID
            End Get
            Set(ByVal iValue As Integer)
                _iVesselID = iValue
            End Set
        End Property
        Public Property iShellID() As Integer
            Get
                Return _iShellID
            End Get
            Set(ByVal iValue As Integer)
                _iShellID = iValue
            End Set
        End Property
    #End Region
    End Class
    
    Public Class ShellSet
        Inherits Collections.ArrayList
    
        Public Shadows Function Add(ByVal oValue As Shell) As Integer
            If Not oValue Is Nothing Then
                MyBase.Add(oValue)
            End If
        End Function
    
        Default Public Shadows Property Item(ByVal iIndex As Integer) As Shell
            Get
                Return CType(MyBase.Item(iIndex), Shell)
    
            End Get
            Set(ByVal Value As Shell)
                MyBase.Item(iIndex) = Value
            End Set
        End Property
    
    End Class
    and Finally, my boot class:
    Code:
    Option Explicit On
    Public Class Boot
        'Contains a head and shell
    #Region "Private Variables"
        Private _oHeadSet As New HeadSet
        Private _oShellSet As New ShellSet
        Private _iBootID As Integer
        Private _iVesselID As Integer
    #End Region
    
    #Region "Constructors"
        Public Sub New()
            _oHeadSet = New BusinessEntity.HeadSet
            Dim oHead As New Head
            _oHeadSet.Add(oHead)
            _oShellSet = New BusinessEntity.ShellSet
            Dim oShell As New Shell
            _oShellSet.Add(oShell)
    
        End Sub
        Public Sub New(ByVal oHeadSet As HeadSet, ByVal oShellSet As ShellSet, ByVal iBootID As Integer, ByVal iVesselID As Integer)
            _oHeadSet = New BusinessEntity.HeadSet
            _oShellSet = New BusinessEntity.ShellSet
            Dim oHead As New Head
            Dim oShell As New Shell
            _oHeadSet.Add(oHead)
            _oShellSet.Add(oShell)
            _iBootID = iBootID
            _iVesselID = iVesselID
    
        End Sub
    #End Region
    
    #Region "Public Properties"
        Public Property oHeadSet() As BusinessEntity.HeadSet
            Get
                Return _oHeadSet
            End Get
            Set(ByVal oValue As HeadSet)
                _oHeadSet = oValue
            End Set
        End Property
    
        Public Property oShellSet() As BusinessEntity.ShellSet
            Get
                Return _oShellSet
            End Get
            Set(ByVal oValue As ShellSet)
                _oShellSet = oValue
            End Set
        End Property
    
        Public Property iBootID() As Integer
            Get
                Return _iBootID
            End Get
            Set(ByVal iValue As Integer)
                _iBootID = iValue
            End Set
        End Property
      
        Public Property iVesselID() As Integer
            Get
                Return _iVesselID
            End Get
            Set(ByVal iValue As Integer)
                _iVesselID = iValue
            End Set
        End Property
    #End Region
    End Class
    
    
    Public Class BootSet
        Inherits Collections.ArrayList
    
        Public Shadows Function Add(ByVal oValue As Boot) As Integer
            If Not oValue Is Nothing Then
                MyBase.Add(oValue)
    
            End If
        End Function
    
        Default Public Shadows Property Item(ByVal iIndex As Integer) As Boot
            Get
                Return CType(MyBase.Item(iIndex), Boot)
    
            End Get
            Set(ByVal Value As Boot)
                MyBase.Item(iIndex) = Value
            End Set
        End Property
    
    End Class
    and a method which builds the boot (still not working correctly, same counts)
    Code:
    Partial Public Class _Default
        Inherits System.Web.UI.Page
    
        Public oHead As BusinessEntity.Head
        Public oHeadSet As BusinessEntity.HeadSet
        Public oShell As BusinessEntity.Shell
        Public oShellSet As BusinessEntity.ShellSet
        Public oBoot As BusinessEntity.Boot
        Public oBootSet As BusinessEntity.BootSet
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       
            BuildEM()
    
    
        End Sub
        Public Function BuildEM() As BusinessEntity.BootSet
            oHead = New BusinessEntity.Head
            oHeadSet = New BusinessEntity.HeadSet
            oShell = New BusinessEntity.Shell
            oShellSet = New BusinessEntity.ShellSet
            oBootSet = New BusinessEntity.BootSet
    
            oHead.iHeadID = 1
            oHeadSet.Add(oHead)
            oShell.iShellID = 1
            oShellSet.Add(oShell)
            oBoot = New BusinessEntity.Boot
         
            oBoot.iBootID = 1
    
            oBoot.iVesselID = 30
    
            oBootSet.Add(oBoot)
            Return oBootSet
        End Function
    End Class
    Thanks for your assistance!

  4. #4
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Arrow Re: Help instantiating objects embedded in object

    The code you posted is doing exactly what it should, so can you explain what you're trying to achieve?

    Just one quick note:

    In your "BuildEM" method, you create an ohead,oheadset,oshell, and oshellset, but you don't do anything with them after they are created. Did you mean to add them to your oboot object?
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

  5. #5
    Join Date
    Jan 2010
    Posts
    7

    Re: Help instantiating objects embedded in object

    Yes, what I am trying to do is

    1) Create a BootSet,HeadSet, and ShellSet object. A bootset contains a boot, which is to contain a headset and a shellset.
    A headset can contain one or more heads. A shellset can contain one or more shells.

    2) Add the head to the headset, add the shell to the shellset.
    3) add the Headset, with the contained head, to the boot
    4) add the Shellset, with the contained shell, to the boot
    5) add the boot to the bootset

    What I expect to see in my watch on the BootSet is 1 boot, with a shellset and headset, each containing one shell and one head respectivly. Im not sure why I cant get this to work, but I always see a count of zero for heads and shells in their respective sets. I need to be able to access the properties of the head and shells from the bootset.

  6. #6
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Arrow Re: Help instantiating objects embedded in object

    Ok, I think I've got it.

    Are you talking about the 0's highlighted in the attached picture?

    The Head and Shell object are still there, but their properties are not displayed because of the way you have things declared.

    For the sake of clarity, remove the inheritance from the SHELL and HEAD classes.

    For example:

    Code:
        Public Class Head
            Inherits BusinessEntity.HeadSet
    Should be:

    Code:
        Public Class Head
            'COMMENTED OUT: Inherits BusinessEntity.HeadSet
    Attached Images Attached Images
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

  7. #7
    Join Date
    Jan 2010
    Posts
    7

    Re: Help instantiating objects embedded in object

    Well. That did it! Thanks so much. Commenting out the inheritance did allow me to see the shell and head object properties through a watch. And after that, I figured out all I had to do was type in the following into the command window

    ? oBootSet(0).oShellSet(0).iShellID

    Which did return the shellid, with the inheritance lines uncommented as well! Your help is most appreciated!

  8. #8
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Help instantiating objects embedded in object

    Great thread.

    It is always nice to see new members following forum rules and protocols. Thank you for using CODE tags pdidow

    Great answers as usual Craig!

    pdidow, seeing the fact that this thread is resolved, please mark it as such, have a look at this post explaining the method :

    http://www.codeguru.com/forum/showthread.php?t=403073

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured