VB.Net Returning a class from a function call

~J~

~J~

Soldato
Joined
20 Oct 2003
Posts
7,558
Location
London
Little problem...

I have a class as follows

Public Class MCEPartNo

Dim _HUMPartNo As String
Dim _HUMPartDescription As String


Property HUMPartNo() As String
Get
Return _HUMPartNo
End Get
Set(ByVal value As String)
_HUMPartNo = value
End Set
End Property

Property HUMPartDescription() As String
Get
Return _HUMPartDescription
End Get
Set(ByVal value As String)
_HUMPartDescription = value
End Set
End Property

End Class

What I'm basically trying to do is pass a string into a function which performs it's own calculations and then returns a populated class.

The function is, quite large, so I've narrowed it down to the basics which is...

Shared Function validateMCEPartNo(ByVal _MCEPartNo As String) As MCEPartNo

Dim a As New MCEPartNo
a.HUMPartNo = "PN"
a.HUMPartDescription = "PD"

End Function

So far so good. But I'm getting the infamous "Object reference not set to an instance of an object" error when I'm calling the function, and I'm calling it as...

Dim a As New MCEPartNo
a = validateMCEPartNo(Me.txtMCEPartNo.Text)

Me.txtHUMPartNo.Text = a.HUMPartNo
Me.txtHUMDescription.Text = a.HUMPartDescription

Me.chkHUMTicket.Focus()

Any ideas where I'm going wrong?
 
Your validateMCEPartNo method isn't actually returning the created object.

Code:
Shared Function validateMCEPartNo(ByVal _MCEPartNo As String) As MCEPartNo

Dim a As New MCEPartNo
a.HUMPartNo = "PN"
a.HUMPartDescription = "PD"

[b]validateMCEPartNo = a 'return object[/b]

End Function
 
Last edited:
NathanE said:
Your validateMCEPartNo method isn't actually returning the created object.

Yeah, I thought that, can't see how I can reference the created object that called the function call though.
 
Doh!

Stupid moment :D

Dim a As MCEPartNo
a = Mitsui.validateMCEPartNo(Me.txtMCEPartNo.Text)

Me.txtHUMPartNo.Text = a.HUMPartNo
Me.txtHUMDescription.Text = a.HUMPartDescription

Me.chkHUMTicket.Focus()


Shared Function validateMCEPartNo(ByVal _MCEPartNo As String) As MCEPartNo

Dim b As New MCEPartNo
b.HUMPartNo = "KK"
Return b


End Function

Works a treat!
 
Back
Top Bottom