Solved

Completely stuck on a problem with incompatible types!

Posted on 2001-07-25
7
273 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
  • 3
  • 3
7 Comments
 
LVL 9

Accepted Solution

by:
ITugay earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
you're right, igor,
i should read to end of q
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:ITugay
Comment Utility
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
Comment Utility
? there is no result assigned (last function from xanthe)
0
 
LVL 9

Expert Comment

by:ITugay
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now