2

I'm using the code from this answer to read a CSV file, and I want to assign the const CSVRow& row value to a form control (e.g. a ListBox). How can I accomplish this?

Here's the code I'd like to change:

void display(const CSVRow& row)
{
    if(!row.size()) return;
    CSVRowCI i = row.begin();
    std::cout << *(i++); 
    for(; i != row.end(); ++i)
        std::cout << ',' << *i;
}

But instead of std::cout << ',' << *i, I want to do:

this->ListBox1->Items->Add(*i);

I tried using *i.ToString(), but it gives me an error:

Cannot convert const std::basic string to System::String

Community
  • 1
  • 1
amrita Jaiswal
  • 121
  • 2
  • 5

2 Answers2

0

I see that the type CSVRowCI is actually a typedef representing an individual std::string element in a std::vector of std::strings.Since you want to use this code in a C++/CLI WinForms application, you'd need to convert it to a System.String first (since that's what the Add() method is expecting). Perhaps you could do something along the lines of:

String^ myString= gcnew String(*i.c_str());
myListBox->Items->Add(myString);

I'm not very familiar with C++/CLI so, there probably are better ways to convert a std::string to System.String.

Bhargav
  • 9,869
  • 1
  • 19
  • 29
  • So how to covert it to System.String? – amrita Jaiswal Mar 02 '12 at 07:16
  • I've described one method to convert from `std::string` to `System.String` right there in the answer. Did you read the answer fully? – Bhargav Mar 02 '12 at 08:21
  • Thanks for reply. But I tried your method but its giving error like: 1. 'c_str' : is not a member of 'std::_Vector_const_iterator<_Ty,_Alloc>'... 2. 'gcnew' may only be used to create an object with managed type.... 3. 'String' : ambiguous symbol 4.'^' : cannot use this indirection on type 'String' – amrita Jaiswal Mar 05 '12 at 04:32
  • @amritaJaiswal : What is contained in the `std::vector`? If the `vector` does not contain `std::string`s then you'll have problems. Please post the exact code that you have which leads to the errors. The answer above is based on a short analysis of [this](http://stackoverflow.com/a/556754/761170) post. So, if your code is different from it, further changes may be required to get it to work correctly. – Bhargav Mar 05 '12 at 04:38
  • @amritaJaiswal : If you're using the code exactly as it is, then I guess the line `typedef std::string String;` may cause some problems as `String` is a CLI type, so try using some other identifier for the `typedef` statement. – Bhargav Mar 05 '12 at 05:58
  • yes it was giving error so I declare as string instead of String. – amrita Jaiswal Mar 05 '12 at 06:41
  • which identifier shall i use? – amrita Jaiswal Mar 05 '12 at 06:42
  • @amritaJaiswal : Please use a non-conflicting name for the identifier like `StrVal`. So the `typedef` statement would look like `typedef std::string StrVal;` and the one following it would be `typedef std::vector CSVRow;`. Hopefully that should solve the problem. – Bhargav Mar 05 '12 at 08:07
  • Thanks.. I have done whatever you told. Now its giving error 'c_str' : is not a member of 'std::_Vector_const_iterator<_Ty,_Alloc>' at this line String^ myString= gcnew String(*i.c_str()); – amrita Jaiswal Mar 05 '12 at 09:57
  • I tried with this String^ myString= gcnew StrVal(*i.c_str()); and for this also its giving error as-1. gcnew may only be used to create an object with managed type...2. 'c_str' : is not a member of 'std::_Vector_const_iterator<_Ty,_Alloc>' – amrita Jaiswal Mar 05 '12 at 09:59
  • Could you please edit the question & post the **exact** code that you're trying to compile? – Bhargav Mar 05 '12 at 10:20
  • @ Bhargav Bhat: Thanks for your time. But I got other source code to read CSV files data and it is working fine. – amrita Jaiswal Mar 06 '12 at 07:05
0

The answer you are implementing appears to be flawed. It is possible to have commas embedded within CSV fields, which the answer doesn't seem to recognize.

Perhaps you know for a fact that your data will never have embedded commas, in which case there shouldn't be an issue. But I have an older version that I've posted online that demonstrates some additional steps that should be taken.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466