Solved

Convert TString to AnsiString

Posted on 1998-05-20
5
3,689 Views
Last Modified: 2012-06-27
     Hi.

I have a question about Borland C++.
I have Delphi too, and there I can do this

Edit1.Text := ListBox.Items[3];

but when I do the same thing in C++

Edit1.Text = ListBox.Items[3];

I get the error "Can't convert from TString to AnsiString".
I have tried to cast like this

Edit1.Text = (AnsiString*) ListBox.Items[3];

but it doesn't help.

How should I do?
/Pelle
0
Comment
Question by:PerBoman
5 Comments
 
LVL 3

Expert Comment

by:Norbert
ID: 1164374
Hi,
I have no Borland C++ but I guess that VC++ class CString
is very similar to Borlands TString
CString has a cast operator called LPCTSTR that returns a const char* for non unicode and const WCHAR* for unicode so have a look to the operators TString offers to you I bet there is one that you can use

0
 
LVL 11

Expert Comment

by:alexo
ID: 1164375
Try either:
    Edit1.Text = (char*) ListBox.Items[3];
or
    Edit1.Text = ListBox.Items[3].Relinquish();

Tell me if it worked.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1164376
It is hard to say without knowing the type of either ListBox or Edit1.  But you might want to try

   Edit1.Text = (AnsiString) ListBox.Items[3];

The only operator = supported by AnsiString is from another AnsiString, so the right side must be an AnsiString or you canit use operator =.
0
 
LVL 1

Accepted Solution

by:
bsimser earned 200 total points
ID: 1164377
The ListBox->Items[3] variable is a TStrings type, not a TString. It's a collection of TString objects. In the TStrings class there's a variable called Strings which is an array of TString types. These are the AnsiString types. What you want to do is this:

Edit1->Text = ListBox->Items->Strings[3];

Enjoy.

-Bil
0
 

Author Comment

by:PerBoman
ID: 1164378
Ooh, that's easy!
Thanks.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 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

20 Experts available now in Live!

Get 1:1 Help Now