Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Incompatible types error.



Permlink Replies: 16 - Last Post: Apr 29, 2016 10:00 AM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
Ian Branch

Posts: 442
Registered: 9/23/99
Incompatible types error.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 26, 2016 12:39 AM
Hi Guys,
Trying to bring some D7 code to D10S.
Given the following code...

		{ converts color value to HTML color value (ie: #FFFFFF is white)}
		function ColorToHTML(const Color: TColor): String;
		var
  			HexColor: String[6];
		begin
  			HexColor := DecToHex(ColorToRGB(Color));
  			while Length(HexColor) < 6 do Insert('0', HexColor, 1);
  			Result := HexColor;
  			Result[1] := HexColor[5];
  			Result[2] := HexColor[6];
  			Result[5] := HexColor[1];
  			Result[6] := HexColor[2];
		end;
	


I get a "E2010 Incompatible types: 'Char' and 'AnsiChar'" error at Result[1].
As I can't control/change what 'Result[1]' wants how do I convert HexColor[5]?

Regards & TIA,
Ian
Robert Triest

Posts: 687
Registered: 3/24/05
Re: Incompatible types error.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 26, 2016 1:05 AM   in response to: Ian Branch in response to: Ian Branch
type ColorString=string[6];
 
function ColorToHTML(const Color: TColor): ColorString;
var HexColor: ColorString;
begin
    HexColor := IntToHex(ColorToRGB(Color),6);
    Result := HexColor;
    Result[1] := HexColor[5];
    Result[2] := HexColor[6];
    Result[5] := HexColor[1];
    Result[6] := HexColor[2];
end;


function ColorToHTML(const Color: TColor): String;
var HexColor: String;
begin
    HexColor := IntToHex(ColorToRGB(Color),6);
    Result := HexColor;
    Result[1] := HexColor[5];
    Result[2] := HexColor[6];
    Result[5] := HexColor[1];
    Result[6] := HexColor[2];
end;
Arthur Hoornweg

Posts: 414
Registered: 6/2/98
Re: Incompatible types error.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 26, 2016 1:42 AM   in response to: Ian Branch in response to: Ian Branch
Fixed-length strings are "legacy" strings in Delphi. Basically they're arrays of Ansichar with a pre-pended length byte. Don't use them.

Just use "String" in your code and you're good.
Ian Branch

Posts: 442
Registered: 9/23/99
Re: Incompatible types error.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 26, 2016 1:57 AM   in response to: Ian Branch in response to: Ian Branch
Thanks Guys. Appreciated.

Ian
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Incompatible types error.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 26, 2016 2:51 AM   in response to: Ian Branch in response to: Ian Branch
Ian Branch wrote:

Result[1] := HexColor[5];
Result[2] := HexColor[6];
Result[5] := HexColor[1];
Result[6] := HexColor[2];
end;
 
	I get a "E2010 Incompatible types: 'Char' and 'AnsiChar'" error at
Result[1].</div>
 
 
Do not use string[x]. It is in fact an "array[0..x] of AnsiChar" with
the length stored at index 0. It is a legacy type from old Turbo Pascal.
 

function ColorToHTML(const Color: TColor): string;
var
C: Cardinal;
begin
C := ColorToRGB(Color);
Result := Format('#%.2x%.2x%.2x',
[C and $FF, C shr 8 and $FF, C shr 16 and $FF]);
end;
{code}

--
Rudy Velthuis http://www.rvelthuis.de

"I doubt, therefore I might be." -- Unknown

Ian Branch

Posts: 442
Registered: 9/23/99
Re: Incompatible types error.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 26, 2016 3:17 AM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Rudy Velthuis (TeamB) wrote:

It is a legacy type from old Turbo Pascal.

Tks Rudy. I can believe that.
Corrected.

Regards,
Ian
Quentin Correll


Posts: 2,412
Registered: 12/1/99
Re: Incompatible types error.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 26, 2016 10:52 AM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Rudy,

| Do not use string[x]. It is in fact an "array[0..x] of AnsiChar" with
| the length stored at index 0. It is a legacy type from old Turbo
| Pascal.

Yeah, string[x] is a great type!!! My old code is full of them. <g>

--

Q -- XanaNews 1.19.1.372 - 2016-04-26 10:50:58
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Incompatible types error.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 27, 2016 4:12 AM   in response to: Quentin Correll in response to: Quentin Correll
Quentin Correll wrote:

Rudy,

Do not use string[x]. It is in fact an "array[0..x] of AnsiChar"
with the length stored at index 0. It is a legacy type from old
Turbo Pascal.

Yeah, string[x] is a great type!!! My old code is full of them. <g>

Indeed. OLD code.

Some of my old code is written in BBC Basic. Would I do that now?
Certainly not.

--
Rudy Velthuis http://www.rvelthuis.de

"The cry has been that when war is declared, all opposition
should be hushed. A sentiment more unworthy of a free country
could hardly be propagated." -- William Ellery Channing
Quentin Correll


Posts: 2,412
Registered: 12/1/99
Re: Incompatible types error.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 27, 2016 10:49 AM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Rudy,

| | Yeah, string[x] is a great type!!! My old code is full of them.
| | <g>

| Indeed. OLD code.
|

| Some of my old code is written in BBC Basic. Would I do that now?
| Certainly not.


You did notice my <g> didn't you? ;-)

--

Q -- XanaNews 1.19.1.372 - 2016-04-27 10:48:41
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Incompatible types error.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 27, 2016 7:22 PM   in response to: Quentin Correll in response to: Quentin Correll
Quentin Correll wrote:

Rudy,

| Yeah, string[x] is a great type!!! My old code is full of them.
| | <g>

Indeed. OLD code.

Some of my old code is written in BBC Basic. Would I do that now?
Certainly not.

You did notice my <g> didn't you? ;-)

Oh, I must have forgotten mine.

--
Rudy Velthuis http://www.rvelthuis.de

"If one acknowledges a group or a nation willing to commit
atrocities, then one must also acknowledges the existence of a
nation willing to commit atrocities to blame on the first
nation."
-- Michael Rivero
Quentin Correll


Posts: 2,412
Registered: 12/1/99
Re: Incompatible types error.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 28, 2016 10:29 AM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Rudy,

| | You did notice my <g> didn't you? ;-)
|
| Oh, I must have forgotten mine.

;-)

--

Q -- XanaNews 1.19.1.372 - 2016-04-28 10:29:19
Free Dorfman

Posts: 139
Registered: 2/4/12
Re: Incompatible types error.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 27, 2016 9:28 PM   in response to: Ian Branch in response to: Ian Branch
Ian,

A while ago I jumped from D6 to XE7.

Strings were a major issue. Having gone through it fairly recently, might I suggest:

I'd start by using "default string types" keeping in mind that they're 0-based.

function ColorToHTML(const Color: TColor): String;
var
  HexColor: string;
begin
HexColor := DecToHex(ColorToRGB(Color));
 
while HexColor.Length < 6 do
  HexColor := '0' + HexColor;
 
Result := HexColor.Substring(4,2) + HexColor.Substring(2,2) + HexColor.Substring(0,2);
                         // (5-6)                     (3-4)                     (1-2)
end;


-Free
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Incompatible types error. [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 28, 2016 11:22 AM   in response to: Free Dorfman in response to: Free Dorfman
Free wrote:

I'd start by using "default string types" keeping in mind that they're
0-based.

The native 'string' type is 1-based on desktop platforms, and 0-based on
mobile platforms. You can disable the 0-based behavior via the {$ZEROBASEDSTRINGS}
directive:

http://docwiki.embarcadero.com/RADStudio/en/Zero-based_strings_(Delphi)

The TStringHelper.Substring() method is 0-based on all platforms and cannot
be changed.

The System.Copy() function for strings is 1-based on all platforms and cannot
be changed.

--
Remy Lebeau (TeamB)
Free Dorfman

Posts: 139
Registered: 2/4/12
Re: Incompatible types error. [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 28, 2016 1:08 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
The native 'string' type is 1-based on desktop platforms, and 0-based on
mobile platforms. You can disable the 0-based behavior via the {$ZEROBASEDSTRINGS}
directive:

Remy,

When you say 1-based, do you mean if you're directly indexing the string (e.g. S[1])?

because - VCL app - DCC32:
var
  S1, S2: string;
begin
S1 := 'ABC';
 
S2 := S.Chars[0]; //S2 is now "A"


Or is there something (potentially major) that I am missing in regards to strings deployed using Delphi desktop compilers (DCC32, DCC64, DCCOSX), and those on the mobile compilers?

I am speaking here of the default state:
{$ZEROBASEDSTRINGS OFF}


As part of my jump from D6 to XE7. I have given up on ANY direct string indexing (again, like S[2]) and am using TStringHelper.Chars.

Is it possible that you're talking about cases where you've got string variables that can't use TStringHelper? [I know I saw this in a few places, but cannot - off the top of my head - remember where/why.]

Thanks.
Jeff Overcash (...

Posts: 1,529
Registered: 9/23/99
Re: Incompatible types error. [Edit] [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 28, 2016 1:50 PM   in response to: Free Dorfman in response to: Free Dorfman
Free Dorfman wrote:
The native 'string' type is 1-based on desktop platforms, and 0-based on
mobile platforms. You can disable the 0-based behavior via the {$ZEROBASEDSTRINGS}
directive:

Remy,

When you say 1-based, do you mean if you're directly indexing the string (e.g. S[1])?

Yes, but your example is going through the class helper, which is always 0 based.


because - VCL app - DCC32:
var
  S1, S2: string;
begin
S1 := 'ABC';
 
S2 := S.Chars[0]; //S2 is now "A"


Or is there something (potentially major) that I am missing in regards to strings deployed using Delphi desktop compilers (DCC32, DCC64, DCCOSX), and those on the mobile compilers?

I am speaking here of the default state:
{$ZEROBASEDSTRINGS OFF}


As part of my jump from D6 to XE7. I have given up on ANY direct string indexing (again, like S[2]) and am using TStringHelper.Chars.

That is the way intended for direct access on cross platform code.

Is it possible that you're talking about cases where you've got string variables that can't use TStringHelper? [I know I saw this in a few places, but cannot - off the top of my head - remember where/why.]

Thanks.

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Incompatible types error. [Edit] [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 29, 2016 10:00 AM   in response to: Free Dorfman in response to: Free Dorfman
Free wrote:

When you say 1-based, do you mean if you're directly indexing
the string (e.g. S[1])?

Yes.

S2 := S.Chars[0]; //S2 is now "A"

The Chars[] property is 0-based on all platforms. All of the TStringHelper
functionality is 0-based.

Or is there something (potentially major) that I am missing in regards
to strings deployed using Delphi desktop compilers (DCC32, DCC64,
DCCOSX), and those on the mobile compilers?

The only difference between desktop strings and mobile strings is in direct
indexing. TStringHelper was introduced to mitigate the issue by adding a
consistent 0-based interface on all platforms.

I really wish Embarcadero had added a compiler warning on the '[]' string
operator so people could easily find all of the places in their code where
direct indexing is being used.

As part of my jump from D6 to XE7. I have given up on ANY direct
string indexing (again, like S[2]) and am using TStringHelper.Chars.

Good for you. That is not always an option, though.

--
Remy Lebeau (TeamB)
Free Dorfman

Posts: 139
Registered: 2/4/12
Re: Incompatible types error.
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 27, 2016 11:51 PM   in response to: Ian Branch in response to: Ian Branch
Ian:

The following is probably worth a quick look (again, jumping from D7...)

http://docwiki.embarcadero.com/RADStudio/XE5/en/Migrating_Delphi_Code_to_Mobile_from_Desktop
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02