Method returning a two dimensional array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rocksam2003
    New Member
    • Aug 2013
    • 4

    #1

    Method returning a two dimensional array

    I have the following method:

    Code:
    public string[,] StringConvert_tblVFWPost(DataTable dt1)
                {
                    string[,] stringArray = new string[dt1.Rows.Count, dt1.Columns.Count];
    
                    for(int row = 0; row < dt1.Rows.Count; ++row)
                    {
                        for(int col = 0; col < dt1.Columns.Count; col++)
                        {
                        stringArray[row, col] = dt1.Rows[row][col].ToString();
                        }
                        return stringArray;
                    }
                }
    The error I'm getting is "Cannot implicitly convert type 'string[*,*]' to 'string'". So that tells me what the issue is. However, I'm not sure how to fix it.

    How do I change this method, so that it properly returns my two dimensional array?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You cannot return an array from a function because an array is not a type. It is a collection of a type.

    All you can return from a function is a type or a pointer to a type.

    Read this: http://bytes.com/topic/c/insights/77...rrays-revealed

    Comment

    • rocksam2003
      New Member
      • Aug 2013
      • 4

      #3
      I should stated that this code is in C#. In C#, pointers are only allowed in unsafe mode. I would like to avoid that. Surely, there's a way I can return an array without a pointer in C#?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Return an object with the array as a data member.

        Comment

        Working...