Board index » cppbuilder » Using AnsiString and it's methods

Using AnsiString and it's methods

We use borland builder 5 a lot to make test tools for embedded hardware
designs. currently i'm making small programm to write .bin files in a
flash-ic in one of our products. Mostly these programs are only used as
development tool, debugging and monitor tool when we develop embedded hard
and software.

In our case, we use a lot of TMemos and File acces routines to visualise
debug strings and log them, and therefor AnsiString is a common object to
use.

For displaying text for instance in a TMemo object, it is handy to format
debug info to a readable string, such as "WriteFlashState = 10" , so printf
fucntions are quite handy here. At this moment i format a string like this:

char TempString[10];

sprintf(TempString, "%d %%", cProgress);
CompleteLabel->Caption = TempString;

so this is a 2-step solution, first i format a value into a readable string,
and then copy the string in a TLabel (which is a AnsiString).

I have searched the borland help, to make it a 1-step solution, and came up
with the following:

CompleteLabel->Caption.sprintf("%d %%", cProgress);

According to the help, the code above should work properly, but..... it
doesnt do anyting, the compiler accepts it, but it's like the code is not
there al all

Since builder is loaded with AnsiString components, and i think AnsiString
is a verry powerful and usefull class, i always had the feeling i'm not
using it optimal enough, expecially cause TMemo, TEdit and all such objects
use AnsiString too.

Does anyone knows a good book, or resource that handles these special
Builder (and prob Delphi too???) objects. If so, i would like to know them

Thanks in advance, Johan Schuld (NL)

 

Re:Using AnsiString and it's methods


Johan,

You could try using AnsiString::printf() instead. That usually works
for me with AnsiStrings.

OTOH you should note that Caption() is not an AnsiString..it's a
property whose read and write methods take/return AnsiStrings.

Although it should perhaps /act/ like an AnsiString the truth is that
in some cases it doesn't. the '+=' operator is one example where it
simply doesn't work.

My advise would be that if ::printf() doesn't work you should use a
temporary AnsiString. It may be that you have discovered another case
where AnsiString properties don't work.

Andrue Cope
[Bicester, UK]

Re:Using AnsiString and it's methods


Quote
Johan Schuld wrote:

> char TempString[10];

> sprintf(TempString, "%d %%", cProgress);
> CompleteLabel->Caption = TempString;

What about this:
CompleteLabel->Caption = AnsiString().sprintf("%d %%", cProgress);

Ivan Johansen

Other Threads