Two Dimensional Array

  • Thread starter Thread starter cql60
  • Start date Start date
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
 
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?
 
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.
 
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.
 
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
 
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 ).
 
Back
Top