473,378 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Property Return Array

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









Dec 1 '05 #1
5 2634
| 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?
Dec 1 '05 #2
"Sam" <qd*@datawave.com> schrieb im Newsbeitrag
news:OJ**************@tk2msftngp13.phx.gbl...
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

Dec 1 '05 #3
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
"steve" <a@bc.com> wrote in message news:Nf*****************@fe04.lga...
| 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?

Dec 1 '05 #4
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

"Armin Zingler" <az*******@freenet.de> wrote in message
news:uN**************@TK2MSFTNGP14.phx.gbl...
"Sam" <qd*@datawave.com> schrieb im Newsbeitrag
news:OJ**************@tk2msftngp13.phx.gbl...
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

Dec 1 '05 #5
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:
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*******@freenet.de> wrote in message
news:uN**************@TK2MSFTNGP14.phx.gbl...
"Sam" <qd*@datawave.com> schrieb im Newsbeitrag
news:OJ**************@tk2msftngp13.phx.gbl...
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


Dec 2 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Russell Campbell | last post by:
Posting again, since my first attempt never appeared: In the onClick method of a listbox, I am attempting to retrieve the selectedIndex property. The listbox is set up to allow multiple...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
5
by: Jon Maz | last post by:
Hi All, I'm reasonably proficient with C#, and am starting with php5. I was happy to learn of the __get and __set methods, but am encountering a problem using them to set an object property of...
3
by: Erik Harris | last post by:
I apologize if this is a stupid question - I'm relatively new to OOP. I have a property that must exist in a class in order to be used by another class. The property, however, does not change with...
0
by: Vincent Finn | last post by:
Hi, I have a user control and I want an array of items to be available to the user In my case these items are a class called Needle. I added a property called Needles to get\set the array...
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
6
by: pinetaj | last post by:
Hello, I have a question of using 'property' on accessing elements of array. There is an array member in a class. I'd like to restrict accessing the elements of the array through property. And...
8
by: Papa.Coen | last post by:
Contrary to what the title might make you believe; this is not a n00b question. I recently came across a problem with using a property which made me question the way I use/see properties in...
2
by: =?Utf-8?B?Z2FkeWE=?= | last post by:
I use one of 2 arrays dependent on the country. Rather than say: if exchangeID = 1 then dim myPlaceBets() as As UK.exchange.PlaceBets many statements myPlaceBetsReq.bets = myPlaceBets else...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.