Using a Structure in a Class

R

Ren

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 ValuationStructure 'another structure
End Structure

And the class I'm creating looks like:

Public Class Report
Private _ID As Integer
Private _ValuationResult As myWebservice.ValuationResult

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.ValuationResult
Get
Return _ValuationResult
End Get
Set(ByVal Value as myWebservice.ValuationResult)
_ValuationResult = 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.ValuationResult)

ValuationResult = New myWebservice.ValuationResult

Me.ValuationResult.Reliability = Result.Reliability
Me.ValuationResult.ShowClientID = Result.ShowClientID
Me.ValuationResult.ShowDepartment = Result.ShowDepartment
Me.ValuationResult.ShowSource = Result.ShowSource

ValuationResult.Valuation = New myWebservice.Valuation

Me.ValuationResult.Valuation.FSA = Result.Valuation.FSA
Me.ValuationResult.Valuation.PID = Result.Valuation.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
 
A

Armin Zingler

Ren said:
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 ValuationStructure 'another structure
End Structure

And the class I'm creating looks like:

Public Class Report
Private _ID As Integer
Private _ValuationResult As myWebservice.ValuationResult

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.ValuationResult
Get
Return _ValuationResult
End Get
Set(ByVal Value as myWebservice.ValuationResult)
_ValuationResult = 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.ValuationResult)

ValuationResult = New myWebservice.ValuationResult

Me.ValuationResult.Reliability = Result.Reliability
Me.ValuationResult.ShowClientID = Result.ShowClientID
Me.ValuationResult.ShowDepartment = Result.ShowDepartment
Me.ValuationResult.ShowSource = Result.ShowSource

ValuationResult.Valuation = New myWebservice.Valuation

Me.ValuationResult.Valuation.FSA = Result.Valuation.FSA
Me.ValuationResult.Valuation.PID = Result.Valuation.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?


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

Public Sub New(ByVal Result As myWebservice.ValuationResult)

_ValuationResult = Result

End Sub



Armin
 
A

Armin Zingler

Ren said:
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?


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

Armin
 
R

Ren

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top