Simple Class Problem - Array as property.

K

Keith Rebello

How do I pass an array of values as a property of a class?
i.e. If Class1 has a property called XVals how do I assign an array of
x-values (X()) to the XVals property of Class 1? I need an example of the
property set procedure, please.
 
K

Klaus Löffelmann

Keith,

something like this?

Public Class Something

Dim mySingle As Single() = {123, 234, 345}

Public Property XYals() As Single()
Get
Return mySingle
End Get
Set(ByVal Value As Single())
mySingle = Value
End Set
End Property

End Class

Klaus
 
J

Jay B. Harlow [MVP - Outlook]

Keith,
Have you tried something like:

Public Class Class1

Private m_xvals() As Integer

Public Property XVals() As Integer()
Get
Return m_xvals
End Get
Set(ByVal value As Integer())
m_xvals = value
End Set
End Property

Public Shared Sub Main()
Dim c1 As New Class1
Dim x() As Integer = {0, 1, 2, 3, 4, 5}

c1.XVals = x

c1.XVals = New Integer() {3, 4, 5}

End Sub

End Class

In the above I am replacing the existing array each time, alternatively
could use Array.CopyTo to keep the same m_xvals array, but replace its
values. (to avoid aliasing problems)

Set(ByVal value As Integer())
value.CopyTo(m_xvals, 0)
End Set

Of course with CopyTo, you need to be certain that m_xvals is the correct
size.

Hope this helps
Jay
 

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