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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dreaded Self, why here?

Status
Not open for further replies.

enonod

Programmer
Jan 7, 2005
22
GB
Using the following extract will somebody please tell me why, in the implementation section (blue), I must put self in front of height and width otherwise I get zero as the values??
Also why wasn't it required in the other (red) position?

The code was working without this when I first wrote it some months ago so it seems that I have done something somewhere, possibly even in the IDE that has affected it.
How can I cure this please?
Thank you

Code:
interface

uses windows, graphics, classes, math, controls, SysUtils,Reuseable;

type

  TBody = class(TGraphicControl)
  private
    FOrientation: Char;   //Can be H or V or U for Upright
    FPen: TPen;
    FBrush: TBrush;
    Fmiddle: TPoint;
    procedure SetOrientation(value: Char);
    procedure SetMiddle(value: TPoint);
    procedure SetPen(value: TPen);
    procedure SetBrush(value: TBrush);
  protected
    procedure Paint; override;
    procedure StyleChange(sender: TObject); dynamic;
    procedure MouseDown(Button: TMouseButton;
             Shift: TShiftState; X, Y: Integer);override;
    procedure MouseMove(Shift: TShiftState; X, Y: integer);override;
  published
    procedure DblClick;override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property Orientation: Char read FOrientation 
                           write SetOrientation;
    property middle: TPoint read Fmiddle write SetMiddle;
  end;

implementation

procedure TBody.DblClick;
var	temp: integer;
begin
	inherited DblClick;

  if (formm.AxialLock.checked) or (Forientation='U') then
     exit;
{Will body fit on other wseg?}
	with Board1.Wires[tag] do
  begin
    if wseg[bodywire xor 7].length >= max([COLOR=red][b]width,height[/b][/color]) then
    begin
      bodywire:=bodywire xor 7;
[COLOR=blue][b]      temp:=self.height;
      self.height:=self.width;
      self.width:=temp;
[/b][/color]      if Forientation='H' then
        orientation:='V'
      else if Forientation='V' then
        orientation:='H';
      middle:=wseg[bodyWire].middle;
    end;
  end;
end;
 
Even though your inheritance ought to preclude it and this may be a more basic problem caused by the way you have set this up.
Height and width are fields of lots of things, A 'with' statement dosnt always keep you out of trouble, so the compiler may be making the wrong assumptions.
My guess is that the blue bits are assignments where the type must be correct.
The red bits are a comparison so it dosnt matter it just return the max whatever it is.
Try using the full syntax. (some people never use 'with')



Steve:
A Delphi Programmer
A Feersum Endjinn indeed
 

Just to clarify Steve's post:

Since TBody descends from TGraphicControl, it has Height and Width properties (from TControl which is in the hierarchy).

You are referencing a variable called Board1 but you did not post the definition, but based on your symptoms, I would guess it also has Height and Width properties. Most likely you have descended it also from a TControl or one of its descendants.

by wrapping your code in with Board1... any unqualified reference to Width and Height will be the width and height of Board1. If you want the width and height of TBody you will need to qualify the properties with Self.

I would further infer that at this point of the code, you have created a Board1 object (Type TBoard?) in code, but have not given it a height or width. When you drop a component on a form using the palette, it already has a default height and width, but when you create one yourself in code, it doesn't. So it is up to you to set the height and width properties.

If I may be permitted another observation... You are referencing a variable (Board1) inside of the TBody.DblClick procedure. Your code might be easier to follow if you named the procedure according to what it is doing and passed the reference explicitly as a parameter:
[tt]
procedure TBody.MakeFit( ABoard:TBoard );
[/tt]
then called the procedure from your event handler with something like
[tt]
procedure DblClick;
begin
MakeFit( Board1 );
end;
[/tt]
Nothing wrong with using "with" but you have to be careful when dealing with more than one object at a time.
 
Thank you both for your observations. I believe I follow what has been said with the exception of the last part from Zathras. Since I am constantly trying to learn, all observations are welcome, especially critical.
I did omit the Board1 setup thinking it irrelevant and thought too much code would confuse the issue. I now enclose the declarations hoping you may further clarify the part about referencing Board1, or any other comments on my mistakes. I can supply all the other objects as well if required.

Board1 is a TScrollBox with TControl Canvas upon which everything is drawn. All graphicControls are parented by Board1 (I think).
Wires is a TObjectList containing wire objects that consist of segmented line objects (wseg)
Wseg are TGraphicControls to permit stretching of wires in different directions.
Wseg parent is Board1.
The Wire objects can have a TBody attached, also a TGraphicControl as already shown.
Thank you!
Code:
[COLOR=green]//==============Unit Board1================================[/color]
  Twires=Class(TObjectList)
  protected
    function GetItem(Index: Integer): TWire;
  public
    constructor Create;
    destructor destroy; override;
    function Add(AObject: TWire): Integer;
    property Items[Index: Integer]: 
             TWire read GetItem;      default;
    procedure CalcPoly;
  end;
//-------------------------------------------------------
  TBoard=class(TScrollBox)
  private
    FCanvas: TCanvas;
  public
    Origin, Current: TPoint;
    Drawing: Boolean;
    Strips: TStrips;
    Wires:	TWires;
    constructor create(Aowner: TComponent); override;
    destructor destroy; override;
    property Canvas: TCanvas read FCanvas;
  end;
//-------------------------------------------------------
var	Board1:	TBoard;
implementation

constructor TBoard.create(AOwner: TComponent);
begin
  inherited create(Aowner);
  left:=64;
  top:=72;
  width:=456;
  height:=334;
  BorderStyle:=bsNone;
  [COLOR=red]FCanvas:=TControlCanvas.Create;
  TControlCanvas(FCanvas).Control:=self;[/color]
  ShowHint:=True;
  SetLength(chains,1);
  SetLength(chains[high(chains)],1);
  chains[0,0]:=0;
  SetLength(axials,1);
  SetLength(axials[high(axials)],1);
  axials[0,0]:=0;
end;

destructor TBoard.destroy;
begin
  FCanvas.free;
  inherited destroy;
end;
//-----------------------------------------------------
constructor TWires.create;
var	i: integer;
begin
  inherited create;
[COLOR=red]  Add(Twire.create);[/color]  //what was originally wire [0]
  items[0].axial:=false;
  items[0].Vend:=0;
  items[0].Vsource:=0;
  for i:=1 to kSegs do
  begin
[COLOR=red]    items[0].wseg[i]:=Twseg.create(Board1);
    items[0].wseg[i].parent:=Board1;[/color]
    items[0].wseg[i].Pen.width:=kPenWire;
    items[0].wseg[i].Pen.color:=clBlack;
    items[0].wseg[i].tag:=0;
  end;
end;
[COLOR=green]//=============Unit Wire==================================[/color]
  Twire=class(TObject)
  private
    FLeft: Integer;
    FTop: Integer;
    FWidth: Integer;
    FAxial: boolean;
[COLOR=blue]    Fwseg: array[1..6] of Twseg;
    Fbody: TBody;[/color]
    procedure SetLeft(value: Integer);
    procedure SetTop(value: Integer);
    procedure SetWidth(value: Integer);
    procedure SetAxial(value: boolean);
    function getwseg(index: integer):Twseg;
    procedure setwseg(index: integer; value:Twseg);
    procedure setbody(value: Tbody);
  public
    Link: Array[1..2] of TLink;
    constructor create;
    constructor CreateFromStream(fs: Tfilestream);
    destructor destroy;override;
    property Left: Integer read FLeft write SetLeft;
    property Top: Integer read FTop write SetTop;
    property Width: Integer read FWidth write SetWidth;
    property Axial: boolean read Faxial write setaxial;
    property wseg[index:integer]:Twseg 
             read getwseg write setwseg;
    property body: Tbody read Fbody write Setbody;
    procedure WriteToStream(fs:TFileStream);
    procedure ReadFromStream(fs:TFileStream);
  end;

implementation

constructor Twire.create;
begin
  inherited create;
  SetLength(Faligned,1);
  Vsource:=0;
  Vend:=0;
end;

Destructor Twire.destroy;
var	i: integer;
begin
  if axial then
  body.free;
  for i:=1 to kSegs do
    wseg[i].free;
  inherited destroy;
end;
[COLOR=green]//===============Unit WSeg===============================[/color]
  Twseg=class(TGraphicControl)
  private
    FBrush: TBrush;
    FPen: TPen;
    FPoly: array[0..1] of TPoint;
    Flength: integer;
    Fmiddle: Tpoint;
    Flimits: array[1..2] of integer;
    FOrientation: char;
    Fns: integer;
    Few: integer;
    Ftag: integer;
    procedure SetPen(value: TPen);
    procedure SetBrush(value: TBrush);
    function GetPoly(Index: Integer): TPoint;
    procedure SetPoly(Index: integer; value: TPoint);
//*    procedure SetLink(value: TPlace);
    procedure SetLimits(index: integer; value: integer);
    function GetLimits(index: integer): integer;
    procedure SetOrientation(value: char);
    procedure Setns(value: integer);
    procedure Setew(value: integer);
  protected
    procedure Paint; override;
    procedure StyleChange(sender: TObject); dynamic;
    procedure MouseDown(Button: TMouseButton;
    	      Shift: TShiftState; X, Y: Integer);override;
    procedure MouseUp(Button: TMouseButton; 
              Shift: TShiftState; X, Y: Integer);override;
    procedure MouseMove(Shift: TShiftState; 
              X, Y: Integer);override;
  public
    constructor Create(AOwner: TComponent); override;
    constructor CreateFromStream(fs: Tfilestream);
    destructor Destroy; override;
    property poly[Index: Integer]: Tpoint 
               read GetPoly write SetPoly;
    property length: integer read Flength;
    property middle: Tpoint read Fmiddle;
    property limits[index: integer]: integer 
               read GetLimits write SetLimits;
    property Orientation: Char 
               read FOrientation write SetOrientation;
    property ns: integer read Fns write Setns;
    property ew: integer read Few write Setew;
    property Brush: TBrush read FBrush write SetBrush;
    property Pen: TPen read FPen write SetPen;
    procedure WriteToStream(fs:TFileStream); //virtual;
    procedure ReadFromStream(fs:TFileStream);// virtual;
   end;

implementation

constructor Twseg.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FPen:=TPen.Create;
  FPen.OnChange:=StyleChange;
  FBrush:=TBrush.Create;
  FBrush.OnChange:=StyleChange;
end;

destructor Twseg.Destroy;
begin
  FPen.Free;
  FBrush.Free;
  inherited Destroy;
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top