Adding a Variable to a Group

GuyS (45 posts)
February 12, 2018 08:48 AM
Accepted Answer

Hi Bill,

I'm trying to add/update variables to a Group using the code as included.

It works oke, but I need the 'Include in program's environment variable' to be set when the variable is new. How can I do that?

 

Thanks,

Guy

 

public void SetAdtGroupVariable(ref ListView lvMsg, ref int msgCnt, string groep, string var, string value)
        {
            using (DataContext context = aScheduler.NewDataContext())
            {
                try
                {
                    var group = context.GetJobGroup(groep);
                    JobVariable variable = group.JobVariables.GetVariable(var);
 
                    if (variable == null)
                    {
                        // JobVariable variable = new JobVariable();
                        variable = (JobVariable)Convert.ChangeType(context.CreateObject(ClassID.JobVariable), typeof(JobVariable));
                        variable.Name = var;
                        variable.Value = value;
                        group.JobVariables.Add(variable);
 
                        msgCnt += 1;
                        var item1 = new ListViewItem(new[] { "(I) Variable '" + var + "' added",
                            msgCnt.ToString().PadLeft(4,'0') });
                        lvMsg.Items.Add(item1);
                        group.Save();
                    }
                    else
                    {
                        variable.Value = value;
                        group.Save();
 
                        msgCnt += 1;
                        var item1 = new ListViewItem(new[] { "(I) Variable '" + var + "' has been set to '" + value + "'",
                            msgCnt.ToString().PadLeft(4,'0') });
                        lvMsg.Items.Add(item1);
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show("??" + e.ToString());
                    throw;
                }
            }
        }

 

Bill Staff (599 posts)
February 12, 2018 09:00 AM
Accepted Answer

You can do this by setting variable.AddToEnvironment=true.

By the way you can simplify your object creation syntax. Instead of using ChangeType, you can just use

variable = (JobVariable)context.CreateObject(ClassID.JobVariable);

This is because the object returned by CreateObject(ClassID.JobVariable) is guaranteed to be of type JobVariable, so you don't need to convert it--you just need the cast to tell the compiler what it is.

GuyS (45 posts)
February 13, 2018 12:55 AM
Accepted Answer

Hi Bill,

You are faster then light. How easy it can be, thanks for answer and hint.

Kind regards,

Guy

Replies are disabled for this topic.