0

I am trying to write a network visualization software based on an existing framework (GraphX). For a grouping algorithm I have had to draw my nodes based on a manually assigned location (assigned to the data object they are based on), which causes dragging to be disabled. However, there is still an invisible control which is dragged so I would like to use the initial offset in drawing the nodes so that they respond to dragging of the invisible nodes.

To do so I need to get a static variable at the time of drawing the nodes. I have been trying for a while now to get it but I can only seem to get pointers. I tried my hand shallow copies and unsafe code but no luck..

public Point GetStartPosition(bool final = true, bool round = false)
{
    DataVertexControlWFA copy = (DataVertexControlWFA)this.MemberwiseClone();
    return copy.GetPosition();
}

is a method I hoped would be the current position of the object (unalterable)

and

algPosition = GetPosition();

double x = algPosition.X;
double y = algPosition.Y;

double* StartX;
double* StartY;

StartX = &x;
StartY = &y;

double offset_SourceX = _vertex.Point.X - *StartX;
double offset_SourceY = _vertex.Point.Y - *StartY;

Point position = new Point(algPosition.X + offset_SourceX, algPosition.Y + offset_SourceY);

Is a block of code I hoped would result in an unchangeable value of StartX and StartY but no luck.

  • **I think** you [want a singleton](https://stackoverflow.com/questions/2155688/what-is-a-singleton-in-c)? – Liam Jun 06 '18 at 09:22
  • 1
    It's not clear to me from the description what your real issue is, but I think you are over-engineering it. For example, since `Point` is a value type, your `GetStartPosition` could be just `return this.GetPosition();`. – 500 - Internal Server Error Jun 06 '18 at 09:29
  • Every control has three properties which uniquely defines each object:1) Top 2) Left 3) Width 4) Height. So you need to restore the TOP and LEFT to put back to original position. – jdweng Jun 06 '18 at 09:30
  • If you want a unchangeable value, use properties which only got a getter and no setter – Chrᴉz remembers Monica Jun 06 '18 at 09:41
  • @-500 this would get me a changeable value right? @ Chriz How would I give it the first value then? – Bastiaan de Graaf Jun 06 '18 at 09:46
  • 2
    It would be helpful if you could provide a [mcve]. At the moment we just have snippets, and I'm finding the description hard to understand. Additionally, we don't know which `Point` type this is - there are a lot around, some of which are classes and some of which are structs, which makes a huge difference. – Jon Skeet Jun 06 '18 at 09:49
  • 1
    `Point` is a value type, not a reference, so `GetStartPosition` will always return a copy of what the position is right now. Assigning a new value to what you get from that does not change anything except for that local copy. To actually change the position of something you have to assign to a writable property, or call a method. – 500 - Internal Server Error Jun 06 '18 at 09:53
  • I'd also suggest you read [What is the difference between a reference type and value type in c#?](https://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c) But as others have stated, I'm really not sue what your asking TBH – Liam Jun 06 '18 at 10:04

1 Answers1

0

You can do this:

public class Program
{
    // These are C# get-only properties
    // They cannot be assigned a value
    double StartX => algPosition.X;    // Body is called each time the member is read
    double StartY => algPosition.Y;    // Which gives the most up-to-date values 
}

...

algPosition = GetPosition();

double offset_SourceX = _vertex.Point.X - StartX;
double offset_SourceY = _vertex.Point.Y - StartY;

Point position = new Point(algPosition.X + offset_SourceX, algPosition.Y + offset_SourceY);

Properties are syntactic sugar for methods. GetX() => 4; can become X => 4; and they equal performance wise. Sometimes, it makes more sense to use a method that looks like a field, which is what a property is.

GetX() sounds like it might have some weird side-effect to it. X sounds like it won't, and is simply calculating a simple value.

In this example, a compile warning will be generated if anyone tries to do StartX = foo because there is no way to set it. Not all properties are like that. You can read more about them here.

Note this requires no pointers and is true for value and reference types.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
  • Thanks, this was helpful. however, I am afraid the controls are redrawn when they are dragged so my entire premise is wrong as a new controll appears to be generated, at which point a new value will be assigned to StartX and StartY. I might assign it to my data object in this fashion though! – Bastiaan de Graaf Jun 06 '18 at 10:27
  • Welcome. If it helped, and you found it useful, click the up arrow to upvote it, and if it answered the question you were asking, hit the checkmark underneath the downarrow to accept it as the answer xP – AustinWBryan Jun 06 '18 at 10:36