Properties for Integer (or double) arrays?

E

Eric A. Johnson

I'm trying to figure out if there is any way to create a Property for a
private array member. I have tried it like this:

' Property for the length of a side
Public Property SideLength(ByVal sideNumber As Integer) As Integer

Get

Return mSideLength(sideNumber)

End Get

Set(ByVal sideNum As Integer, ByVal Value As Integer)

mSideLength(sideNum) = Value

End Set

End Property



....and the declaration is like so:



' Array containing the length of each side

Private mSideLength As Integer()

' ...miscellaneous code...

mSideLength = New Integer(mSides - 1) _

{side1, side2, side3}



.... where msides is equal to the number of sides for the shape (in this
case, 3, as it is a triangle). The errors I am getting in declaring the
property are as follows:



C:\Documents and Settings\Eric\Desktop\Eric\School\Advanced VB
2\Shapes\ShapeLibrary\Triangle.vb(82): 'Set' method cannot have more than
one parameter.
C:\Documents and Settings\Eric\Desktop\Eric\School\Advanced VB
2\Shapes\ShapeLibrary\Triangle.vb(83): Name 'Value' is not declared.


I can tell you why I want to use this as a property, if you want, but
mainly, I want to know how (and if!) it is possible to Get/Set an array
member as a property. How would I code it? The book I have doesn't tell me
if it is or isn't possible.

-- Eric
 
C

Chris

Eric said:
I'm trying to figure out if there is any way to create a Property for a
private array member. I have tried it like this:

' Property for the length of a side
Public Property SideLength(ByVal sideNumber As Integer) As Integer

Get

Return mSideLength(sideNumber)

End Get

Set(ByVal sideNum As Integer, ByVal Value As Integer)

mSideLength(sideNum) = Value

End Set

End Property



...and the declaration is like so:



' Array containing the length of each side

Private mSideLength As Integer()

' ...miscellaneous code...

mSideLength = New Integer(mSides - 1) _

{side1, side2, side3}



... where msides is equal to the number of sides for the shape (in this
case, 3, as it is a triangle). The errors I am getting in declaring the
property are as follows:



C:\Documents and Settings\Eric\Desktop\Eric\School\Advanced VB
2\Shapes\ShapeLibrary\Triangle.vb(82): 'Set' method cannot have more than
one parameter.
C:\Documents and Settings\Eric\Desktop\Eric\School\Advanced VB
2\Shapes\ShapeLibrary\Triangle.vb(83): Name 'Value' is not declared.


I can tell you why I want to use this as a property, if you want, but
mainly, I want to know how (and if!) it is possible to Get/Set an array
member as a property. How would I code it? The book I have doesn't tell me
if it is or isn't possible.

-- Eric

You were close...
' Property for the length of a side
Public Property SideLength(ByVal sideNumber As Integer) As Integer
Get
Return mSideLength(sideNumber)
End Get
Set(ByVal Value As Integer)
mSideLength(sideNum) = Value
End Set
End Property
 
E

Eric A. Johnson

Ummm... duh... I guess I didn't notice the fact that the side number was
already included as part of the property declaration. Thanks for you help!
:)
 

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