PDM, Task Add-in, Is anyone else using a [Serializable()] class/struct for task variables?

Programming and macros
User avatar
bnemec
Posts: 1869
Joined: Tue Mar 09, 2021 9:22 am
Answers: 10
Location: Wisconsin USA
x 2466
x 1344

PDM, Task Add-in, Is anyone else using a [Serializable()] class/struct for task variables?

Unread post by bnemec »

When I started framing up our PDM Task Add-in (new to PDM) I needed to use the SetValEx and GetValEx for some data pertaining to the task itself. Having a tiny bit of Object Oriented programming background, I started getting the heebie-geebies from calling Set/GetValEx(“variableName”, variableValue) all over the place I had the variable names as constants but there were different data types and a lot of casting I wasn’t liking it. Then when I wanted to store an enumerated type but couldn’t (either needed to just store as string, number or serialize) I restructured a bit.

I created a class “TaskVarEx” to encapsulate all the variables with the Serialize method, the default constructor for initial values, an overriding constructor that takes a string parameter to deserialize and Properties to access all the variables. The variables themselves are each private in the serializable struct “Vars” which is public but only instantiated within the scope of the TaskVarEx class. So all of the variables are stuffed into one task variable, the name of that variable is stored in the static variable:

Code: Select all

public const string VARS_EX_NAME = "VARS";
Getting the variables object looks like this in C#:

Code: Select all

IEdmTaskProperties props = (IEdmTaskProperties)poCmd.mpoExtra;
…
TaskVarEx taskVars = new TaskVarEx((string)props.GetValEx(TaskVarEx.VARS_EX_NAME));
Accessing them is as simple as using the Property:

Code: Select all

taskVars.TaskType 
or
taskVars.ShowMenu
And storing them:

Code: Select all

task = (IEdmTaskInstance)poCmd.mpoExtra;
…
task.SetValEx(TaskVarEx.VARS_EX_NAME, taskVars.Serialize());

The add in has been running this way for over a year and I have not yet found issues with this method of encapsulating the task variables. There’s been other issues I’ve addressed, but so far this is working. I my mind it makes the code much simpler to read and a bit more inline with object oriented programming. I’ve been curious is anyone else is doing something similar with their custom task add-in.

Thanks.
Post Reply