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

A question on properties

Dom
I have a class that has a System.Drawing.Rectangle as a member, called
m_Rect. I set it up as a property, like this:

get { return m_Rect;}
set { m_Rect = value;}

Now in my main code I want to do the following:

myObject.Rect.Width = width;
myObject.Rect.Height = height;

I can see why I can't -- I guess myObect.Rect.Width is calling up the
"get" and not the "set". But is there some way to do what I want?

Dom

Aug 14 '07 #1
6 1088
On Aug 14, 4:12 pm, Dom <dolivas...@gmail.comwrote:
I have a class that has a System.Drawing.Rectangle as a member, called
m_Rect. I set it up as a property, like this:

get { return m_Rect;}
set { m_Rect = value;}

Now in my main code I want to do the following:

myObject.Rect.Width = width;
myObject.Rect.Height = height;

I can see why I can't -- I guess myObect.Rect.Width is calling up the
"get" and not the "set". But is there some way to do what I want?
Well, you could create your own properties which get/set the
appropriate properties in m_Rect.

Jon

Aug 14 '07 #2
Dom
On Aug 14, 11:28 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Aug 14, 4:12 pm, Dom <dolivas...@gmail.comwrote:
I have a class that has a System.Drawing.Rectangle as a member, called
m_Rect. I set it up as a property, like this:
get { return m_Rect;}
set { m_Rect = value;}
Now in my main code I want to do the following:
myObject.Rect.Width = width;
myObject.Rect.Height = height;
I can see why I can't -- I guess myObect.Rect.Width is calling up the
"get" and not the "set". But is there some way to do what I want?

Well, you could create your own properties which get/set the
appropriate properties in m_Rect.

Jon
I'm not sure I get this. Do you mean properties called "Rect_Width"
and "Rect_Height" which then changes the m_Rect member?

Aug 14 '07 #3
On Aug 14, 4:37 pm, Dom <dolivas...@gmail.comwrote:
Well, you could create your own properties which get/set the
appropriate properties in m_Rect.

I'm not sure I get this. Do you mean properties called "Rect_Width"
and "Rect_Height" which then changes the m_Rect member?
Yup. (I wouldn't include underscores myself, but it's entirely up to
you.)

Jon

Aug 14 '07 #4
Dom
On Aug 14, 11:43 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Aug 14, 4:37 pm, Dom <dolivas...@gmail.comwrote:
Well, you could create your own properties which get/set the
appropriate properties in m_Rect.
I'm not sure I get this. Do you mean properties called "Rect_Width"
and "Rect_Height" which then changes the m_Rect member?

Yup. (I wouldn't include underscores myself, but it's entirely up to
you.)

Jon
Well, this proves the point I made earlier. If gets and sets do
nothing but return a variable and set a variable, it's easier to just
make it public.

My $.02

Dom

Aug 14 '07 #5
Dom <do********@gmail.comwrote:
Yup. (I wouldn't include underscores myself, but it's entirely up to
you.)

Well, this proves the point I made earlier. If gets and sets do
nothing but return a variable and set a variable, it's easier to just
make it public.
Easier? Temporarily. Less maintainable in the long run, however.

There are lots of best practices which are costly in the short term,
but have great benefit in the long term. Not exposing public variables
is just one of them.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 14 '07 #6
Dom wrote:
Well, this proves the point I made earlier. If gets and sets do
nothing but return a variable and set a variable, it's easier to just
make it public.
Easier? Sure. More appropriate? Probably not, though that depends on
how concerned you are about the encapsulation of your implementation.

In the example you gave, the code that works is a little more complicated:

myObject.Rect = new Rectangle(width, myObject.Rect.Height);
myObject.Rect = new Rectangle(myObject.Rect.Width, height);

Or less literally, but more efficient:

myObject.Rect = new Rectangle(width, height);

But it can easily be done, and without exposing the class's implementation.

You are right that, assuming all your get and set methods do is return
and set a member variable, it's easier to just make the variable public.
But writing simple get and set methods aren't _that_ much harder, they
don't make the code that uses the property _that_ much harder, and they
do have design advantages over simply exposing the member variable as
public.

Note also that the Rectangle struct is a bit of an anomaly. That is, by
convention it is preferable that value types are immutable. Not all
are, and of course the Rectangle falls into this mutable category. But
more generally, since value types are often not mutable in the first
place, exposing the member variable as a public member wouldn't change
the basic behavior anyway; you'd still have to set the whole thing at
once, just as you do with properties.

So, while your observation holds true for the Rectangle struct, and a
handful of other struct types, for many other struct types it's not true
that there's any practical advantage of a public member over a property.

Pete
Aug 14 '07 #7

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

Similar topics

2
by: Rick Austin | last post by:
I recently had to perform a reinstalltion of Windows XP (my registry seems to have become corrupt). After this completed I had to reinstall all applications since most use the registry for settings,...
4
by: Lyn | last post by:
Hi, This question may seem a bit academic... To learn more about Access VBA, I have been enumerating the properties of various form controls. This was mostly successful and I have learned a lot...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
6
by: JerryP | last post by:
Hello, is there a way to launch the property dialogue for a directory from my c# app ? I would also like to launch the User Account Properties from Active Directory Users and Computers, and the...
3
by: Martin Montgomery | last post by:
I have, for example, a property called myProperty. I would like, when using a property grid to display the property name as "My Property". Is this possible. Is there an attribute etc Thank ...
7
by: Donald Grove | last post by:
Is it possible to retrieve field properties from a table in access2000 using code? I have tried: " dim dbs as dao.database dim tbl as dao.tabledef dim fld as dao.field dim prop as...
1
by: Christophe Peillet | last post by:
I have a CompositeControl with two types of properties: 1.) Mapped Properties that map directly to a child control's properties (ex.: this.TextboxText = m_txt.Text). These properties are handled...
7
by: Anderskj | last post by:
Hi! I am developing a c# application. I have a interface (which can change therefore my problem) If i do like this: List<PropertyInfoproperties = new List<PropertyInfo>();...
0
by: =?Utf-8?B?UmljayBHbG9z?= | last post by:
For some unknown reason (user error?), I cannot get a NameValueCollection to persist in the app.config file. Unlike other settings, I cannot get the String Collection Editor GUI to allow my to...
4
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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,...
0
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...

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.