Welcome Guest ( Login | Register )



All times are UTC - 7 hours [ DST ]



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Access a tag via string and write to it
PostPosted: Tue Dec 04, 2012 1:20 am 
User avatar

Joined: Thu Nov 29, 2012 2:27 pm
Posts: 92

Offline
Hi!

Previous I been using the following method to read values from tags when accessing them via a string congaing their names.

Code:
 private GlobalDataItem GetGlobalDataItem(string propertyName)
 {
         PropertyInfo tagProperty = typeof (Neo.ApplicationFramework.Generated.Tags).GetProperty(propertyName);
         if(tagProperty == null)
                 return null;
         else
                 return tagProperty.GetValue(Globals.Tags, null) as GlobalDataItem;
}


Now I need to write to the tag instead of reading from it.

Is it possible?

Best Regards

Edmund

_________________
Edmund Andersson

AITECH AB

Part of Beijer Integrator Group


 Profile  
 
 Post subject: Re: Access a tag via string and write to it
PostPosted: Tue Dec 04, 2012 9:18 am 

Joined: Tue Mar 13, 2012 9:53 am
Posts: 824

Offline
You are referencing the Value property on the object that GetGlobalDataItem is returning. Just assign a value to that property instead of reading from it.

Code:
GetGlobalDataItem("Tag" + number.ToString()).Value = SomeValue

_________________
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer


 Profile  
 
 Post subject: Re: Access a tag via string and write to it
PostPosted: Tue Dec 04, 2012 3:14 pm 
User avatar

Joined: Thu Nov 29, 2012 2:27 pm
Posts: 92

Offline
Aaah Of course, why didn´t I think of that :o

The method doesn’t return the "value" but a reference to the value property of the object...

Thanks Mark!!

_________________
Edmund Andersson

AITECH AB

Part of Beijer Integrator Group


 Profile  
 
 Post subject: Re: Access a tag via string and write to it
PostPosted: Tue Feb 19, 2013 7:03 am 

Joined: Wed Feb 13, 2013 7:09 am
Posts: 2

Offline
I've tried this and it compiles, but later causes the Qterm A7 to crash when this is called. Am I making some mistake here?

Code:
namespace Neo.ApplicationFramework.Generated
{
    using System.Windows.Forms;
    using System;
    using System.Drawing;
    using Neo.ApplicationFramework.Tools;
    using Neo.ApplicationFramework.Common.Graphics.Logic;
    using Neo.ApplicationFramework.Controls;
    using Neo.ApplicationFramework.Interfaces;
       
   using Neo.ApplicationFramework.Tools.OpcClient;
   using System.Reflection;
   
    public partial class WellAnalysisParams
    {
      //This Method allows referencing tags in arrays
      private GlobalDataItem GetGlobalDataItem(string propertyName)
      {
         PropertyInfo tagProperty = typeof(Neo.ApplicationFramework.Generated.Tags).GetProperty(propertyName);
         if(tagProperty == null)
            return null;
         else
            return tagProperty.GetValue(Globals.Tags, null) as GlobalDataItem;         
      }
      
      void CurrentWellNumber_InputValueChanged(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
      {
         //This Method used to take the well number and save it in the correct
         //well analysis data memory section
         
         //Tag name should look like WellXSourceData13 (Wells 1-30)
         int wellNumber = Globals.Tags.AnalysisWellSelector.Value.Int;
         int newWellNumber = Globals.Tags.AnalysisWellNumber.Value.Int;
         
         GetGlobalDataItem("Well" + wellNumber.ToString() + "SourceData13").Value = newWellNumber;   
      }
    }
}


 Profile  
 
 Post subject: Re: Access a tag via string and write to it
PostPosted: Tue Feb 19, 2013 9:02 am 

Joined: Tue Mar 13, 2012 9:53 am
Posts: 824

Offline
What error does it crash with? You should be able to run it on your PC in iX Developer and it should generate an exception.

You may need to do:
GetGlobalDataItem("Well" + wellNumber.ToString() + "SourceData13").Value.Int

It depends on what the data type of that particular tag is set to.

_________________
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer


 Profile  
 
 Post subject: Re: Access a tag via string and write to it
PostPosted: Tue Feb 19, 2013 12:43 pm 

Joined: Wed Feb 13, 2013 7:09 am
Posts: 2

Offline
mark.monroe wrote:
What error does it crash with? You should be able to run it on your PC in iX Developer and it should generate an exception.


Object reference not set to an instance of an object.

mark.monroe wrote:
You may need to do:
GetGlobalDataItem("Well" + wellNumber.ToString() + "SourceData13").Value.Int

It depends on what the data type of that particular tag is set to.


Throws compile-time error:
Property or indexer 'Neo.ApplicationFramework.Interfaces.VariantValue.Int' cannot be assigned to -- it is read only


 Profile  
 
 Post subject: Re: Access a tag via string and write to it
PostPosted: Tue Feb 19, 2013 2:34 pm 

Joined: Tue Mar 13, 2012 9:53 am
Posts: 824

Offline
That normally means that it can not find the object that you are referencing.

This code:
GetGlobalDataItem("Well" + wellNumber.ToString() + "SourceData13").Value

Maybe referencing a tag that does not exist.

_________________
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer


 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 7 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: