Brad
Brad

Reputation: 2237

Convert Delphi 7 code to work with Delphi 2009

I have a String that I needed access to the first character of, so I used stringname[1]. With the unicode support this no longer works. I get an error: [DCC Error] sndkey32.pas(420): E2010 Incompatible types: 'Char' and 'AnsiChar'

Example code:

//vkKeyScan from the windows unit

var
KeyString : String[20];
MKey : Word;

mkey:=vkKeyScan(KeyString[1])

How would I write this in modern versions of Delphi

Upvotes: 1

Views: 2214

Answers (2)

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108963

The type String[20] is a ShortString of length 20, i.e. a ShortString that contains 20 characters. But ShortStrings behave like AnsiStrings, i.e. they are not Unicode - one character is one byte. Thus KeyString[1] is an AnsiChar, whereas the vkKeyScan function expects a WideChar (=Char) as argument. I really have no idea whatsoever why you want to use the type String[20] instead of String (=UnicodeString), but you could convert the AnsiChar KeyString[1] to a WideChar:

mkey := vkKeyScan(WideChar(KeyString[1]))

Upvotes: 4

Muhammad Alkarouri
Muhammad Alkarouri

Reputation: 24662

Off the top of my head: do you really need a string, which is equal to widestring in Delphi 2009?

One option is to have the definition var KeyString: AnsiString;

then when you take KeyString[1] that would be an AnsiChar rather than a Char.

Upvotes: 0

Related Questions