Page 1 of 1

How to do a AND

Posted: Mon Dec 10, 2012 10:38 am
by KevinA.
I have to enable a light when two controller tags are active so I was wondering if there is any way to do a AND without having to use a script?

Re: How to do a AND

Posted: Tue Dec 11, 2012 4:25 am
by Edmund
Are you running on a PC or a Panel?

If you are running on a PC you could let the first tag operate the Visibility property and the second tag the Opacity property (I believe Opacity is only available on PC-targets).

On a panel you can put a rectangle (in the same color as your background) on top and let one of the tags hiding it and the other one the "light-icon"...

Maybe there is some other and more simpler way, but not that I know of (without using scripts).

Best Regards

Edmund

Re: How to do a AND

Posted: Tue Dec 11, 2012 7:55 am
by KevinA.
And with a script how would it be? I tried to code it referring myself to a website to learn how to code C # but it downt work at all.

Re: How to do a AND

Posted: Tue Dec 11, 2012 8:56 am
by mark.monroe

Code: Select all

if((Globals.Tags.MyTag1.Value == 1) && (Globals.Tags.MyTag2.Value == 1)){
   //Do Something here
}

http://csharp.net-tutorials.com/basics/if-statement/

Re: How to do a AND

Posted: Tue Dec 11, 2012 9:49 am
by Edmund
Well, easiest way would be.

Go to tags, create a new tag (internal, BOOL) called Tag3.

Hit the script tab in the bottom.

To the left you have all your tags, expand tag 1 and DoubleClick ValueChange.

A new eventhandler is created for you

Code: Select all

void Tag1_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
			
}
This function will be called every time the tag changes value.

Now we have to check if the tags are both true and if they are lets set our third tag to true as well.

Code: Select all

void Tag1_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
	if(Tag1.Value == true && Tag2.Value == true)
	{
		Tag3.Value = true;
	}
	else
	{
		Tag3.Value = false;
	}
}
So now we check if both are true then we set Tag3 to true or false.

But right now we only check the status each time Tag1 is changed, we also need to do this when Tag2 is changed.

Code: Select all

void Tag2_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
	if(Tag1.Value == true && Tag2.Value == true)
	{
		Tag3.Value = true;
	}
	else
	{
		Tag3.Value = false;
	}
}
This will work but it creates duplicated code, which is BAD!

Let´s create a little function that we can call from each eventhandlers.

Code: Select all

void DetermineStatusForTag3()
{
	if(Tag1.Value == true && Tag2.Value == true)
	{
		Tag3.Value = true;
	}
	else
	{
		Tag3.Value = false;
	}
}
And call the function from the eventhandlers

Code: Select all

void Tag1_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
	DetermineStatusForTag3();
}
		
void Tag2_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
	DetermineStatusForTag3();
}
Of course we can make this even better and more flexible by passing the tags as parameters to the function and so on... But that’s the next lesion.

Complete code

Code: Select all

void Tag1_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
	DetermineStatusForTag3();
}
		
void Tag2_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
	DetermineStatusForTag3();
}


void DetermineStatusForTag3()
{
	if(Tag1.Value == true && Tag2.Value == true)
	{
		Tag3.Value = true;
	}
	else
	{
		Tag3.Value = false;
	}
}
Now you can use Tag3 to show or hide your light.

Best Regards

Re: How to do a AND

Posted: Tue Dec 11, 2012 10:08 am
by KevinA.
Great!! Thanks a lot Edmund, that really helped me to understand a bit more the way C# works, thank you very much!!