Your code is a wild mixture of native C++ and C++/CLI. While this is possible, it is not recommended unless definitely necessary, and I don't see the necessity for that in your code.
The .NET System::String class offers a wealth of useful methods you can use here. I managed to accomplish your task (including display of the message box) in just four lines of pure C++/CLI code using the System::String methods Split() and Join().
Hint: For the Split() call you'll need a String delimiter array containing only "|$|" as its sole element. This is tricky (if even possible at all) to write inline in the function call. Therefore I defined it as a variable, constructed using this line:
Code:
array<String ^> ^astrDelim = {"|$|"};
BTW, you don't need your own function to convert native strings to System::String objects. The String class has two constructors (one for Unicode strings and one for ANSI strings) that do that for you.
BTW2, it would have been helpful to actually describe or show the "weird output" you got. That way I probably wouldn't have been forced to follow the logic of your code (wich is somewhat convoluted) or compile your code (which wouldn't have been easy because it's no complete compilable example). Actually I did non of those; instead I solved the problem from scratch using pure C++/CLI.
Last edited by Eri523; January 19th, 2011 at 04:55 PM.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
And that was soooooo simple (~_~) thanks, that worked perfectly (^-^)b
After getting array from line, I wanted to get rid of 0 dimension (or is it element??? well, first part of after Split()).
I tried to use Join(String, String[], Int32, Int32), but I couldn't get index of last dimension in array (I tried to use Rank, but that probably was wrong choice =D) so I just used.
Code:
result[0]=""; // it's better for later if it doesn't have value
String ^ result2 = String::Join("\n", result);
result2 = result2->Remove(0, 1); // to get rid of \n after Join()
but I don't feel right doing it this way xD Update: seem I will need only first line and others can be excluded from code
as for "weird output", it would be hard to explain xD
if I have line
asd||$|c:/asd/qwe/zxc|$|
output would be
c:/asd/qwe/zxc|$|
if
asd||$|c:/asd/qwe/zxc|$|c:/asd/qwe/zxc|$|
output
c:/asd/qwe/zxc|$|c:/a
c:/asd/qwe/zxc|$|
if
asd||$|c:/asd/qwe/zxc|$|c:/asd/qwe/zxc|$|c:/asd/qwe/zxc|$|
output
c:/asd/qwe/zxc|$|c:/a
c:/asd/qwe/zxc|$|c:/asd/qwe/zxc (I don't remember how exactly this line looked like, but it was longest =D)
c:/asd/qwe/zxc|$|
But well, I don't need fix for that "wild mixture" anymore. Your way of approach is much better
Last edited by Migeria; January 20th, 2011 at 02:17 PM.
After getting array from line, I wanted to get rid of 0 dimension (or is it element??? well, first part of after Split()).
I tried to use Join(String, String[], Int32, Int32), but I couldn't get index of last dimension in array (I tried to use Rank, but that probably was wrong choice =D) so I just used.
Well, you had the right idea, but the Rank property was indeed the wrong choice. The array class has a Length property that can be used here.
I solved it like this, using the Join() overload that you suggested and the array's length:
That way I didn't need to blank out the first element of the split array and remove the bogus '\n' at the beginning of the string resulting from the Join().
as for "weird output", it would be hard to explain xD
[...]
Oh, that looks really chaotic. Luckily we don't need to find out anymore how your code could produce that...
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
ah.. I thought Array::Length returns how many symbols there are in whole array xD so I excluded it from possible variants.
Seems I mixed up what is element and what's dimension (~_~')
Bookmarks