473,770 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(vert ex)
' 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 2652
| 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...henc e, no
compile complaints.

make sense?
Dec 1 '05 #2
"Sam" <qd*@datawave.c om> schrieb im Newsbeitrag
news:OJ******** ******@tk2msftn gp13.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...henc e,
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*******@free net.de> wrote in message
news:uN******** ******@TK2MSFTN GP14.phx.gbl...
"Sam" <qd*@datawave.c om> schrieb im Newsbeitrag
news:OJ******** ******@tk2msftn gp13.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*******@free net.de> wrote in message
news:uN******** ******@TK2MSFTN GP14.phx.gbl...
"Sam" <qd*@datawave.c om> schrieb im Newsbeitrag
news:OJ******** ******@tk2msftn gp13.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
41074
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 selections. Unfortunately, the property returns -1. Clicking again returns the proper result. Yet when I do the same thing on the same listbox after changing it to allow only one entry to be selected, it always returns the proper result on the first...
16
25419
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 the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
5
5537
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 type "array". Below is a simple example of the problem I'm having with a sample object "Article", which has an array property "authors". As you can see, I can't even use standard php array syntax to set a single author, even though the same...
3
1782
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 each instance (it returns an instance of a delegate that points to the same method no matter what the instance). I thought the best way to make sure that Class1 could be used by Class2 would be to create an interface that defined this property,...
0
1596
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 (Needle) and it seems to work except that the designer doesn't handle it correctly In the property page of the designer I see the property and I can
3
2700
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 return an array as a property because it will return a copy of the array instead a reference to it. How can I force the property to return a reference to the array? Is it only a feature of arrays? I hope normal class objects (including collections)...
6
18958
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 also by annotating the set method of the property with user-defined attribute, i'd like to activate something whenever the set method is invoked. I wrote the code as the followings. But, my problem is that it allows to set a value on an element of...
8
2221
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 general. The problem was with changing item values in a DataRow. The dotNET DataRow class has a ItemValues property which returns an object array containing item values. I tried to change some of these values by passing this object array to a method...
2
1678
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 dim myPlaceBets() As AU.exchange.PlaceBets many statements
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.