Using a Structure in a Class

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

    #1

    Using a Structure in a Class

    Hi all,

    I'm still rather new to .NET so I hope you'll bear with me as I try and explain
    my question.

    I am writing an ASP.NET application using VB.NET. I am accessing a web method
    from a webservice that returns a structure. On the client side I have added the
    webservice as a reference and created a class that contains the structure as a
    member as well as some other variables. The structure looks something like:

    Public Structure ValuationResult
    Dim Reliability As Integer
    Dim ShowClientID As String
    Dim ShowDepartment As String
    Dim ShowSource As String
    Dim Valuation As ValuationStruct ure 'another structure
    End Structure

    And the class I'm creating looks like:

    Public Class Report
    Private _ID As Integer
    Private _ValuationResul t As myWebservice.Va luationResult

    Public Property ID as integer
    Get
    Return _ID
    End Get
    Set(ByVal Value As Integer)
    _ID = Value
    End Set
    End Property

    Public Property ValuationResult as myWebservice.Va luationResult
    Get
    Return _ValuationResul t
    End Get
    Set(ByVal Value as myWebservice.Va luationResult)
    _ValuationResul t = Value
    End Set
    End Property

    ...

    End Class

    I am now trying to create a constructor that will allow the user to pass the
    populated structure that I get back from the webservice and populate the
    appropriate fields. After some trial and error I've come up with:


    Public Sub New(ByVal Result As myWebservice.Va luationResult)

    ValuationResult = New myWebservice.Va luationResult

    Me.ValuationRes ult.Reliability = Result.Reliabil ity
    Me.ValuationRes ult.ShowClientI D = Result.ShowClie ntID
    Me.ValuationRes ult.ShowDepartm ent = Result.ShowDepa rtment
    Me.ValuationRes ult.ShowSource = Result.ShowSour ce

    ValuationResult .Valuation = New myWebservice.Va luation

    Me.ValuationRes ult.Valuation.F SA = Result.Valuatio n.FSA
    Me.ValuationRes ult.Valuation.P ID = Result.Valuatio n.PID

    ...

    End Sub

    Now this seems to work when I call the constructor but I am unclear exactly as
    to why I need to call New for the structures and any structures contained within
    the parent struct. Afterall, they are strucutures and not classes. Is it
    because the struct is part of the class and no memory has yet been allocated for
    it until I call New?

    Also, is this the best way of doing this? Is there another more elegant way?

    Thanks for your help.

    Ren




  • Armin Zingler

    #2
    Re: Using a Structure in a Class

    "Ren" <nospam@nospam. org> schrieb[color=blue]
    > Hi all,
    >
    > I'm still rather new to .NET so I hope you'll bear with me as I try
    > and explain my question.
    >
    > I am writing an ASP.NET application using VB.NET. I am accessing a
    > web method from a webservice that returns a structure. On the
    > client side I have added the webservice as a reference and created
    > a class that contains the structure as a member as well as some
    > other variables. The structure looks something like:
    >
    > Public Structure ValuationResult
    > Dim Reliability As Integer
    > Dim ShowClientID As String
    > Dim ShowDepartment As String
    > Dim ShowSource As String
    > Dim Valuation As ValuationStruct ure 'another structure
    > End Structure
    >
    > And the class I'm creating looks like:
    >
    > Public Class Report
    > Private _ID As Integer
    > Private _ValuationResul t As myWebservice.Va luationResult
    >
    > Public Property ID as integer
    > Get
    > Return _ID
    > End Get
    > Set(ByVal Value As Integer)
    > _ID = Value
    > End Set
    > End Property
    >
    > Public Property ValuationResult as myWebservice.Va luationResult
    > Get
    > Return _ValuationResul t
    > End Get
    > Set(ByVal Value as myWebservice.Va luationResult)
    > _ValuationResul t = Value
    > End Set
    > End Property
    >
    > ...
    >
    > End Class
    >
    > I am now trying to create a constructor that will allow the user to
    > pass the populated structure that I get back from the webservice and
    > populate the appropriate fields. After some trial and error I've
    > come up with:
    >
    >
    > Public Sub New(ByVal Result As myWebservice.Va luationResult)
    >
    > ValuationResult = New myWebservice.Va luationResult
    >
    > Me.ValuationRes ult.Reliability = Result.Reliabil ity
    > Me.ValuationRes ult.ShowClientI D = Result.ShowClie ntID
    > Me.ValuationRes ult.ShowDepartm ent = Result.ShowDepa rtment
    > Me.ValuationRes ult.ShowSource = Result.ShowSour ce
    >
    > ValuationResult .Valuation = New myWebservice.Va luation
    >
    > Me.ValuationRes ult.Valuation.F SA = Result.Valuatio n.FSA
    > Me.ValuationRes ult.Valuation.P ID = Result.Valuatio n.PID
    >
    > ...
    >
    > End Sub
    >
    > Now this seems to work when I call the constructor but I am unclear
    > exactly as to why I need to call New for the structures and any
    > structures contained within the parent struct. Afterall, they are
    > strucutures and not classes. Is it because the struct is part of
    > the class and no memory has yet been allocated for it until I call
    > New?
    >
    > Also, is this the best way of doing this? Is there another more
    > elegant way?[/color]


    Maybe I didn't get the point, but why don't you simply write:

    Public Sub New(ByVal Result As myWebservice.Va luationResult)

    _ValuationResul t = Result

    End Sub



    Armin

    Comment

    • Ren

      #3
      Re: Using a Structure in a Class

      Armin,

      Thanks for your reply. I realized just as I hit "send" on my post that I could
      do as you suggested. What in the world was I thinking?

      On Wed, 15 Feb 2006 17:17:02 +0100, "Armin Zingler" <az.nospam@free net.de>
      wrote:
      [color=blue]
      >"Ren" <nospam@nospam. org> schrieb[color=green]
      >> Hi all,
      >>
      >> I'm still rather new to .NET so I hope you'll bear with me as I try
      >> and explain my question.
      >>
      >> I am writing an ASP.NET application using VB.NET. I am accessing a
      >> web method from a webservice that returns a structure. On the
      >> client side I have added the webservice as a reference and created
      >> a class that contains the structure as a member as well as some
      >> other variables. The structure looks something like:
      >>
      >> <<snip>>
      >> Also, is this the best way of doing this? Is there another more
      >> elegant way?[/color]
      >
      >
      >Maybe I didn't get the point, but why don't you simply write:
      >
      >Public Sub New(ByVal Result As myWebservice.Va luationResult)
      >
      > _ValuationResul t = Result
      >
      >End Sub
      >
      >
      >
      >Armin[/color]

      Comment

      • Armin Zingler

        #4
        Re: Using a Structure in a Class

        "Ren" <nospam@nospam. org> schrieb[color=blue]
        > Armin,
        >
        > Thanks for your reply. I realized just as I hit "send" on my post
        > that I could do as you suggested. What in the world was I
        > thinking?[/color]


        Too many trees to see the wood. Happens. :-)

        Armin

        Comment

        Working...