471,089 Members | 1,247 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,089 software developers and data experts.

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(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
Nov 16 '05 #1
3 1963
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" <Al******@discussions.microsoft.com> wrote in message
news:C7**********************************@microsof t.com...
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

Nov 16 '05 #2
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" <Al******@discussions.microsoft.com> wrote in message
news:C7**********************************@microsof t.com...
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

Nov 16 '05 #3
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" <Al******@discussions.microsoft.com> wrote in message
news:C7**********************************@microsof t.com...
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

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by domeceo | last post: by
8 posts views Thread by kalinga1234 | last post: by
11 posts views Thread by John Pass | last post: by
reply views Thread by amazon | last post: by
13 posts views Thread by masso600 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.