Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Component reading external variable? 1

Status
Not open for further replies.

djjd47130

Programmer
Nov 1, 2010
480
US
I got an idea for a component I'm building, and don't know if it's possible. The goal is to make the component recognize when an external boolean (one from not inside the component) changes. Specifically, I want to tell this component to monitor the value of the Active property of a server socket component. If at any time this value is changed from True to False, I want my component to recognize it and respond accordingly. The variable specified can be of any other component. I know I'll probably have to make a timer which is continually checking this value, but how to keep record of what value to read? It should be something like this...

Code:
procedure TMySocket.SetBoolToMonitor(var Variable: Bool);
begin
  //"Variable" represents the Boolean property to be monitored
  //I don't want to copy the value, I want to keep a pointer reference to this variable.
end;


JD Solutions
 
Pass a pointer to the value into the component and then readdress that to the boolean value.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Thanks, but I already assumed this. Pointers are still my least favorite subject, and don't know how to go about it. I don't even know how to store such a pointer, assign it, or read it. I know it involves using ^ and @ but how I have no clue.

Sorry, I learn by example, not by description, so I need some sample code for this scenario, including how to declare the pointer, how to assign it, and how to read it...

The best way I guess is fill-in-the-blank... Can anyone help?

Code:
unit Unit1;

interface

uses
  Classes, Windows, SysUtils;

type
  TMyComponent = class(TComponent)
  private
    fMyBoolPointer: Bool; //How to properly declare this as a pointer?
    procedure SetBoolToMonitor(var Variable: Bool); //How to properly pass the pointer into the procedure?
    function GetBoolValue: Bool;
    procedure SetBoolValue(Value: Bool);
  public
    property BoolToMonitor: Bool read fBoolToMonitor write SetBoolToMonitor;
      //How to properly declare this property as the pointer?
    property BoolValue: Bool read GetBoolValue write SetBoolValue;
  end;

implementation

procedure TMyComponent.SetBoolToMonitor(var Variable: Bool);
begin
  fMyBoolPointer:= Variable; //How to properly assign this as a pointer?
end;

function TMyComponent.GetBoolValue: Bool;
begin
  Result:= fMyBoolPointer; //How to properly read this from the pointer?
end;

procedure TMyComponent.SetBoolValue(Value: Bool);
begin
  fMyBoolPointer:= Value; //How to properly set this value via the pointer?
end;

end.


JD Solutions
 
There's a lot you can do with pointers and it's a useful topic. That given, this should be sufficient if you're wanting to see what is involved:
Code:
{$APPTYPE CONSOLE}
program ptrtest; uses windows;
var
  boolvalue: BOOL;
  boolptr: pointer;
begin
  boolvalue := false;
  writeln('Boolean Value: ', boolvalue);
  boolptr := @boolvalue;
  writeln('Boolean Pointer Value: ', BOOL(boolptr^) );
  Bool(boolptr^) := true;
  writeln('Boolean Pointer Value: ', BOOL(boolptr^) );
  readln;
end.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Thanks, great approach, I shall see what I can do with that.

JD Solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top