Including a class as a member of another class

J

Jeff Cobelli

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.Enumerations
Public Class DefaultValues
Public Name As String
Public Lenders() As clsLender
End Class
End Namespace

Then the code looks like this:

<WebMethod()> Public Function GetDefaultValues( ByVal some value As Guid) As
DataTypesVB.Enumerations.DefaultValues
Dim i As Integer
Dim DefaultValues As New DataTypesVB.Enumerations.DefaultValues()
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).Name = 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>Server 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
 
P

pritiphadke

Try

Namespace DataTypesVB.Enumerations
Public Class DefaultValues
Public Name As String
Public Lenders() As New clsLender
End Class
End Namespace
 
R

Robby

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

This statement ...

Dim DefaultValues As New DataTypesVB.Enumerations.DefaultValues()

creates an empty array of DataTypesVB.Enumerations.DefaultValues. 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
 
J

Jeff Cobelli

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.Name. 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).Name = 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
 
R

Robby

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.Lenders 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.
With DefaultValues
ReDim Preserve .Lenders(i)
* .Lenders(i) = New clsLender()
.Lenders(i).ID = some value
.Lenders(i).Name = some value
End With

Robby
 
J

Jeff Cobelli

You are the best, thank you!

- Jeff

Robby said:
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.Lenders 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.

* .Lenders(i) = New clsLender()

Robby
 

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