Showing results 1 to 6 of 6

Thread: assign variables in structured data type

  1. #1
    Join Date
    09.04.2015
    Posts
    15

    Default assign variables in structured data type

    Hello, I am trying to write a new value into a structured (an array )variable with C # that already exists in Zenon and has a user-defined data type.
    I do following:
    Code:
    // create variable with var. name from Zenon
    const string PV_Anlagen = "PV-Anlagen";
    ...
    //Create a new container
    _container = context.OnlineVariableContainerCollection.Create(_containerName);
    //Create list of variables 
    List variablesList = new List();
    // add variable to var. list
    variablesList.Add(PV_Anlagen);
    // add to container
    if (!_container.AddVariable(variablesList.ToArray()))
    {
        Debug.Print("List of variables could not be added to the container");
        return;
    }
    I have following questions:
    1. the addition ( _container.AddVariable(VariableName) ) of pv-anlagen-variable fails. How can I make this variable known in the project?
    2. How can I access the following part: for example PV-Anlagen[2].Bezeichnung?
    Thank you in advance for your support.
    Attached Thumbnails Attached Thumbnails Screenshot.png  
    Last edited by genakust : 7th October 2020 at 13:21

  2. #2
    Join Date
    10.12.2007
    Posts
    226

    Default AW: assign variables in structured data type

    You have to add the Variablenames to the variableList before you can add the (String-)List to the container.

  3. #3
    Join Date
    09.04.2015
    Posts
    15

    Default AW: assign variables in structured data type

    Hello mst, thank you for your response. I will check your proposal but I have other variables in the project which works correctly. 

  4. #4
    Join Date
    10.12.2007
    Posts
    226

    Default AW: assign variables in structured data type

    That is possible if you read variables that are online because of so other reason e.g. they are used in acurrently opened picture, or they have a limit that is marked as alarm, or they are archived, or they are marked as always read, or... (This can be checked in code by the .IsOnline method)
    Otherwise before you read the value of a variable you need an onlinevariable (container) and add the variable to the container with the add method and the variablename.
    In your codesample above I can only see declaration of an empty List<String>, but you do not add any variablenames to this list. Therefore you don't add any variables to the container when you do _container.AddVariable(variablesList.ToArray()) because variablesList is empty.

  5. #5
    Join Date
    09.04.2015
    Posts
    15

    Default AW: assign variables in structured data type

    Hello, meanwhile my solution looks like this:

    Code:
    const string PV_Anlagen_1_Prognose_6h = "PV-Anlagen[1].Prognose_6h";
    IVariableCollection myVariables = _project.VariableCollection;
                if (myVariables != null) {
                    foreach (var item in myVariables)
                    {
                        
                        if (item.Name.Equals(PV_Anlagen_1_Prognose_6h))
                        {
                            IVariable hit = item;
                            hit.SetValue(...);
                        }
                    }
                }
    but if I would add to the PV-Anlagen variable another item I would have to write another variable for example 
    Code:
    const string PV_Anlagen_2_Prognose_6h = "PV-Anlagen[2].Prognose_6h";
    and so on.

    How or if it is possible to access the variable via its index?

    for example:
    var dummy = item;
    for(i = 0; i < dummy.Length; i++)
    dummy[i]....

    Thank you.

  • #6
    Join Date
    10.12.2007
    Posts
    226

    Beitrag AW: assign variables in structured data type

    Now it's getting very basic and different from zhe original question. It would help if you explain a little bit more what you want to do...

    Why don't you adress the variables you need directly via their names? Something like (out of mind - spelling may be wrong, just to give you an idea):
    const int MaxPV_anlagenindex = 10 'Max Index of PV_Anlagen Array
    for(int i=1; i<=MaxPV_Anlagenindex; i++)
    {
      IVariable zVar=_project.VariablesCollection.Item("PV-Anlagen[" + i.ToString() + "].Prognose_6h");
      zVar.SetValue(.... 'Do whatever
    }

  • Similar Threads

    1. Structured data type composite of several variables and types
      By RaphaelVogt in forum zenon Operator
      Replies: 2
      Last Post: 4th February 2020, 08:13
    2. Replies: 0
      Last Post: 27th February 2018, 09:36
    3. Nested Structured Data Type in Combi Element
      By ansab.tp in forum zenon Supervisor
      Replies: 2
      Last Post: 21st March 2012, 07:38
    4. Replies: 6
      Last Post: 17th November 2011, 00:05
    5. Nested structured Data types
      By bsneddon in forum Drivers
      Replies: 5
      Last Post: 3rd April 2011, 23:23

    Posting Rules

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •