Resolved how to best use "public"read-write properties of event-driven applications

mauede

Well-known member
Joined
Sep 1, 2021
Messages
103
Location
Northwood - UK
Programming Experience
Beginner
I am trying to convert a working Console UI application into a WPF one. I use an s/w library from Varian Medical Systems, called Velocity API, whose documentation is scarce and somewhat misleading. Velocity API hardcoded logic is built on context. My Console UI application calls several sequential Velocity API methods. However, a GUI must allow for consistent, event-driven actions.
I need one, maybe more, “state” variables to update the context expected by each Velocity API method. I declare most of the properties “public” and accessed through {get; set;}. I do not have a clear understanding of how such properties work. For example, one of my “state” variables is a Boolean variable called “FirstPass” that Is to distinguish between the first time a patient’s CT scan has been selected and the user’s second or n_th choice. Failure to provide the expected context causes the system to throw a memory access violation exception upon calling a Velocity method. The above-mentioned property is defined as follows:
C#:
private bool _firstPass;
public bool FirstPass
  {
get {return _firstPass;}
set {_firstPass = value;}
 }
This property must be set “true” at the application start time and when another patient‘s data is loaded. It must be set “false” after a CT scan has been selected. Unluckily, the runtime value of such a state variable, which I check through the debugger, Is not what I expect. I attribute that to my not clear understanding of the {get; set;} mechanism. I am afraid I am not using it correctly. I would appreciate some clarifying explanations and examples. Thank you very much in advance.
 
Last edited by a moderator:
Declaring a variable (or property) public static is basically declaring a global variable. Global variables and singletons should be avoided to help keep code testable and maintainable.

var pt = new PTAccess(); and PTAccess pt = new PTAccess(); as well as PTAccess pt = new(); are all equivalent.
 
If I declare FirstPass simply as "public" with {get;set} am I sure that it can be accessed in both read/write mode by methods that belong to another class in another file but are still part of the same namespace?

Thank you
 
When FirstPass is accessed by a method of class MainWindow (in another file) I have to create an instance of class PTAccess to do that. Which one of the following two statements is correct?
Neither one.

The most correct thing to do is to inject an instance of PTAccess into the instance of MainWindowViewModel.

The next passable thing is to
inject an instance of PTAccess into the instance of MainWindow.

What will work is to create an instance of PTAccess in the instance of MainWindow.

In all cases above, store it in an instance variable.
 
If I declare FirstPass simply as "public" with {get;set} am I sure that it can be accessed in both read/write mode by methods that belong to another class in another file but are still part of the same namespace?
Yes. Even if they are in different name spaces, you can still get access to them by fully qualifying the class name.
 
Thank you very much for all your help and patience.
I could make some progress despite the behaviour of Velocity API that is not documented and quite erratic.
I have attached my first GUI.
It was hard to display the 4th column (the one with yellow background). Not because of my gaps with C# and WPF. Instead because of the unexpected and undocumented behaviour of Velocity API methods. I just achieved this result by trial & error with Velocity API methos.
I have not understood the cause of my previous several failures.
The next challenge will come from WPF. I will have to display a list of strings on a fifth ListBox whose items will have to allow editing.
I am posting another question explaining my problem to get some guidelines.
Thank you again
 

Attachments

  • Screenshot 2022-03-05 at 20.36.30.png
    Screenshot 2022-03-05 at 20.36.30.png
    917.7 KB · Views: 142
Back
Top Bottom