Ashley West
Ashley West

Reputation: 53

How to find variable calling TdwsGlobal.onReadVar/OnWriteVar event

I am dynamically creating variables within a DWSUnit as follows:

v := dwsUnit.Variables.Add('c', 'float'); // etc
v.OnWriteVar := writeVar;
v.OnReadVar := readVar;

All variables point to the same event procedure.

The problems is I can't figure out how to determine which variable triggered the event.

The event only gives me a TprogramInfo structure but I can't find anything in it to identify the actual variable.

If I created the variables at design time I could have a seperate event for each variable, but this is not possible when the variables are dynamically created.

Am I missing something here.

I've only just started using DWS and have got most of what I need working great, but I'm stuck here!

Upvotes: 3

Views: 175

Answers (1)

Eric Grange
Eric Grange

Reputation: 6211

You're supposed to have an event attached to the variable. If you're creating them dynamically and want to keep track of the variable, you could move you event to a class, create that class and use it for the event, something like

type
   TVariableEventHandler = class
      Name : String; // store variable name here (or better, something more relevant)
      procedure DoWrite(info: TProgramInfo; var value : Variant);
      procedure DoRead(info: TProgramInfo; const value : Variant);
   end;

then when declaring the variable

handler := TVariableEventHandler.Create; 
handler.Name := 'c'; // etc
v := dwsUnit.Variables.Add('c', 'float'); // etc
v.OnWriteVar := handler.DoWrite;
v.OnReadVar := handler.DoRead;

in practice, rather than storing the Name in the handler instance, you would probably be better off storing a more direct reference to whatever you're truly exposing.

You can also make the handler's DoWrite/DoRead methods virtual, and provide specialized implementations.

Upvotes: 2

Related Questions