Page 1 of 1

re-usability of buttons and verification screens question

Posted: Mon Jun 18, 2018 2:12 pm
by stasKanRich
Hello All!

I am new to C# and having trouble "finding" the tags allocated to my object.

I want to be able dynamically without hardcoding the tag name to be able to pass it on to (a pop up screen or anything else)

So when i select a Tag in the tag security section(see attached picture) i can use in in the script as sender. Tag or something similar. this is probable not a "stock supported" feature.

here is where i am at after fighting with this for some time now:

i have added this to the Tags class(gets a tag from passing it the name - works fine):

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

now on a button click script action i do something like:

Code: Select all

	void Button_Click(System.Object sender, System.EventArgs e)
		{
                  var TagName = "TestTag2";
GlobalDataItem tag = Tags.StringToTag<GlobalDataItem>(TagName);
		
	LightweightTag tag2 = Tags.StringToTag<LightweightTag>(TagName);
MessageBox.Show(  tag2.ToString());
                }

i cant find a way to track the assigned tag name or even direct reference to the tag in the button/sender variables.

Please help? is there an easier way to do this?

All i need is a Sender.Tag type reference back to the UI chosen tag to allow me to requse the code and button without making a million instances of the same thing in my projects..



[/color]

Re: re-usability of buttons and verification screens questio

Posted: Fri Jun 22, 2018 3:07 pm
by stasKanRich
After forcing my way...
I was able to get the Tag by "Running through the code with a chainsaw"..

I get the field infos from the sender obj.
Then I either look for data item or dynamic bindings in the sender (depending on obj type)
I then use this data to to comb my way in Globals.Tags info fiends to get my tag...

half a page of code to get the tag associated with the sender without knowing the tag name.

There's got to be a better way !!!! :)

is there? Please :) ?

Re: re-usability of buttons and verification screens questio

Posted: Fri Sep 07, 2018 6:29 am
by Derkins.Goblertroter
Keep in mind that C# is a statically typed language, if you found a solution to your problem albeit long or complicated, you should celebrate.