Question Using Class as datatype in property

mpv55

New member
Joined
Dec 17, 2010
Messages
1
Programming Experience
5-10
Please look the following code. I am getting error in the line
MySrs(1).A=10

It is probably that I havenot created an instance of class, MyPoint.

please help me to solve it.

Thanks

Mahendra




Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MySrs As New MySeries(20)


MySrs(1).A = 10


End Sub
End Class
Public Class MySeries
Private mPoint() As MyPoint
Public Sub New(ByVal Size As Integer)
ReDim mPoint(Size)
End Sub
Default Property Mpnt(ByVal index As Integer) As MyPoint
Get
Return mPoint(index)
End Get
Set(ByVal value As MyPoint)
mPoint(index) = value
End Set
End Property

End Class

Public Class MyPoint
Dim mA As Double
Property A As Double
Get
Return mA
End Get
Set(ByVal value As Double)
mA = value
End Set
End Property
Dim mB As Double
Property B As Double
Get
Return mB
End Get
Set(ByVal value As Double)
mB = value
End Set
End Property
Dim mC As Double
Property C As Double
Get
Return mC
End Get
Set(ByVal value As Double)
mC = value
End Set
End Property
End Class
 
1. Please use CODE tags - it makes code easier to read :)

2. Does MySrs (of type MySeries) have a property A?
 
InertiaM said:
2. Does MySrs (of type MySeries) have a property A?
No, but the array element the default property points to does. Yes, use code tags for readability.
mpv55 said:
It is probably that I havenot created an instance of class, MyPoint.
You are absolutely correct, the "error" you're getting is a NullReferenceException ("Object reference not set to an instance of an object."). You seem to be familiar with the New keyword to create an object instance. Your MySeries constructor resizes the mPoint array, but does not create and assign any object instances to the array elements. You have to do that, for example:
VB.NET:
MySrs(1) = New MyPoint
 
Back
Top