[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

Completely stuck on a problem with incompatible types!

I wrote a component for a line which could go in any direction (diagonally, horizontally, vertically) and that could have an arrow head at any end.  I am having a problem when I try to create a line dynamically at runtime from the text in a file.  The text specifying the properties of the line is held in a string array....but Delphi is unhappy with this as it says that my string is not the required type to set properties such as the arrow type and style of line.  Fair enough.  So, now I have tried to put a function into the code for the component to allow it to translate the string into the required type....only now I'm getting incompatible type errors.  I'm just completely stuck!
I've attached the code I have below and if anyone has any ideas I'd really like to hear from them!
Thanks,
Xanthe


unit XantheAllFunctionLine;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TXantheAFLineStyles = (lsHorizontal,lsVertical,lsDiagonalUp,lsDiagonalDown);
  TXantheAFLineStyle  = set of TXantheAFLineStyles;
  TXantheArrowTypes = (atHorizontalLeft,atHorizontalRight,atVerticalTop,atVerticalBottom,atDiagonalUpTop,atDiagonalUpBottom,atDiagonalDownTop,atDiagonalDownBottom,atNone);
  TXantheArrowType = set of TXantheArrowTypes;

  TXantheAFLine = class(TGraphicControl)
    private
      FStyle : TXantheAFLineStyle;
      FArrow : TXantheArrowType;
      FPen : TPen;
      FColour : TColor;
      procedure SetStyle(NewStyle:TXantheAFLineStyle);
      procedure SetArrowType(NewStyle:TXantheArrowType);
      procedure SetColour(V:TColor);
    protected
      procedure Paint; override;
    public
      constructor Create(AOwner:TComponent); override;
      destructor Free;
    published
      procedure PenChange(Sender:TObject);
      property Height;
      property Left;
      property Pen:TPen read FPen write FPen;
      property ShowHint;
      property Style:TXantheAFLineStyle read FStyle write SetStyle default [lsHorizontal];
      property Arrow:TXantheArrowType read FArrow write SetArrowType default [atNone];
      property Colour:TColor read fColour write SetColour default clBlack;
      property Top;
      property Width;
      function ChangeArrow(S:string):TXantheArrowType;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Xanthe',[TXantheAFLine]);
end;

constructor TXantheAFLine.Create(AOwner:TComponent);
begin
  inherited Create(AOwner);
  FPen := TPen.Create;
  FPen.OnChange := PenChange;
  Height := 40;
  Width := Height;
  Style := [lsHorizontal];
  Arrow := [atNone];
  Colour := clBlack;
end;

destructor TXantheAFLine.Free;
begin
  FPen.Free;
  inherited Free;
end;

procedure TXantheAFLine.Paint;
var
  X : Integer;
  Y : Integer;
  W,W1 : Integer;
  H,H1 : Integer;
begin
  Canvas.Pen := Pen;
  Canvas.Pen.Color:=FColour;
  X := Pen.Width div 2;
  Y := X;
  W := Width - X;
  W1:= W div 2;
  H := Height - Y;
  H1:= H div 2;
  if X<Pen.Width/2 then
    begin
      Dec(W);
      Dec(H);
    end;
  if lsHorizontal in FStyle then
    begin
      Canvas.PenPos := Point(X,H1);
      Canvas.LineTo(W,H1);
      if atHorizontalLeft in FArrow then
         begin
              Canvas.PenPos := Point(X,H1);
              Canvas.LineTo(W1 div 2,Y + (H1 div 2));
              Canvas.PenPos := Point(X,H1);
              Canvas.LineTo(W1 div 2,H - (H1 div 2));
         end;
      if atHorizontalRight in FArrow then
         begin
              Canvas.PenPos := Point(W,H1);
              Canvas.LineTo(W - W1 div 2,Y + (H1 div 2));
              Canvas.PenPos := Point(W,H1);
              Canvas.LineTo(W - W1 div 2,H - (H1 div 2));
         end;
    end;
  if lsVertical in FStyle then
    begin
      Canvas.PenPos := Point(W1,Y);
      Canvas.LineTo(W1,H);
      if atVerticalTop in FArrow then
         begin
              Canvas.PenPos := Point(W1,Y);
              Canvas.LineTo(W1 div 2,Y + (H1 div 2));
              Canvas.PenPos := Point(W1,Y);
              Canvas.LineTo(W1 + W1 div 2,Y + (H1 div 2));
         end;
      if atVerticalBottom in FArrow then
         begin
              Canvas.PenPos := Point(W1,H);
              Canvas.LineTo(W - W1 div 2,H - (H1 div 2));
              Canvas.PenPos := Point(W1,H);
              Canvas.LineTo(W1 - W1 div 2,H - (H1 div 2));
         end;
    end;
  if lsDiagonalUp in FStyle then
    begin
      Canvas.PenPos := Point(X,H);
      Canvas.LineTo(W,Y);
      if atDiagonalUpTop in FArrow then
         begin
              Canvas.PenPos := Point(W,Y);
              Canvas.LineTo(W - W1 div 2,Y);
              Canvas.PenPos := Point(W,Y);
              Canvas.LineTo(W,Y + (H1 div 2));
         end;
      if atDiagonalUpBottom in FArrow then
         begin
              Canvas.PenPos := Point(X,H);
              Canvas.LineTo(X,H - (H1 div 2));
              Canvas.PenPos := Point(X,H);
              Canvas.LineTo(W1 div 2,H);
         end;
    end;
  if lsDiagonalDown in FStyle then
    begin
       Canvas.PenPos := Point(X,Y);
       Canvas.LineTo(W,H);
      if atDiagonalDownTop in FArrow then
         begin
              Canvas.PenPos := Point(X,Y);
              Canvas.LineTo(X + W1 div 2,Y);
              Canvas.PenPos := Point(X,Y);
              Canvas.LineTo(X,H1 - (H1 div 2));
         end;
      if atDiagonalDownBottom in FArrow then
         begin
              Canvas.PenPos := Point(W,H);
              Canvas.LineTo(W - W1 div 2,H);
              Canvas.PenPos := Point(W,H);
              Canvas.LineTo(W,H1 + (H1 div 2));
         end;
    end;
end;

procedure TXantheAFLine.PenChange(Sender:TObject);
begin
  Invalidate;
end;

procedure TXantheAFLine.SetStyle(NewStyle:TXantheAFLineStyle);
begin
  if NewStyle<>FStyle then
    begin
      FStyle := NewStyle;
      Invalidate;
    end;
end;

procedure TXantheAFLine.SetArrowType(NewStyle:TXantheArrowType);
begin
  if NewStyle<>FArrow then
    begin
      FArrow := NewStyle;
      Invalidate;
    end;
end;

procedure TXantheAFLine.SetColour(V:TColor);
begin
  if V<>FColour then
    begin
      FColour := V;
      Invalidate;
    end;
end;

function TXantheAFLine.ChangeArrow(S:string):TXantheArrowType;
begin
     if (S='bsNone') then
        ChangeArrow:=atNone;
end;

(this last function doesn't work - gives the incompatible types error)
0
xanthe
Asked:
xanthe
  • 3
  • 3
1 Solution
 
ITugayCommented:
Hi xanthe@lc,

you use set of .... so you need to use "[...]", it would be something like this:

function TXantheAFLine.ChangeArrow(S:string):TXantheArrowType;
begin
     if (S='bsNone') then
        ChangeArrow:=[atNone];
end;
------
Igor.
0
 
kretzschmarCommented:
well, i guess you need a translation of the names of your Type enumerations

like
TXantheAFLineStyles = (lsHorizontal,lsVertical,lsDiagonalUp,lsDiagonalDown);
 
Const
TXantheAFLineStylesName : Array[lsHorizontal..lsDiagonalDown] of string := ('lsHorizontal','lsVertical','lsDiagonalUp','lsDiagonalDown');

and a method which does translate this

like
Function TXantheAFLine.StyleNameToStyle(AName : String) : TXantheAFLineStyles;
var i : Integer;
begin
  i := low(TXantheAFLineStylesName);
  while (I <= High(TXantheAFLineStylesName) and (TXantheAFLineStylesName[i] <> AString) do inc(i);
  if i <= High(TXantheAFLineStylesName) then
    Result := TXantheAFLineStyles(Ord(i))
  else
    raise exception.create('Invalid name');
end;

not sure about this line
    Result := TXantheAFLineStyles(Ord(i))

just from head
maybe there is a better way,
because the ide can enumerate the names

meikl ;-)
0
 
kretzschmarCommented:
you're right, igor,
i should read to end of q
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ITugayCommented:
meikl :-)
may be, may be....

another thing that result of function should be
TXantheArrowTypes
   instead of
TXantheArrowType

------
Igor.
0
 
kretzschmarCommented:
? there is no result assigned (last function from xanthe)
0
 
ITugayCommented:
Hi xanthe@lc,

I mean.ChangeArrow function.

function TXantheAFLine.ChangeArrow(S:string):TXantheArrowType[s?];

May be I miss something, but result is assigned.

     if (S='bsNone') then
        ChangeArrow := atNone; // old style result assignment

-----
Igor.
 
0
 
xantheAuthor Commented:
Igor,
Fantastic..yet again!  I think that is the one thing that I didn't try!  Thanks also to meikl who replied here.  Incidentally, is it really 5 o'clock in the morning when you are writing this? - no wonder you know so much if you're that keen!
Thanks again,
Xanthe
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now