I am trying to use a GraphicPath for the first time. I am confused about instances and scope. I’ve added a Property to the window that is a GraphicPath, thinking it would have global scope. When I try to access it in a window method with the AddLineToPoint method it get a nil exception error as though it was not instantiated yet. Just before the method its value is Nil. If I declare it with a Var… As New it is not Nil prior to access, its value is GraphicPath.
This must just be a beginner’s error. Do I need to instantiate it somewhere as well as just adding the property?
Yes. Object properties are not automatically instantiated (unlike discrete values, like string and integer). You’ll need to do something like:
…before you access it for the first time.
Great, thanks. Does it matter where that New statement is? Could be in say the Open event for the window, or a method for the window? Both seem to work, just wondering.
The resulting object is identical, if that’s what you mean. It’s up to you where you put the code, depending on the overall needs of your program. Mainly you need to guarantee that it is created before any other code tries to access it.
i made a feature request for it but xojo not want the implementation.
(this feature was optional and backward compatible for others.)
usually all properties are in use so you would always create the object soner or later.
let it nil have no benefit, ok you save a few bytes for awhile…
The reason you can’t do this with objects is that some objects require parameters in their Constructor that the framework can’t supply. Picture is a good example: how could the system know what size and depth you need? You’d end up with an object you couldn’t use and which might have invalid properties.
Instantiating the GraphicPath cleared up the errors using it. However questions about scope and persistence remain. I expected it to work something like an array of points, that I could add to from different methods, and would persist at least at least until painted. It does not appear to do that. If I add 3 points in in-line code and then draw, I see lines. If I do that in a different method (other than the Paint event), again I see the lines. However if I add three points while iterating in a Do look, nothing. Stepping though the program, the current point as indicated by the debugger goes through very odd changes. This might be due to it not updating in the debugger correctly, not sure.
Can someone give me a short explanation of how a GraphicsPath is supposed to work, what is its scope and persistence?
What I am trying to do is draw a graph, points in the graph arrive via a serial port periodically. I though as they arrived, I could simply add them to a GraphicsPath object (there are several) and then draw them when needed. This does not appear to be what was intended, for example there is no way to clear it so it must get cleared implicitly by some action. The documentation is not enlightening.
You should probably start a new thread, using your most recent post, with a more relevant headline.
I was able to reverse engineer this a bit, I did not first set the first point with a GraphicsPath.MoveToPoint(x, y). Yes, I should not depend on uninitialized states. If you do not set the first point (even though it is reported as (0,0)) it will not draw and subsequent point.
Now, the only remaining problem is how do you reset the GraphicsPath property to an initial state (no points) when you want to start again? There is no RemoveAll method. Is there a way to remove the property instance and reinstantiate it fresh?
You create a brand new object with
myGraphicsPath = New GraphicsPath.
Ah, OK, that automatically deconstructs and cleans up the old instance? It appears to in tests.
Yes. As soon as you remove the last reference to an object, it is immediately destructed. Replacing the GrapicsPath with a new one removes the reference, so it gets destroyed.
Thanks! I think I am set now, getting things to work.