Solved

Completely stuck on a problem with incompatible types!

Posted on 2001-07-25
7
286 Views
Last Modified: 2010-04-06
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
Comment
Question by:xanthe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 9

Accepted Solution

by:
ITugay earned 100 total points
ID: 6317038
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6317051
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6317055
you're right, igor,
i should read to end of q
0
Technology Partners: 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!

 
LVL 9

Expert Comment

by:ITugay
ID: 6317073
meikl :-)
may be, may be....

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

------
Igor.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6317093
? there is no result assigned (last function from xanthe)
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6317105
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
 

Author Comment

by:xanthe
ID: 6317167
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question