multidimensional arrays and tostring

  • Thread starter Thread starter Nick Valeontis
  • Start date Start date
N

Nick Valeontis

Here is a snippet:


------------------------------
double[,] a = new Double[2,2];
a[0, 0] = 1;
a[0, 1] = 2;
a[1, 0] = 3;
a[1, 1] = 4;
MessageBox.Show(a.ToString());

result: System.Double[,]
------------------------------


Is there a way to customize ToString's returning value so that i can get
something like "{ 1 2},{3, 4}" - without having to create a new class;




With regards,

Nick
 
Hi Nick,

Short of formatting the string yourself you are stuck with creating your
own class, which would format the string for you.

The string formatting part would look something like this

double[,] a = new Double[2, 2];
a[0, 0] = 1;
a[0, 1] = 2;
a[1, 0] = 3;
a[1, 1] = 4;

string s = "";

for (int i = 0; i <= a.GetUpperBound(0); i++)
{
s += "{";

for (int o = 0; o <= a.GetUpperBound(1); o++)
{
s += a[i, o].ToString() + ", ";
}

if (s.EndsWith(", "))
s = s.Substring(0, s.Length - 2);

s += "},";
}

if (s.EndsWith(","))
s = s.Substring(0, s.Length - 1);

MessageBox.Show(s);


Here is a snippet:


------------------------------
double[,] a = new Double[2,2];
a[0, 0] = 1;
a[0, 1] = 2;
a[1, 0] = 3;
a[1, 1] = 4;
MessageBox.Show(a.ToString());

result: System.Double[,]
------------------------------


Is there a way to customize ToString's returning value so that i can get
something like "{ 1 2},{3, 4}" - without having to create a new class;




With regards,

Nick
 

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

Back
Top