Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a reference to a struct in C#

in my application I have a LineShape control and a custom control (essentially a PictureBox with Label).

I want the LineShape to change one of its points coordinates, according to location of the custom control.

I had an idea to set a reference to a LineShape point inside the custom control and add location change event handler that changes referenced point coordinates.

However built in Point is a struct which is a value type, so that won't work. Does anyone have idea, how to make a reference to a structure or maybe someone knows a workaround for my problem?

I tried the solution regarding usage of the nullable type but it still doesn't work. Here's the way I define the field in my custom control (DeviceControl):

private Point? mConnectionPoint;

And implementation of the location change event handler:

private void DeviceControl_LocationChanged(object sender, EventArgs e)
{
    if (mConnectionPoint != null)
    {
        DeviceControl control = (DeviceControl)sender;

        Point centerPoint= new Point();
        centerPoint.X = control.Location.X + control.Width / 2;
        centerPoint.Y = control.Location.Y + control.Height / 2;

        mConnectionPoint = centerPoint;
    }
}
like image 567
Maksymilian Chwałek Avatar asked Sep 27 '11 09:09

Maksymilian Chwałek


1 Answers

You can pass value types by reference by adding 'ref' before it when passing in a method.

like this:

void method(ref MyStruct param)
{
}
like image 72
hcb Avatar answered Oct 16 '22 13:10

hcb