R.,
I think that the main issue here is that the State argument itself cannot be changed, once it is passed to the SubscribeXXXX method. This is by design.
What happens with the TextBox etc. is that you can change the contents of it, i.e. a property of that text object, but you still cannot (in the event handler) exchange the text box for a different object.
So, when you pass in a ‘bool’, this is not a reference type but rather an immutable value type, the behavior is the same: You cannot change it (and it has no properties either).
This can be overcome, though: The customer can create a simple class that will just hold the variable, something like:
class BoolVariable
{
public bool Value { get; set; }
}
Then create an instance of it before the SubscribeXXXX call:
var bool1 = new BoolVariable();
and then pass Bool1 as State to SubscribeXXXX.
Accessing the value will require to use constructs like bool1.Value instead of just bool1.
In the event handler, the value could be changed by
((BoolVariable)e.State).Value = (bool)e.vtq.Value;
Separate classes will be needed for other data types.
In order to handle more data types than just bool, the event handler will have to recognize which object is in the State (e.g. by using the ‘is’ operator test), or, you can Subscribe separately for each data types, and use a callback method that’ll handle just State-s of that particular data type, different with each Subscribe.
I haven’t tried any of this actually, but it should work; of you run into problems, let me know. The class can even be made into a generic:
class Variable
{
public T Value { get; set; }
}
And then the code would use Variable, Variable etc.
It might also be possible to cheat a little and use Nullable class from .NET framework that originally has a different purpose but could serve as well.
I should say, however, that without further coding, this approach has an inherent synchronization problem, and to make it right, there should probably also be some locking code added.
Version 5.20 will support “mapping” of OPC items to properties and fields on .NET objects. The customer will then be able to declare an object holding his variables, set up the mapping to OPC items, start the subscription, and the properties will be changed automatically, without writing an event handler at all.
Best regards,