Solved

Completely stuck on a problem with incompatible types!

Posted on 2001-07-25
7
289 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

728 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