Property Return Array | | |
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 | | | | re: Property Return Array
| 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? | | | | re: Property Return Array
"Sam" <qdo@datawave.com> schrieb im Newsbeitrag
news:OJpcvtq9FHA.2120@tk2msftngp13.phx.gbl...[color=blue]
> 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?[/color]
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.
[color=blue]
> b. When I tried to assign a value such as Triangle(0).p.X = 5, the
> compiler complained[/color]
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 | | | | re: Property Return Array
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
"steve" <a@bc.com> wrote in message news:NfKjf.14893$Mj.8691@fe04.lga...[color=blue]
>| 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?
>
>[/color] | | | | re: Property Return Array
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
"Armin Zingler" <az.nospam@freenet.de> wrote in message
news:uNZt$Xs9FHA.2176@TK2MSFTNGP14.phx.gbl...[color=blue]
> "Sam" <qdo@datawave.com> schrieb im Newsbeitrag
> news:OJpcvtq9FHA.2120@tk2msftngp13.phx.gbl...[color=green]
>> 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?[/color]
>
> 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.
>[color=green]
>> b. When I tried to assign a value such as Triangle(0).p.X = 5, the
>> compiler complained[/color]
>
> 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[/color] | | | | re: Property Return Array
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
--
Dennis in Houston
"Sam" wrote:
[color=blue]
> 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
>
>
>
> "Armin Zingler" <az.nospam@freenet.de> wrote in message
> news:uNZt$Xs9FHA.2176@TK2MSFTNGP14.phx.gbl...[color=green]
> > "Sam" <qdo@datawave.com> schrieb im Newsbeitrag
> > news:OJpcvtq9FHA.2120@tk2msftngp13.phx.gbl...[color=darkred]
> >> 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?[/color]
> >
> > 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.
> >[color=darkred]
> >> b. When I tried to assign a value such as Triangle(0).p.X = 5, the
> >> compiler complained[/color]
> >
> > 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[/color]
>
>
>[/color] |  | Similar Visual Basic .NET bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,533 network members.
|