By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,865 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,865 IT Pros & Developers. It's quick & easy.

textBox1.Size.Width

P: n/a

Why can't the following work when it has a set property defined?
this.textBox1.Size.Width = 25;

I get error

Cannot modify the return value of 'System.Windows.Forms.Control.Size'
because it is not a variable

but following works

Size s = new Size(textBox1.Size.Width + 5, textBox1.Size.Height);
this.textBox1.Size = s;

What is the sence in giving comment for properties get and set?
Documentation error maybe?

Dec 9 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> Documentation error maybe?
No, the documetation is correct. The property allows you to get and set
the size, that is, the Size structure as a whole.
If you are curious why you get such behavior, think about the fact that
Size is a value type object.

Dec 9 '05 #2

P: n/a
Well I got it now that Size is actually a struct which make sence for it to
throw that error. But still I disagree with the way Size.Width etc are
having comments. It gives a false idea what it is a property.
"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Documentation error maybe?

No, the documetation is correct. The property allows you to get and set
the size, that is, the Size structure as a whole.
If you are curious why you get such behavior, think about the fact that
Size is a value type object.

Dec 9 '05 #3

P: n/a
Sorry, but I don't think you do "get" it; the following works fine:

Size size = new Size();
size.Height = 100;
size.Width = 100;
new Form().Size = size;

So the setters and getters are perfectly valid. The problem is that you are
expecting Size to behave like a class...

Essentially, by typing

textbox1.Size.Width = 100;

You are essentially (in terms of thinking about it) doing the following:

Size tempSize = (Size) textbox1.Size.MemberwiseClone(); // protected
method - substitute with any method that clones this one! I don't know what
method the IL will actually use, but this suffices for the example...
tempSize.Width = 100;

As you can see, we have not altered the size of textbox1 at all, since
textbox1 has a completely different Size object that is unaffected.

The complier has neatly saved you from making this error. You could argue
that it should have updated the Size property with the new struct, but that
would make it very inefficient if you were using this method to update
multiple properties. In this scenario it's better to make you get a local
copy, update the properties on your local copy of the struct, and then
update the object's struct once and once only.

Does that help?

Marc
"Pohihihi" <no*****@hotmail.com> wrote in message
news:ul*************@tk2msftngp13.phx.gbl...
Well I got it now that Size is actually a struct which make sence for it
to throw that error. But still I disagree with the way Size.Width etc are
having comments. It gives a false idea what it is a property.
"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Documentation error maybe?

No, the documetation is correct. The property allows you to get and set
the size, that is, the Size structure as a whole.
If you are curious why you get such behavior, think about the fact that
Size is a value type object.


Dec 9 '05 #4

P: n/a
Sorry, but I don't think you do "get" it; the following works fine:
That might be right, I somethings feel like dumb myself.
Size size = new Size();
size.Height = 100;
size.Width = 100;
new Form().Size = size;

So the setters and getters are perfectly valid. The problem is that you
are expecting Size to behave like a class...
No, but because of the way commenting is done it seems like Width and Height
are properties of a class. I am talking about the comment/information you
see about something on intellisense.

Now size struct already have that comment thus any other object carrying
can't do much on that, that is what I meant by got it.

Essentially, by typing
textbox1.Size.Width = 100; ........
........ update the object's struct once and once only.
Does that help?


Yes, thanks.
Dec 10 '05 #5

P: n/a
The Size struct has no way of knowing (in any given scenario) whether it is
going to be held as a field/variable, ora accessed as a property, so there
is no way that it can "own" this aspects of the commenting. If you were
accessing the field directly (rather than using a property) this would not
be an issue. The Size's intellisense comments are 100% correct and 100%
appropriate.

However! If its that much of an issue for you, note that Control (a
base-class to TextBox) directly exposes the Width and Height properties,
which would do exactly what you want without the effort (but less
efficiently than setting both in one go via the Size).

Marc

"Pohihihi" <no*****@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Sorry, but I don't think you do "get" it; the following works fine:


That might be right, I somethings feel like dumb myself.
Size size = new Size();
size.Height = 100;
size.Width = 100;
new Form().Size = size;

So the setters and getters are perfectly valid. The problem is that you
are expecting Size to behave like a class...


No, but because of the way commenting is done it seems like Width and
Height are properties of a class. I am talking about the
comment/information you see about something on intellisense.

Now size struct already have that comment thus any other object carrying
can't do much on that, that is what I meant by got it.

Essentially, by typing
textbox1.Size.Width = 100;

.......
.......
update the object's struct once and once only.
Does that help?


Yes, thanks.

Dec 10 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.