Including a class as a member of another class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Cobelli

    Including a class as a member of another class

    I am trying to include two classes as members of another class in a
    webservice. The definitions look basically like this:

    Public Class clsLender
    Public ID As String
    Public Name As String
    End Class
    ----------------------------------------------------------------------------------------
    Namespace DataTypesVB.Enu merations
    Public Class DefaultValues
    Public Name As String
    Public Lenders() As clsLender
    End Class
    End Namespace

    Then the code looks like this:

    <WebMethod()> Public Function GetDefaultValue s( ByVal some value As Guid) As
    DataTypesVB.Enu merations.Defau ltValues
    Dim i As Integer
    Dim DefaultValues As New DataTypesVB.Enu merations.Defau ltValues()
    With DefaultValues
    .Name = some value
    End With

    For i = 0 To some number
    With DefaultValues
    ReDim Preserve .Lenders(i)
    .Lenders(i).ID = some value
    .Lenders(i).Nam e = some value
    End With
    Next i
    Return DefaultValues
    End Function

    When I run this web service (quite a bit of code has been cut for
    readability) I get the following error:

    <faultstring>Se rver was unable to process request. --> Object reference not
    set to an instance of an object.</faultstring>

    Please help me understand where my failure is and how I can fix it. Thanks
    in advance!

    - Jeff


  • pritiphadke@gmail.com

    #2
    Re: Including a class as a member of another class

    Try

    Namespace DataTypesVB.Enu merations
    Public Class DefaultValues
    Public Name As String
    Public Lenders() As New clsLender
    End Class
    End Namespace

    Comment

    • Robby

      #3
      Re: Including a class as a member of another class


      You should not give your variables the same name as the class name. It is
      confusing.

      This statement ...

      Dim DefaultValues As New DataTypesVB.Enu merations.Defau ltValues()

      creates an empty array of DataTypesVB.Enu merations.Defau ltValues. So that
      when these lines are executed ...

      With DefaultValues
      .Name = some value
      End With

      you get the "Object reference not set to an instance of an object" error.
      Remove the opening and closing brackets at the end of the declaration and
      things should work better.

      More importantly, do not give your variable names the same name as the class
      name. This is extremely poor programming practice. It makes it makes it
      difficult to see if you are invoking an instance or shared property or
      procedure. While it does not count in this small example when you get to
      larger programs you will have headaches.

      Robby



      "Jeff Cobelli" <jcobelli@theim pactgroup.com> wrote in message
      news:OunTYP38EH A.3700@tk2msftn gp13.phx.gbl...[color=blue]
      >I am trying to include two classes as members of another class in a
      >webservice. The definitions look basically like this:
      >
      > Public Class clsLender
      > Public ID As String
      > Public Name As String
      > End Class
      > ----------------------------------------------------------------------------------------
      > Namespace DataTypesVB.Enu merations
      > Public Class DefaultValues
      > Public Name As String
      > Public Lenders() As clsLender
      > End Class
      > End Namespace
      >
      > Then the code looks like this:
      >
      > <WebMethod()> Public Function GetDefaultValue s( ByVal some value As Guid)
      > As DataTypesVB.Enu merations.Defau ltValues
      > Dim i As Integer
      > Dim DefaultValues As New DataTypesVB.Enu merations.Defau ltValues()
      > With DefaultValues
      > .Name = some value
      > End With
      >
      > For i = 0 To some number
      > With DefaultValues
      > ReDim Preserve .Lenders(i)
      > .Lenders(i).ID = some value
      > .Lenders(i).Nam e = some value
      > End With
      > Next i
      > Return DefaultValues
      > End Function
      >
      > When I run this web service (quite a bit of code has been cut for
      > readability) I get the following error:
      >
      > <faultstring>Se rver was unable to process request. --> Object reference
      > not set to an instance of an object.</faultstring>
      >
      > Please help me understand where my failure is and how I can fix it.
      > Thanks in advance!
      >
      > - Jeff
      >[/color]


      Comment

      • Jeff Cobelli

        #4
        Re: Including a class as a member of another class

        Hi Robby - thanks for your response. I took your advice and removed the
        opening and closing parenthesis at the end of the variable declaration (this
        also required me to remove the "New" keyword). Anyway, the same error still
        gets returned. The web service is not failing on the line where I assign a
        value to DefaultValues.N ame. The problem is occurring somewhere in the
        following code:

        For i = 0 To some number
        With DefaultValues
        ReDim Preserve .Lenders(i)
        .Lenders(i).ID = some value
        .Lenders(i).Nam e = some value
        End With
        Next i

        I am assuming it has something to do with redimensioning the Lenders class.
        I know it is here because if I comment this section out the web service runs
        fine and returns the expected result. Do you have any other ideas for
        resolving this problem?

        Thanks also for your suggestions regarding naming conventions. I'll
        definitely put your advice into practice.

        Thanks in advance,

        - Jeff

        "Robby" <edmund@not.my. email.com> wrote in message
        news:uxNF%23838 EHA.2572@tk2msf tngp13.phx.gbl. ..[color=blue]
        >
        > You should not give your variables the same name as the class name. It is
        > confusing.
        >
        > This statement ...
        >
        > Dim DefaultValues As New DataTypesVB.Enu merations.Defau ltValues()
        >
        > creates an empty array of DataTypesVB.Enu merations.Defau ltValues. So that
        > when these lines are executed ...
        >
        > With DefaultValues
        > .Name = some value
        > End With
        >
        > you get the "Object reference not set to an instance of an object" error.
        > Remove the opening and closing brackets at the end of the declaration and
        > things should work better.
        >
        > More importantly, do not give your variable names the same name as the
        > class name. This is extremely poor programming practice. It makes it
        > makes it difficult to see if you are invoking an instance or shared
        > property or procedure. While it does not count in this small example when
        > you get to larger programs you will have headaches.
        >
        > Robby
        >
        >
        >
        > "Jeff Cobelli" <jcobelli@theim pactgroup.com> wrote in message
        > news:OunTYP38EH A.3700@tk2msftn gp13.phx.gbl...[color=green]
        >>I am trying to include two classes as members of another class in a
        >>webservice. The definitions look basically like this:
        >>
        >> Public Class clsLender
        >> Public ID As String
        >> Public Name As String
        >> End Class
        >> ----------------------------------------------------------------------------------------
        >> Namespace DataTypesVB.Enu merations
        >> Public Class DefaultValues
        >> Public Name As String
        >> Public Lenders() As clsLender
        >> End Class
        >> End Namespace
        >>
        >> Then the code looks like this:
        >>
        >> <WebMethod()> Public Function GetDefaultValue s( ByVal some value As Guid)
        >> As DataTypesVB.Enu merations.Defau ltValues
        >> Dim i As Integer
        >> Dim DefaultValues As New DataTypesVB.Enu merations.Defau ltValues()
        >> With DefaultValues
        >> .Name = some value
        >> End With
        >>
        >> For i = 0 To some number
        >> With DefaultValues
        >> ReDim Preserve .Lenders(i)
        >> .Lenders(i).ID = some value
        >> .Lenders(i).Nam e = some value
        >> End With
        >> Next i
        >> Return DefaultValues
        >> End Function
        >>
        >> When I run this web service (quite a bit of code has been cut for
        >> readability) I get the following error:
        >>
        >> <faultstring>Se rver was unable to process request. --> Object reference
        >> not set to an instance of an object.</faultstring>
        >>
        >> Please help me understand where my failure is and how I can fix it.
        >> Thanks in advance!
        >>
        >> - Jeff
        >>[/color]
        >
        >[/color]


        Comment

        • Robby

          #5
          Re: Including a class as a member of another class


          So sorry, the New keyword line is ok. I was a little confused by the
          variable name with the class name. So the real problem is that ...

          ReDim Preserve .Lenders(i)

          extends the DefaultValues.L enders array with one more entry BUT it does not
          initalize that entry. Therefore, you need to initalize the entry before you
          invoke its instance properties or procedures.
          [color=blue]
          > With DefaultValues
          > ReDim Preserve .Lenders(i)[/color]
          * .Lenders(i) = New clsLender()[color=blue]
          > .Lenders(i).ID = some value
          > .Lenders(i).Nam e = some value
          > End With[/color]

          Robby


          "Jeff Cobelli" <jcobelli@theim pactgroup.com> wrote in message
          news:%23$8mxZ58 EHA.3840@tk2msf tngp13.phx.gbl. ..[color=blue]
          > Hi Robby - thanks for your response. I took your advice and removed the
          > opening and closing parenthesis at the end of the variable declaration
          > (this also required me to remove the "New" keyword). Anyway, the same
          > error still gets returned. The web service is not failing on the line
          > where I assign a value to DefaultValues.N ame. The problem is occurring
          > somewhere in the following code:
          >
          > For i = 0 To some number
          > With DefaultValues
          > ReDim Preserve .Lenders(i)
          > .Lenders(i).ID = some value
          > .Lenders(i).Nam e = some value
          > End With
          > Next i
          >
          > I am assuming it has something to do with redimensioning the Lenders
          > class. I know it is here because if I comment this section out the web
          > service runs fine and returns the expected result. Do you have any other
          > ideas for resolving this problem?
          >
          > Thanks also for your suggestions regarding naming conventions. I'll
          > definitely put your advice into practice.
          >
          > Thanks in advance,
          >
          > - Jeff
          >
          > "Robby" <edmund@not.my. email.com> wrote in message
          > news:uxNF%23838 EHA.2572@tk2msf tngp13.phx.gbl. ..[color=green]
          >>
          >> You should not give your variables the same name as the class name. It
          >> is confusing.
          >>
          >> This statement ...
          >>
          >> Dim DefaultValues As New DataTypesVB.Enu merations.Defau ltValues()
          >>
          >> creates an empty array of DataTypesVB.Enu merations.Defau ltValues. So that
          >> when these lines are executed ...
          >>
          >> With DefaultValues
          >> .Name = some value
          >> End With
          >>
          >> you get the "Object reference not set to an instance of an object" error.
          >> Remove the opening and closing brackets at the end of the declaration and
          >> things should work better.
          >>
          >> More importantly, do not give your variable names the same name as the
          >> class name. This is extremely poor programming practice. It makes it
          >> makes it difficult to see if you are invoking an instance or shared
          >> property or procedure. While it does not count in this small example
          >> when you get to larger programs you will have headaches.
          >>
          >> Robby
          >>
          >>
          >>
          >> "Jeff Cobelli" <jcobelli@theim pactgroup.com> wrote in message
          >> news:OunTYP38EH A.3700@tk2msftn gp13.phx.gbl...[color=darkred]
          >>>I am trying to include two classes as members of another class in a
          >>>webservice . The definitions look basically like this:
          >>>
          >>> Public Class clsLender
          >>> Public ID As String
          >>> Public Name As String
          >>> End Class
          >>> ----------------------------------------------------------------------------------------
          >>> Namespace DataTypesVB.Enu merations
          >>> Public Class DefaultValues
          >>> Public Name As String
          >>> Public Lenders() As clsLender
          >>> End Class
          >>> End Namespace
          >>>
          >>> Then the code looks like this:
          >>>
          >>> <WebMethod()> Public Function GetDefaultValue s( ByVal some value As
          >>> Guid) As DataTypesVB.Enu merations.Defau ltValues
          >>> Dim i As Integer
          >>> Dim DefaultValues As New DataTypesVB.Enu merations.Defau ltValues()
          >>> With DefaultValues
          >>> .Name = some value
          >>> End With
          >>>
          >>> For i = 0 To some number
          >>> With DefaultValues
          >>> ReDim Preserve .Lenders(i)
          >>> .Lenders(i).ID = some value
          >>> .Lenders(i).Nam e = some value
          >>> End With
          >>> Next i
          >>> Return DefaultValues
          >>> End Function
          >>>
          >>> When I run this web service (quite a bit of code has been cut for
          >>> readability) I get the following error:
          >>>
          >>> <faultstring>Se rver was unable to process request. --> Object reference
          >>> not set to an instance of an object.</faultstring>
          >>>
          >>> Please help me understand where my failure is and how I can fix it.
          >>> Thanks in advance!
          >>>
          >>> - Jeff
          >>>[/color]
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • Jeff Cobelli

            #6
            Re: Including a class as a member of another class

            You are the best, thank you!

            - Jeff

            "Robby" <edmund@not.my. email.com> wrote in message
            news:uGx%23$g78 EHA.3596@TK2MSF TNGP12.phx.gbl. ..[color=blue]
            >
            > So sorry, the New keyword line is ok. I was a little confused by the
            > variable name with the class name. So the real problem is that ...
            >
            > ReDim Preserve .Lenders(i)
            >
            > extends the DefaultValues.L enders array with one more entry BUT it does
            > not initalize that entry. Therefore, you need to initalize the entry
            > before you invoke its instance properties or procedures.
            >[color=green]
            >> With DefaultValues
            >> ReDim Preserve .Lenders(i)[/color]
            > * .Lenders(i) = New clsLender()[color=green]
            >> .Lenders(i).ID = some value
            >> .Lenders(i).Nam e = some value
            >> End With[/color]
            >
            > Robby
            >
            >
            > "Jeff Cobelli" <jcobelli@theim pactgroup.com> wrote in message
            > news:%23$8mxZ58 EHA.3840@tk2msf tngp13.phx.gbl. ..[color=green]
            >> Hi Robby - thanks for your response. I took your advice and removed the
            >> opening and closing parenthesis at the end of the variable declaration
            >> (this also required me to remove the "New" keyword). Anyway, the same
            >> error still gets returned. The web service is not failing on the line
            >> where I assign a value to DefaultValues.N ame. The problem is occurring
            >> somewhere in the following code:
            >>
            >> For i = 0 To some number
            >> With DefaultValues
            >> ReDim Preserve .Lenders(i)
            >> .Lenders(i).ID = some value
            >> .Lenders(i).Nam e = some value
            >> End With
            >> Next i
            >>
            >> I am assuming it has something to do with redimensioning the Lenders
            >> class. I know it is here because if I comment this section out the web
            >> service runs fine and returns the expected result. Do you have any other
            >> ideas for resolving this problem?
            >>
            >> Thanks also for your suggestions regarding naming conventions. I'll
            >> definitely put your advice into practice.
            >>
            >> Thanks in advance,
            >>
            >> - Jeff
            >>
            >> "Robby" <edmund@not.my. email.com> wrote in message
            >> news:uxNF%23838 EHA.2572@tk2msf tngp13.phx.gbl. ..[color=darkred]
            >>>
            >>> You should not give your variables the same name as the class name. It
            >>> is confusing.
            >>>
            >>> This statement ...
            >>>
            >>> Dim DefaultValues As New DataTypesVB.Enu merations.Defau ltValues()
            >>>
            >>> creates an empty array of DataTypesVB.Enu merations.Defau ltValues. So
            >>> that when these lines are executed ...
            >>>
            >>> With DefaultValues
            >>> .Name = some value
            >>> End With
            >>>
            >>> you get the "Object reference not set to an instance of an object"
            >>> error. Remove the opening and closing brackets at the end of the
            >>> declaration and things should work better.
            >>>
            >>> More importantly, do not give your variable names the same name as the
            >>> class name. This is extremely poor programming practice. It makes it
            >>> makes it difficult to see if you are invoking an instance or shared
            >>> property or procedure. While it does not count in this small example
            >>> when you get to larger programs you will have headaches.
            >>>
            >>> Robby
            >>>
            >>>
            >>>
            >>> "Jeff Cobelli" <jcobelli@theim pactgroup.com> wrote in message
            >>> news:OunTYP38EH A.3700@tk2msftn gp13.phx.gbl...
            >>>>I am trying to include two classes as members of another class in a
            >>>>webservic e. The definitions look basically like this:
            >>>>
            >>>> Public Class clsLender
            >>>> Public ID As String
            >>>> Public Name As String
            >>>> End Class
            >>>> ----------------------------------------------------------------------------------------
            >>>> Namespace DataTypesVB.Enu merations
            >>>> Public Class DefaultValues
            >>>> Public Name As String
            >>>> Public Lenders() As clsLender
            >>>> End Class
            >>>> End Namespace
            >>>>
            >>>> Then the code looks like this:
            >>>>
            >>>> <WebMethod()> Public Function GetDefaultValue s( ByVal some value As
            >>>> Guid) As DataTypesVB.Enu merations.Defau ltValues
            >>>> Dim i As Integer
            >>>> Dim DefaultValues As New DataTypesVB.Enu merations.Defau ltValues()
            >>>> With DefaultValues
            >>>> .Name = some value
            >>>> End With
            >>>>
            >>>> For i = 0 To some number
            >>>> With DefaultValues
            >>>> ReDim Preserve .Lenders(i)
            >>>> .Lenders(i).ID = some value
            >>>> .Lenders(i).Nam e = some value
            >>>> End With
            >>>> Next i
            >>>> Return DefaultValues
            >>>> End Function
            >>>>
            >>>> When I run this web service (quite a bit of code has been cut for
            >>>> readability) I get the following error:
            >>>>
            >>>> <faultstring>Se rver was unable to process request. --> Object reference
            >>>> not set to an instance of an object.</faultstring>
            >>>>
            >>>> Please help me understand where my failure is and how I can fix it.
            >>>> Thanks in advance!
            >>>>
            >>>> - Jeff
            >>>>
            >>>
            >>>[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            Working...