since you are creating an instance of the object in a global variable, which assumes that you want to subsequently assign values ââto it, rather than assigning a completely new object.
You can do this as suggested using the .Assign method, or you can copy the corresponding fields one by one if there are only a few.
You need a âdeep copyâ of the fields of the TUser object, and also make sure that if you have any objects inside TUser that you determine whether they should be deeply copied or if the links can be copied instead.
But this is a common mistake that people make. You want one or the other - either create an instance or copy VALUES; or do not create an instance and assign it another instance, for example the one that you get from TObjectManager. But, as also pointed out, the lifetime of the returned TObjectManager cannot be what you expect. (If it uses the interface, it automatically counts and safely assigns to another object. But this can be unobvious, without digging in this case or not.)
Since TObjectManager returns a reference to an object or NIL, you can simply assign the TObjectManager value directly to a global variable. In this case, do not create a default instance in the initialization section.
I also repeat moods around passing an object to form constructors, rather than using a global variable. This allows you to configure unit testing.
I would also like to add a comment that no one has touched yet.
else begin LoggedInUser:=u; //Here I assign my local User data to my global User variable Form1.Destroy; A00Form.visible:=true; end;
This is NOT how you destroy form! Because, technically speaking, nothing after calling Form1.Destroy does not work inside a valid instance. It probably works fine because the stack is probably not corrupted; but does anyone know if A00Form.visible will work: = true or not.
And ... this is not how you close the form and still focus on the other. This form should not be aware of any other forms. Again, this is what should be entered when the form is created, if necessary, which is not really the case.
Generally speaking, use the OnClose handler to do what you want when the form closes. But in this case, you donât even want to.
You want to instantiate the form from another location, and then use ShowModal to display it. When it closes, you want to set up some kind of return status that says if it is registered correctly. If they did, THEN open any next form that you want to see. If not, you probably want to display them again in the login form with the message displayed on it.
It also indicates how you enter a TUser entry into each form - by wrapping the form, it opens inside the method, which creates an instance of the form, passing the TUser object (via the constructor or property), and then gets by after ShowModal returns. This may mean that you only want to HIDE the form at the close, and not for FREE, allowing the control display method to access the form data before destroying the form! (I believe that caHide is the default action for the OnClose handler, so you will need to add OnClose if you want to replace it with caFree. This is because the IDE's default behavior is to automatically create forms in which if you don't want they are freed when they close.If you do not automatically create your forms, you NEED to free them after creating them and displaying them using Show or ShowModal - unless you decide to leave them hanging in memory, which first defeats the goal of bypassing auto-creation. )