Access a tag via string and write to it

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
Post Reply
User avatar
Edmund
Posts: 92
Joined: Thu Nov 29, 2012 2:27 pm

Access a tag via string and write to it

Post by Edmund »

Hi!

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

Code: Select all

 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

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

Re: Access a tag via string and write to it

Post by mark.monroe »

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: Select all

GetGlobalDataItem("Tag" + number.ToString()).Value = SomeValue
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

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

Re: Access a tag via string and write to it

Post by Edmund »

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

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

Re: Access a tag via string and write to it

Post by rossmoffett »

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: Select all

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;	
		}
    }
}

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

Re: Access a tag via string and write to it

Post by mark.monroe »

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

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

Re: Access a tag via string and write to it

Post by rossmoffett »

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

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

Re: Access a tag via string and write to it

Post by mark.monroe »

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

Post Reply