473,385 Members | 2,210 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,385 software developers and data experts.

How to handle a property?

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 using the property as a parameter:

this.DoSomething(row.ItemArray);

But allas, ItemArray does not actually return the internal item value
list but a copy in the shape of an object array. I thought (and still
do) this was strange, I only use properties for controlled access to
my private member variables and avoid putting logic in get/set blocks
and when using a property I expected to access a private member
variable as well. What is even worse is that I had to use Reflector to
find out what ItemArray actually does, since it's not documented in
MSDN (for as far as I can find). This leads to tedious trial-and-error
(although I've managed with my view of Properties so far ;)

If the behaviour of ItemArray is something that might be expected then
using the following code is very dangerous:

this.DoSomething(myObject.HisProperty.HerProperty. TheirProperty);

Especially when a property value is(/might be) changed allong the way.
To be absolutely safe this should be rewritten to something like;

object a = myObject.HisProperty;
object b = a.HerProperty;
object c = b.TheirProperty;
this.DoSomething(c);
b.TheirProperty = c
a.HerProperty = b
myObject.HisProperty = a

Here:
http://msdn.microsoft.com/library/de...guidelines.asp
MS states that a method should be used if an array is returned.

See here for code examples:
http://www.navelpluis.nl/index.php?o...id=21&Itemid=5

So what defines a propertie and what can be expected from it?

Feb 2 '07 #1
8 2200
In article <11**********************@p10g2000cwp.googlegroups .com>,
pa*******@gmail.com says...
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 using the property as a parameter:

this.DoSomething(row.ItemArray);
According to the documentation on ItemArray:

"Gets or sets all the values for this row **through an array**"

The emphasis is mine. It sounds like to me you can either "get" all the
values into an array, or you can "set" all of the values from an array.
I don't see any individual "set a single item" semantics in this. For
that you'd need to use the Item property.

And my next question is why not just past the DataRow since that is what
you want updated. By passing just the array, you're asking DoSomething
to update the array -- not the DataRow.

My 2 cents... :)

--
Patrick Steele
http://weblogs.asp.net/psteele
Feb 2 '07 #2
According to the documentation on ItemArray:
>
"Gets or sets all the values for this row **through an array**"

The emphasis is mine. It sounds like to me you can either "get" all the
values into an array, or you can "set" all of the values from an array.
So 'through an array' means that it is generated, not a member var?
I don't see any individual "set a single item" semantics in this. For
that you'd need to use the Item property.
Documentation doesn't even(or always) state that when this ìs actually
possible. Like when a properties does return a member array. But I
don't want to make this thread a discussion about documentation and/or
formulation but about the definition of a property.
And my next question is why not just past the DataRow since that is what
you want updated. By passing just the array, you're asking DoSomething
to update the array -- not the DataRow.
This is not the point. If the misinterpretation happens here, I can
happen anywhere.
Next to that, it was my intention to only update its values and using
a property for a parameter in a method call isn't something strange or
something you should avoid. The classes I mention are only used to
illustrate my 'problem'.

Feb 2 '07 #3
In article <11**********************@a34g2000cwb.googlegroups .com>,
pa*******@gmail.com says...
According to the documentation on ItemArray:

"Gets or sets all the values for this row **through an array**"

The emphasis is mine. It sounds like to me you can either "get" all the
values into an array, or you can "set" all of the values from an array.

So 'through an array' means that it is generated, not a member var?
That's the way I'm interpreting it. And it sounds like from your
experience, that's the way it is working.
And my next question is why not just past the DataRow since that is what
you want updated. By passing just the array, you're asking DoSomething
to update the array -- not the DataRow.

This is not the point. If the misinterpretation happens here, I can
happen anywhere.
I was just curious as to why you were taking that approach. It doesn't
seem "natural" to me. But that's just me.
Next to that, it was my intention to only update its values and using
a property for a parameter in a method call isn't something strange or
something you should avoid. The classes I mention are only used to
illustrate my 'problem'.
I didn't mean to imply it was strange. It's not strange for a property
that exposes a reference type. It's definitely something to avoid with
a value type since you're only getting a copy.

--
Patrick Steele
http://weblogs.asp.net/psteele
Feb 2 '07 #4
On Feb 2, 6:39 am, "Papa.Coen" <papa.c...@gmail.comwrote:
But allas, ItemArray does not actually return the internal item value
list but a copy in the shape of an object array.
So what defines a propertie and what can be expected from it?
This is a good approach to return array from a class, by return a
shallow copy instead of a reference, and control update the content of
the array/collection variable via a specific method.

QL

Feb 2 '07 #5
This is a good approach to return array from a class, by return a
shallow copy instead of a reference, and control update the content of
the array/collection variable via a specific method.
I understand it's safer but using an array property will result in a
lot of overhead, since changing a single array element requires you to
copy an entire array twice! Does this concern all collection types
then or is it just the DataRow class and then again : how could I have
known? I still believe the documentation is not very clear on this
point. I want to figure out what can be expected of a property.

Feb 2 '07 #6
In article <11*********************@j27g2000cwj.googlegroups. com>,
pa*******@gmail.com says...
I want to figure out what can be expected of a property.
I think that is a documentation issue.

And I agree that "ItemArray" really shouldn't be a property -- they
should have a GetItemValues() and SetItemValues() methods. As you
pointed out earlier (either in this thread or via email), Microsoft's
documentation states that properties shouldn't return arrays. It's too
ambiguous as to what you're being given.

Even Microsoft makes mistakes in their designs. ;)

--
Patrick Steele
http://weblogs.asp.net/psteele
Feb 3 '07 #7

So let's conclude that using properties requires a bit more attention
than might be expected :)
Thank you for your insights.

If anyone else has something to say, I'm still curious to what other
people think about this subject...
Feb 5 '07 #8
Papa.Coen wrote:
This is a good approach to return array from a class, by return a
shallow copy instead of a reference, and control update the content of
the array/collection variable via a specific method.

I understand it's safer but using an array property will result in a
lot of overhead, since changing a single array element requires you to
copy an entire array twice!
This is the reason that properties which return arrays are not
recommended. For example, FxCop will complain about it, and the
Framework Design Guidelines advise against it.
Does this concern all collection types
then or is it just the DataRow class
The collection types use indexed properties, not properties of an array
type. These two things are different. If you consider a property P of
type T as a combination of 'T get_P()' and (optional) 'void set_P(T
value)', then indexed properties are like 'T get_P(U index)' and 'void
set_P(U index, T value)', where U is the type of the indexer. And
indexed properties may have an arbitrary number of indexers.
and then again : how could I have
known? I still believe the documentation is not very clear on this
point. I want to figure out what can be expected of a property.
It should probably have been written as a pair of Get and Set methods.
Reading properties should ideally be cheap and a read-only operation. If
the operation is expensive, then a method call is more appropriate to
flag that fact.

-- Barry

--
http://barrkel.blogspot.com/
Feb 5 '07 #9

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

Similar topics

6
by: R.Wieser | last post by:
Hello All, I'm trying to get a "Virtual Listbox" to work. I've currently got a form, and used CreateWindowExA to create a ListBox with the LBS_OWNERDRAWFIXED and LBS_NODATA flags on it. I've...
3
by: Fred Hebert | last post by:
I am trying to use a 3rd party DLL that requires the main window handle as a parameter. e.g. MyFunc(WHND MyHandle); The example looks something like this: Result = MyFunc(Handle); Where...
4
by: Brad Jones | last post by:
<Previously posted in microsoft.public.dotnet.framework.windowsforms> Hi all. Any suggestions here would be appreciated. Thanks for reading. I'm primarly a C++ developer but I've been trying to...
3
by: JJ | last post by:
Hi All, How can I get the handle to a window in c#? I did notice that for a form in C# there is a hwnd property, is this one and the same? Thanks, JJ
3
by: TC | last post by:
Hey Folks, I am using the following 4 Win32 APIs with a C# AddIn: FindWindow SetWindowLong SetForegroundWindow EnableWindow Within the AddIn, there are some winforms. I use these APIs to...
17
by: Fred Hebert | last post by:
I am trying to use a 3rd party DLL that requires the main window handle as a parameter. e.g. MyFunc(WHND MyHandle); The example looks something like this: Result = MyFunc(Handle); Where...
7
by: Ken Varn | last post by:
I am working in managed C++. I have a Mutex object in which I need to replace the Handle property with a new handle. The new handle is being constructed using Win32 CreateMutex call. I need to...
6
by: jk | last post by:
Looking through WebUIValidation.js, I discovered that the standard validators don't cater for non-numeric date formats (e.g. dd-MMM-yyyy) which I would like to do To keep code to a minimum, I...
2
by: Schorschi | last post by:
Can't seemd to get ReadFile API to work! Returns invalid handle error? =========================================================================== Ok, the visual basic gurus, help! The...
6
by: anonymous | last post by:
hi all, I'm trying to use the capCreateCaptureWindow (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_capcreatecapturewindow.asp) in a Windows...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.