How to Get Tag Value using string to Tag Function ?

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
Post Reply
ParthDesai
Posts: 9
Joined: Fri Jan 15, 2016 4:00 am

How to Get Tag Value using string to Tag Function ?

Post by ParthDesai »

Hello,

I am using string to tag function given below and wanted to show tag value in message box on button click event. "value1" is my Tag Name.
When i click on button it shows error in the log file as
Object reference not set to an instance of an object.
Below is my Code

Code: Select all

void Button1_Click(System.Object sender, System.EventArgs e)
		{
		 MessageBox.Show(StringToTag("value1").Value);
		
			
		}
		// returns null if tag not found
		public GlobalDataItem StringToTag(string tagName)
		{
			string typeName = (new GlobalDataItem()).GetType().Name;
			PropertyInfo[] props = Globals.Tags.GetType().GetProperties();
			foreach (PropertyInfo prop in props) {
				if (prop.PropertyType.Name.Equals(typeName)) {
					if (prop.Name.Equals(tagName, StringComparison.CurrentCultureIgnoreCase)) {
						return (GlobalDataItem)prop.GetValue(Globals.Tags, null);
					}               
				}
			}
			return null;
		}

andis59
Posts: 13
Joined: Mon Apr 08, 2013 11:50 am

Re: How to Get Tag Value using string to Tag Function ?

Post by andis59 »

The problem is that the StringToTag don't work!
It will always return null.

You should never do:

Code: Select all

MessageBox.Show(StringToTag("value1").Value);
When you have a function that can return null.

You need to split it into several lines

Code: Select all

GlobalDataItem item = StringToTag("value1");
if(item!=null)
{
  // here you might want to check if the tag has any value...
  MessageBox.Show(item.Value);
}
But at the moment this will not work since the StringToTag function always returns null...

// Anders

AMitchneck
Posts: 137
Joined: Mon Jun 11, 2012 2:10 pm

Re: How to Get Tag Value using string to Tag Function ?

Post by AMitchneck »

After doing a lot of digging I discovered iX 2.20 no longer creates tags as properties. They are now created as fields (public variables). This seems to have been to reduce baggage created by defining tags as properties. I also found iX has two types of tags it can create: GlobalDataItem and LightweightTag. Although these classes are similar, they cannot be recast to the other type. To deal with this, I created a generic StringToTag function

In the Tags script, add the following static function

Code: Select all

public static T StringToTag<T>(string tagName) where T : class
{
	Type type = typeof(T);
	FieldInfo[] props = Globals.Tags.GetType().GetFields();
	foreach (FieldInfo prop in props) {
		if (prop.FieldType.Equals(type)) {
			if (prop.Name.Equals(tagName, StringComparison.CurrentCultureIgnoreCase)) {
				return (T)prop.GetValue(Globals.Tags);
			}               
		}
	}
	return null;
}
Do not forget to add "using System.Reflection;" to head of Tags script.

As an example of how to use the function, I created a sample screen named Screen1 with a single button, script for screen shown below

Code: Select all

//--------------------------------------------------------------
// Press F1 to get help about using script.
// To access an object that is not located in the current class, start the call with Globals.
// When using events and timers be cautious not to generate memoryleaks,
// please see the help for more information.
//---------------------------------------------------------------

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 GlobalDataItem = Neo.ApplicationFramework.Tools.OpcClient.GlobalDataItem;
	using LightweightTag = Neo.ApplicationFramework.Tools.OpcClient.LightweightTag;
    
    public partial class Screen1
    {
		
		void Button_Click(System.Object sender, System.EventArgs e)
		{
			GlobalDataItem tag = Tags.StringToTag<GlobalDataItem>("Tag1");
			if (tag == null)
			{
				LightweightTag tag2 = Tags.StringToTag<LightweightTag>("Tag1");
				if (tag2 == null)
				{
					MessageBox.Show("Error");
				}
				else
				{
					MessageBox.Show(tag2.Value.ToString());
				}
			}
			else
			{
				MessageBox.Show(tag.Value.ToString());
			}
		}
    }
}
If you only want to use the tag's "Value" property, you could also add the following to the Tags script to reduce coding elsewhere:

Code: Select all

public VariantValue this[string tagName]
{
	get
	{
		GlobalDataItem GDI = Tags.StringToTag<GlobalDataItem>(tagName);
		if (GDI == null)
		{
			LightweightTag LWT = Tags.StringToTag<LightweightTag>(tagName);
			if (LWT == null)
				throw new NotSupportedException(tagName);
			else
				return LWT.Value;
		}
		else
			return GDI.Value;
	}
	set
	{
		GlobalDataItem GDI = Tags.StringToTag<GlobalDataItem>(tagName);
		if (GDI == null)
		{
			LightweightTag LWT = Tags.StringToTag<LightweightTag>(tagName);
			if (LWT == null)
				throw new NotSupportedException(tagName);
			else
				LWT.Value = value;
		}
		else
			GDI.Value = value;
	}
}
This will enable you to use the code "Globals.Tags["Tag1"]" to read/write to Tag1. You may want to add a catch block just in case the function throws a "NotSupportedException". (Note: you will need to add the using remarks for GlobalDataItem and LightweightTag as in the Screen1 example in the Tags script.)

Hope this helps.
Adam M.
Controls Engineer
FlexEnergy

andis59
Posts: 13
Joined: Mon Apr 08, 2013 11:50 am

Re: How to Get Tag Value using string to Tag Function ?

Post by andis59 »

Wow! That must have taken a while to figure out!

Thank you very much!

// Anders

ladin79
Posts: 19
Joined: Tue Nov 13, 2012 12:25 pm

Re: How to Get Tag Value using string to Tag Function ?

Post by ladin79 »

Good morning,
I tried to use the ValueChange event of a LightweightTag in a script but it does not seem to work, someone has already had some experience on it?
Thank you.

Nightf1y
Posts: 3
Joined: Thu Dec 03, 2015 2:48 pm

Re: How to Get Tag Value using string to Tag Function ?

Post by Nightf1y »

AMitchneck

Very helpful - thanks

Post Reply