0

I want to change the value of pictureBox1.Location. And the result really confuses me!

Point player;    
player = pictureBox1.Location;    
player.X += 10; //it works    
pictureBox1.Location.X += 10;//it doesn't work!! Why??    

so I try this one :

pictureBox1.Location = player // it works      

Could anyone tell me why? I only learnt c# for 1 week with head first c#, and I cannot find the answer through the Internet or the book.

Sorry, I didn't make my question clarified. I cannot build

pictureBox1.Location.X += 10    .

There is an error:

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

I want to know the difference between player.X and pictureBox1.Location.X

Bill P
  • 3,622
  • 10
  • 20
  • 32
Sunjue Li
  • 27
  • 4
  • 2
    [Why are mutable structs evil?](http://stackoverflow.com/questions/441309/why-are-mutable-structs-evil) and [Immutability of structs](http://stackoverflow.com/questions/608542/immutability-of-structs) – Selman Genç Jan 31 '15 at 13:52
  • @Selman22, it's true that you shouldn't create mutable structs, but in this case the OP didn't create it, and he has to work with it... – Thomas Levesque Jan 31 '15 at 13:54
  • 2
    I didn't mean to answer his question. but those questions would help him to understand why this is happening. – Selman Genç Jan 31 '15 at 13:57
  • 1
    You can to set it to anew Point `pictureBox1.Location=new Point(x+10 ,1)` – The One Jan 31 '15 at 14:04
  • Thank you @JAT, It works and very simple. – Sunjue Li Jan 31 '15 at 14:34

2 Answers2

1

Location is of type Point, which is a value type (struct). So when you access pictureBox1.Location, it returns a copy of the location. Changing X on this copy will have no effect on pictureBox1.Location, so it's probably not what you want; the compiler detects it and issues an error.

You must think of Point as a value, not an object that contains values. The fact that the X and Y property is unfortunate; writing mutable structs is a pretty bad idea, but Point dates back to the first version of .NET, and I guess MS had not yet realized how bad it could be...

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Thank you! But why "player.X += 10" works? Sorry, I didn't make my question clarified. Rather than getting a wrong result, I get an error with "pictureBox1.Location.X += 10" and cannot build it.. – Sunjue Li Jan 31 '15 at 14:22
  • @SunjueLi, because `player` is a variable, so you can access it directly; it doesn't make a copy, unlike accessing the `Location` property. The error you're getting on `pictureBox1.Location.X += 10` is because the compiler knows that it wouldn't do what you expect. – Thomas Levesque Jan 31 '15 at 14:40
  • Thank you! I totally understand it! Sorry, I don't have enough reputation to vote up (I used to think it's like quora).. – Sunjue Li Jan 31 '15 at 15:13
0

Messagebox also has, Left (R/W) Right (R) Bottom (R) Top (R/W)

R = Read only R/W = Read write

You can get the most right side of textbox coordinate x by typing

Int right_x = messagebox.Right;

Write

Messagebox.Left += 10;

and it works perfectly.

EDIT:

Source code of Messagebox.Location struct atleast has something like this.

Public struct Location { public int x, y; }

If you try this

messagebox.Location.x += 10;

You are trying to modify useless variable, and it's pointless. Now this is possible

Messagebox.Location = new Point(messagebox.Location.x + 10, messagebox.Location.y);

But the method I provided earlier is faster, since it doesn't care about y axis.

Yytsi
  • 424
  • 1
  • 5
  • 14
  • You could have edited your other answer. This just repeats it and adds a little more to it. In the future, click on the edit link under your post. Feel free to delete your other answer. – LarsTech Jan 31 '15 at 20:52
  • @LarsTech I actually did edit, somehow it created a new answer. I tried to remove it, but stack exchange won't allow it, not sure why. – Yytsi Jan 31 '15 at 20:54
  • @LarsTech If stack exchange allows you to delete the copy of my answer, please do so :) – Yytsi Jan 31 '15 at 20:56
  • I'm not a moderator, so I can't delete your answer. But there should be a link under your post to delete. I'm not aware of any rules against you deleting your own answer, so it should work. – LarsTech Jan 31 '15 at 20:57
  • @LarsTech I see. It's probably because they didn't write function to delete it in Android version, it's my guess. If google chrome has that feature, I will delete it, thanks in advance. – Yytsi Jan 31 '15 at 20:59