none
Trying to split data row string to string[,] RRS feed

  • Question

  • I'm trying to split this row to a multi denominational array. Here is what I have but getting syntax error

    string[,] PotionIngredant = dgvData.Rows[e.RowIndex].Cells["Potion"].ToString().Split(',');

    error:

    Severity Code Description Project File Line Suppression State
    Error CS0029 Cannot implicitly convert type 'string[]' to 'string[*,*]' Potion_Creator C:\Users\mholmes\Documents\Visual Studio 2015\Projects\Potion_Creator\Potion_Creator\Recipe Wizard\Recipe_1_1_1.cs 66 Active

    Wednesday, March 21, 2018 6:13 PM

Answers

  • Hello old_School,

    >>Trying to split data row string to string[,]

    As for convertting one-dimensional array into multi-dimensional, There is no direct way, You should copy stuff into a string[,]. Something like below(Assuming you want it in a single row).

             var array = "1,2,3,4,5,6".Split(',');
    
                string[,] arr = new string[1, array.Length];
                for (int i = 0; i < array.Length; ++i)
                    arr[0, i] = array[i];

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by old_School Wednesday, June 6, 2018 5:50 PM
    Thursday, March 22, 2018 2:55 AM
    Moderator

All replies

  • The Split method only creates a one-dimensional array. You need to convert the one-dimensional array into whatever you need.



    Sam Hobbs
    SimpleSamples.Info


    • Edited by Simple Samples Wednesday, March 21, 2018 9:25 PM
    • Proposed as answer by Petr B Wednesday, March 21, 2018 9:42 PM
    Wednesday, March 21, 2018 9:24 PM
  • Hello old_School,

    >>Trying to split data row string to string[,]

    As for convertting one-dimensional array into multi-dimensional, There is no direct way, You should copy stuff into a string[,]. Something like below(Assuming you want it in a single row).

             var array = "1,2,3,4,5,6".Split(',');
    
                string[,] arr = new string[1, array.Length];
                for (int i = 0; i < array.Length; ++i)
                    arr[0, i] = array[i];

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by old_School Wednesday, June 6, 2018 5:50 PM
    Thursday, March 22, 2018 2:55 AM
    Moderator
  • I am waiting for details of what you need. I hope no news is good news and you have it solved.


    Sam Hobbs
    SimpleSamples.Info

    Thursday, March 22, 2018 4:22 AM