Passing an array to a function as a string parameter

G

Guest

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(TheArray.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
 
P

Peter Rilling

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 said:
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(TheArray.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
 
K

Kevin Aubuchon

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.StringBuilder (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 said:
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(TheArray.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
 
S

Stephen Ahn

Another alternative is to use the BinaryFormatter.
eg :.
==
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
...
...
private string ConvertToBase64String(int[,] data)
{
BinaryFormatter formatter = new BinaryFormatter();
using(MemoryStream ms = new MemoryStream())
{
formatter.Serialize(ms, data);

byte[] res = ms.ToArray();
return Convert.ToBase64String(res);
}
}

private int[,] ConvertFromBase64String(string str)
{
byte[] res = Convert.FromBase64String(str);

BinaryFormatter formatter = new BinaryFormatter();

using(MemoryStream ms = new MemoryStream(res))
{
return (int[,])formatter.Deserialize(ms);
}
}
==

HTH,
Stephen


Alex Munk said:
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(TheArray.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top