Passing an array to a function as a string parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Munk

    Passing an array to a function as a string parameter

    I would like to be able to pass a 2-dimensional array to a function as a string parameter. In the called function I would like to be able to convert the string back into a 2-dimensional array. Example

    Function1 {
    int [,] TheArray[10,10];
    Function2(TheAr ray.ToString();
    }
    Function2 (string parm) {
    .... In here I would like to be able to convert parm back into a 2-dimensional int array. How do I do this
    }

    Appreciate your help,
    Alex
  • Peter Rilling

    #2
    Re: Passing an array to a function as a string parameter

    You could use the String.Join and String.Split. Although you probably have
    your reason for doing this, keep in mind that this will not be as efficient
    as just passing the reference to the array.

    "Alex Munk" <AlexMunk@discu ssions.microsof t.com> wrote in message
    news:C76FB00F-50C3-4721-BECE-C61BEE0574D8@mi crosoft.com...[color=blue]
    > I would like to be able to pass a 2-dimensional array to a function as a[/color]
    string parameter. In the called function I would like to be able to convert
    the string back into a 2-dimensional array. Example[color=blue]
    >
    > Function1 {
    > int [,] TheArray[10,10];
    > Function2(TheAr ray.ToString();
    > }
    > Function2 (string parm) {
    > ... In here I would like to be able to convert parm back into a[/color]
    2-dimensional int array. How do I do this[color=blue]
    > }
    >
    > Appreciate your help,
    > Alex[/color]


    Comment

    • Kevin Aubuchon

      #3
      Re: Passing an array to a function as a string parameter

      Yes, you can do it by coding it yourself. Is there a reason you just don't
      pass the array? Conforming to an existing interface?

      good luck,
      kevin aubuchon

      int [,] ar = new int[2,2];
      int [,] az = new int[2,2];

      ar[0,0] = 1;
      ar[0,1] = 2;
      ar[1,0] = 3;
      ar[1,1] = 4;

      StringBuilder sb = new System.Text.Str ingBuilder (100);
      for (int i = 0; i < 2;i++)
      {
      for (int j=0; j < 2;j++)
      {
      sb.Append (ar[i,j].ToString("##") + ";");
      }
      }
      string s = sb.ToString ();
      char [] cs = {char.Parse (";")};

      string [] lines = s.Split (cs);

      for (int i = 0; i < 2;i++)
      {
      for (int j=0; j < 2;j++)
      {
      az[i,j] = Int32.Parse (lines[i * 2 + j]);
      }
      }


      "Alex Munk" <AlexMunk@discu ssions.microsof t.com> wrote in message
      news:C76FB00F-50C3-4721-BECE-C61BEE0574D8@mi crosoft.com...[color=blue]
      > I would like to be able to pass a 2-dimensional array to a function as a[/color]
      string parameter. In the called function I would like to be able to convert
      the string back into a 2-dimensional array. Example[color=blue]
      >
      > Function1 {
      > int [,] TheArray[10,10];
      > Function2(TheAr ray.ToString();
      > }
      > Function2 (string parm) {
      > ... In here I would like to be able to convert parm back into a[/color]
      2-dimensional int array. How do I do this[color=blue]
      > }
      >
      > Appreciate your help,
      > Alex[/color]


      Comment

      • Stephen Ahn

        #4
        Re: Passing an array to a function as a string parameter

        Another alternative is to use the BinaryFormatter .
        eg :.
        ==
        using System.IO;
        using System.Runtime. Serialization.F ormatters.Binar y;
        ...
        ...
        private string ConvertToBase64 String(int[,] data)
        {
        BinaryFormatter formatter = new BinaryFormatter ();
        using(MemoryStr eam ms = new MemoryStream())
        {
        formatter.Seria lize(ms, data);

        byte[] res = ms.ToArray();
        return Convert.ToBase6 4String(res);
        }
        }

        private int[,] ConvertFromBase 64String(string str)
        {
        byte[] res = Convert.FromBas e64String(str);

        BinaryFormatter formatter = new BinaryFormatter ();

        using(MemoryStr eam ms = new MemoryStream(re s))
        {
        return (int[,])formatter.Dese rialize(ms);
        }
        }
        ==

        HTH,
        Stephen


        "Alex Munk" <AlexMunk@discu ssions.microsof t.com> wrote in message
        news:C76FB00F-50C3-4721-BECE-C61BEE0574D8@mi crosoft.com...[color=blue]
        > I would like to be able to pass a 2-dimensional array to a function as a[/color]
        string parameter. In the called function I would like to be able to convert
        the string back into a 2-dimensional array. Example[color=blue]
        >
        > Function1 {
        > int [,] TheArray[10,10];
        > Function2(TheAr ray.ToString();
        > }
        > Function2 (string parm) {
        > ... In here I would like to be able to convert parm back into a[/color]
        2-dimensional int array. How do I do this[color=blue]
        > }
        >
        > Appreciate your help,
        > Alex[/color]


        Comment

        Working...