user34537
user34537

Reputation:

How do i fix this code? C# Ptr to a struct

As you may know, the wall rect will not update since its a copy and not a reference. Is there a way for me to make a reference or pointer to r and not change this code? I suppose i could do a find/replace in the function but that something i try not to do.

//code to get wall
var r = wall.r;
//more code
r.Height += yDif;

Upvotes: 1

Views: 235

Answers (3)

Laurent Etiemble
Laurent Etiemble

Reputation: 27929

Why don't you update directly the wall rect ?

//code to get wall
var r = wall.r;
//more code
wall.r.Height += yDif;

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942498

It isn't going to work, you already know why. Avoid the copy or simply store the copy back:

//more code
r.Height += yDif;
wall.r = r;

Upvotes: 2

ChaosPandion
ChaosPandion

Reputation: 78292

This of course requires a setter.

//code to get wall
var r = wall.r;
//more code
r.Height += yDif;
wall.r = r;

Upvotes: 1

Related Questions