Page 1 of 1

Access a tag via string and write to it

Posted: Tue Dec 04, 2012 1:20 am
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

Re: Access a tag via string and write to it

Posted: Tue Dec 04, 2012 9:18 am
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

Re: Access a tag via string and write to it

Posted: Tue Dec 04, 2012 3:14 pm
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!!

Re: Access a tag via string and write to it

Posted: Tue Feb 19, 2013 7:03 am
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;	
		}
    }
}

Re: Access a tag via string and write to it

Posted: Tue Feb 19, 2013 9:02 am
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.

Re: Access a tag via string and write to it

Posted: Tue Feb 19, 2013 12:43 pm
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

Re: Access a tag via string and write to it

Posted: Tue Feb 19, 2013 2:34 pm
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.