Property Return Array

S

Sam

Hi All

I have couple of question regarding property of a class and structures.

**** ---- Here is my class and structure ---- *****

1. Public Structure MyPoint
2. Dim p As Point
3. Dim ptColor As Color
4. End Structure



5. Public Class MyTriangle

6. Private _l(2) As MyPoint

7. Public Sub New(ByVal points() As MyPoint)
8. _l = points
9. End Sub

10. Default Public Property Item(ByVal index As Integer) As MyPoint

11. Get
12. Return (_l(index))
13. End Get
14. Set(ByVal value As MyPoint)
15. _l(index) = value
16. End Set

17. End Property

18. Public Property Points() As MyPoint()
19. Get
20. Return (_l)
21. End Get
22. Set(ByVal value As MyPoint())
23. _l = value
24. End Set
25. End Property
26. End Class



My questions are:

a. Is it normal to have a property that return an array as in my class line
18?
b. When I tried to assign a value such as Triangle(0).p.X = 5, the compiler
complained

Would anyone give me a hand?

Regards,

Sam

**** ---------------------------- *****
Here is what I tried

Dim v0, v1, v2 As MyPoint
Dim vertex(2) As MyPoint

vertex(0) = v0
vertex(1) = v1
vertex(2) = v2

Dim Triangle As New MyTriangle(vertex)


' The following statement give me an error
' "Epression is a value and therefore cannot be
' the target of an assignment
Triangle(0).p.X = 5


' However if I try this statement which access my
' property that returns an array, I'm ok

Triangle.Points(0).p.X = 5
 
A

Armin Zingler

Sam said:
Hi All

I have couple of question regarding property of a class and structures.

**** ---- Here is my class and structure ---- *****

1. Public Structure MyPoint
2. Dim p As Point
3. Dim ptColor As Color
4. End Structure



5. Public Class MyTriangle

6. Private _l(2) As MyPoint

7. Public Sub New(ByVal points() As MyPoint)
8. _l = points
9. End Sub

10. Default Public Property Item(ByVal index As Integer) As MyPoint

11. Get
12. Return (_l(index))
13. End Get
14. Set(ByVal value As MyPoint)
15. _l(index) = value
16. End Set

17. End Property

18. Public Property Points() As MyPoint()
19. Get
20. Return (_l)
21. End Get
22. Set(ByVal value As MyPoint())
23. _l = value
24. End Set
25. End Property
26. End Class



My questions are:

a. Is it normal to have a property that return an array as in my class
line 18?

If you can access the array by the Item property, it's not common practice
to return the whole array. I'd say, either or.
b. When I tried to assign a value such as Triangle(0).p.X = 5, the
compiler complained

A structure is a value type (unlike a reference type like a class).
Therefore, the Item property always returns a copy of the item in the array.
Changing the value of the copy wouldn't make sense because the original
value in the array in the class doesn't change although the code looks like
it would. Thus the compiler complains. Instead you'd have to write:

dim p as mypoint

p = triangle(0)
p.p.x = 5
triangle(0) = p


Armin
 
S

steve

| My questions are:

| a. Is it normal to have a property that return an array as in my class
line
| 18?

sure...why not? although i usually create an strong typed collection
class...you'll come to appreciate not having to check for "if Points is
nothing" every time you want to do something with the Points property. also
makes it easier to add, find, and remove points within the collection.
either way is fine though.


| b. When I tried to assign a value such as Triangle(0).p.X = 5, the
compiler
| complained

for this to work, you'd need to set the Points property as the default
property...e.x.

Public Default Property Points() As MyPoint()


this works:

Triangle.Points(0).p.X = 5

because you have been explicit in accessing the Points property...hence, no
compile complaints.

make sense?
 
S

Sam

Hi Steve

Thanks for the response. I think the point property can work without the
default keyword. In addtion, the using default property also requires a
parameter which I already implemented in my default Item property. The Item
property is the one I'm having some problem with not the Points property

Regards,

Sam
 
S

Sam

Armin,

What you said in my second question kinda makes sense but I don't quite get
how having access in indivisual array from my Points property (which return
an array)differs than the my Item. Both of my property access the same array
_l(2) which contains point structure. I'm just wondering if this has
anything to do with reference passing of Array type?

Sam
 
G

Guest

You can add another property (might want check validity of idx when
reading/setting;

Public Property SetPoint(idx as integer) As MyPoint
Get
Return (_l(idx))
End Get
Set(ByVal value As MyPoint)
.. _l(idx) = value
End Set
End Property
 

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