Two Dimensional Array

C

cql60

Hi all Pro,

I have the class call "Person", and I do declare two dimensional array type
of string inside this class which will keep "First_Name" and "Last_Name" as
the record. When I try to serialize it by using XMLSerializer, I got error
like this:

An unhandled exception of type 'System.InvalidOperationException' occurred
in system.xml.dll
Additional information: There was an error reflecting type
'WindowsApplication1.Array_Serialize'.

Does some Pro out there did encounter this problem before? I did try to
serialize with one dimensional array and it works fined, but not for two
dimensional array. May some Pro help please. Thanks you all in advance and
I'm deeply appreciated for your help. Have a good one...

Kate
 
S

Sankar Nemani

can you send ur code or code snippet? 'coz it looks like u have a type
called Array_Serialize in ur code. What is that for?
 
P

Paul E Collins

cql60 said:
I did try to serialize with one dimensional array and
it works fined, but not for two dimensional array.

Try using an array of arrays instead.

public int[,] iArray = new int[10, 10]; // This won't serialise.
public int[][] iArray = new int[10][]; // This will serialise.

Unfortunately, it's more of a chore to initialise the array elements.

for (int iElement = 0; iElement < 10; iElement++)
{
iArray[iElement] = new int[10];
}

P.
 
C

cql60

Thanks you very much, Paul. I'm appreciated.

I did this and it did not work.
**************************************************

public Array iArray;

public void initialize_Array()

{

iArray = Array.CreateInstance( typeof(Int32), 10, 10 );

for ( int i = iArray.GetLowerBound(0); i <= iArray.GetUpperBound(0); i++ )

{

for ( int j = iArray.GetLowerBound(1); j <= iArray.GetUpperBound(1); j++ )

{

iArray.SetValue( (i*100)+(j*10), i, j );

}

}

}

*********************************************************



Paul E Collins said:
cql60 said:
I did try to serialize with one dimensional array and
it works fined, but not for two dimensional array.

Try using an array of arrays instead.

public int[,] iArray = new int[10, 10]; // This won't serialise.
public int[][] iArray = new int[10][]; // This will serialise.

Unfortunately, it's more of a chore to initialise the array elements.

for (int iElement = 0; iElement < 10; iElement++)
{
iArray[iElement] = new int[10];
}

P.
 
P

Paul Hetherington

I beleive that serialization doesn't work with Multi-Dim Arrays.
Create a class with LastName and FirstName as members and then serialize the
array of that class.
-
Paul
 
C

cql60

Thanks you so much for your input, I'm very appreciated. As Paul E Collins
said, serialize only work for jagged arrays, and I did make it work(
Serialized to XML file ).
 

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