0

I have a problem, I am trying to convert from C# to C++/CLI and I don't know how to return a string array in a method. The problem is that strings are showing up as numbers instead of strings.

Here is the Method:

static array<String^> ^Split(String^ Victim, char SplitPoint)
{
int Index=0;
        for each(char Char in Victim)
            if(Char==SplitPoint)
                Index++;
        array<String^> ^SplitStrings = gcnew array<String^>;
        Index=0;
        for each(char Char in Victim)
        {
            if(Char==SplitPoint)
                Index++;
            else
                SplitStrings[Index]=SplitStrings[Index]+Char;
        }
        return SplitStrings;
    };

and the original method in C# looks like this:

    public static string[] Split(string Victim, char SplitPoint)
    {
        int Index = 0;
        foreach (char Char in Victim)
            if (Char == SplitPoint)
                Index++;
        string[] SplitStrings = new string[Index + 1];
        Index = 0;
        foreach (char Char in Victim)
        {
            if (Char == SplitPoint)
            {
                Index++;
            }
            else
                SplitStrings[Index] = SplitStrings[Index] + Char;
        }
        return SplitStrings;
    }
4
  • Are you trying to return a managed array of managed strings, or are you trying to return a native array of native strings? Commented Jul 28, 2011 at 1:40
  • 2
    A little off-topic remark: using Char as a variable identifier makes your code really hard to read. Commented Jul 28, 2011 at 2:22
  • why are you not using System::String::Split for this? Commented Jul 29, 2011 at 17:53
  • Oh, ya. I name variables really weird. And I think the System::String::Split Doesn't really work for this, but this took like two minutes in C# so it didn't really matter. Commented Jul 31, 2011 at 1:42

1 Answer 1

2

It's really unclear what your actual question is, so here's a direct translation of your C# code to C++/CLI:

public:
    static array<String^>^ Split(String^ Victim, wchar_t SplitPoint)
    {
        int Index = 0;
        for each (wchar_t Char in Victim)
            if (Char == SplitPoint)
                Index++;
        array<String^>^ SplitStrings = gcnew array<String^>(Index + 1);
        Index = 0;
        for each (wchar_t Char in Victim)
        {
            if (Char == SplitPoint)
                Index++;
            else
                SplitStrings[Index] = SplitStrings[Index] + Char;
        }
        return SplitStrings;
    }

In particular, note that char in C++/CLI equates to System::SByte, not System::Char -- the native name for the latter is wchar_t. (Also, you didn't specify the dimensions of SplitStrings.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.