Link to home
Start Free TrialLog in
Avatar of Chipmunk77
Chipmunk77

asked on

Convert TWideString to TString

Hi all,

I am trying to take the field column name data from an ADO Query and put the column names into a listbox.  Prior to the latest release of Delphi this was easy, but now they save the field names as TWideString and everything I have tried I either get index out of bounds or incompatible types TWideString and TStrings.  Does anyone know how to copy the data from the query to a listbox easily??

Here are some of the things I have tried:
1.    ListBoxSelect.Items.AddStrings(FDB.ADOQuerySalesman.FieldList);    - incompatible - TStringList and TFieldList

2.    FDB.ADOQuerySalesman.GetFieldNames(tempList);     - deprecated

3.    for I := 1 to FDB.ADOQuerySalesman.FieldCount do
       begin
           ListBoxSelect.Items.AddStrings(FDB.ADOQuerySalesman.FieldList.Strings[i]);    - incompatible - TStrings and TWideString
       end;

4.    for i:=1 to FDB.ADOQuerySalesman.FieldCount do
       begin
           ListBoxSelect.Items.Add(FDB.ADOQuerySalesman.FieldList.Strings[i]); - index out of bounds
       end;

I need an answer soon, I have a project due date coming up and have more work to do once I figure this out!  

Thanks for any help
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Have you tried casting as string? If its a WideString, the that should work correctly.

Regards,
Russell

----

for I := 1 to FDB.ADOQuerySalesman.FieldCount do
       begin
           ListBoxSelect.Items.AddStrings(String(OleFDB.ADOQuerySalesman.FieldList.Strings[i]));
       end;

Hi,

Did you try to typecast explicitly your strings ?  

sth like ListBoxSelect.Items.Add(Strings(FDB.ADOQuerySalesman.FieldList.Strings[i])) or  ListBoxSelect.Items.Add(AnsiStrings(FDB.ADOQuerySalesman.FieldList.Strings[i]));

Not at my Delphi station for the moment, can't verify. I've done sth like this in the past.







By the way, you should also be enumerating from zero.

eg:

for i:=0 to  FDB.ADOQuerySalesman.FieldCount-1 do

---

Russell


Sorry, rllibby we answer at the same moment ...


No problem, happens to all of us from time to time :-)

Russell
Avatar of TName
TName

Hi, adding a WideString to a listbox works for me without typecast...

var
  ws: WideString;
begin
  ws := 'SomeWideString';
  ListBox1.Items.Add(ws);
Ok, sorry,  StringS and WideStringS!  :/
Avatar of Chipmunk77

ASKER

okay, I was able to cast it as a TStrings, but then I get the error List Index Out of Bounds.  I tried with both a 0 to count - 1 and with 1 to count.  I'm not sure what index is out of bounds?  The list box should be adding items, so it's index should not be out of bounds - and the ado query appears in the grid, so the fields should be populated in the TWideStrings

Any ideas?  Thanks for the quick answers
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi

Did you try the opposite to see if it gives the same error:

for I := FDB.ADOQuerySalesman.FieldCount -1 downto 0 do
begin
// Check the value of your FieldCount to be sure it contains a valid and correct value
// Check if the result of each Strings[i] to see if it really contains the correct string
// use Debug.Print to visually see the results
// if OK try the following line
ListBoxSelect.Items.AddStrings(String(OleFDB.ADOQuerySalesman.FieldList.Strings[i]));
end;


Are you sure it was a conversion issue after all?
Because you've tried
  ListBoxSelect.Items.Add(FDB.ADOQuerySalesman.FieldList.Strings[i]);
in the beginning, and ...FieldList.Strings[i] should give you a single WideString at a time (for each i), and the listbox should accept it with or without cast....

Are you sure it's not something else? What happens if you uncomment the line in the loop or the whole loop?
(The Fieldlist.Count that Russel suggested could be the best next move...)
Thank you, thank you, thank you - the FieldList.Count, instead of FieldCount was all it took.  It is working correctly now!  Th