What is wrong ????

M

Macin

Hi, what is wrong with this method? When I run program I get the error:
"Specified cast is not valid." for the statement: temp =
(double[,])lista[n]. The list lista[n] is a list of 2 dimensional arrays of
a size 8x8. Thanks for help


public double[,] wyswietlMacierzWilekosciObrazka(ArrayList lista)
{
double[,] duza = new double[wys,szer];
int n=0;
for (int k=0;k<wys;k+=8)
{
for (int l=0;l<szer;l+=8)
{
double[,] temp = new double[8,8]; temp = (double[,])lista[n];
n+=1;
for (int i=0;i<8;i++)
{
for (int j=0;j<8;j++)
{
duza[i+k,l+j]=temp[i,j];
}
}
}
}
return duza;
}
 
J

Jon Skeet [C# MVP]

Macin said:
Hi, what is wrong with this method? When I run program I get the error:
"Specified cast is not valid." for the statement: temp =
(double[,])lista[n]. The list lista[n] is a list of 2 dimensional arrays of
a size 8x8. Thanks for help

Is it really a list of two dimensional arrays, or is it a list of
arrays of arrays of doubles (double[][] rather than double[,])? Without
seeing how lista is built, it's impossible to really say what's wrong.
 
M

Macin

I think I found a problem. Teh list sometimes contains arrays of type
double, and sometimes a list of type byte. But still don`t know how to solve
it?
 
J

Jon Skeet [C# MVP]

Macin said:
I think I found a problem. Teh list sometimes contains arrays of type
double, and sometimes a list of type byte. But still don`t know how to solve
it?

Well, you can easily test for it:

object item = lista[a];

double[,] doubles = item as double[,];
if (doubles != null)
{
...
}

and likewise for a list of bytes. It's a pretty nasty API to give you
that kind of surprise though, IMO.
 
M

Macin

But actualy I need to create a function that will be able to manage lists of
arrays of all type... Above code doesn`t help me with this. The method
should return 2 dimensional array (type double) consisting of small 2
dimensional arrays that are stored in an ArrayList. I wish it would work
with every type of 2d arrays. Check the code once again

Thank you for your help

and here is a visualization of a problem:

IN RETURN

List of 2d 2d array
arrays

111
111
111

222
222
222
=>> 111222
333 111222
333 111222
333 333444
333444
444 333444
444
444


Uzytkownik "Jon Skeet said:
Macin said:
I think I found a problem. Teh list sometimes contains arrays of type
double, and sometimes a list of type byte. But still don`t know how to
solve
it?

Well, you can easily test for it:

object item = lista[a];

double[,] doubles = item as double[,];
if (doubles != null)
{
...
}

and likewise for a list of bytes. It's a pretty nasty API to give you
that kind of surprise though, IMO.
 
J

Jon Skeet [C# MVP]

Macin said:
But actualy I need to create a function that will be able to manage lists of
arrays of all type... Above code doesn`t help me with this. The method
should return 2 dimensional array (type double) consisting of small 2
dimensional arrays that are stored in an ArrayList. I wish it would work
with every type of 2d arrays. Check the code once again

It's not clear what you're wanting to change - the thing that's
producing the list, or the method you've already shown which is
*consuming* that list.
 
M

Macin

I have to change the second..." the method already shown which is
*consuming* that list." ;)
The thing is, as I`ve written before, the method is not doing it`s task
properly. The problem lies simply in expression: "temp =
(double[,])lista[n]"; which is not working. The reason of that is, because
not alwalys elements of list are double[,]. As I said they sometimes have to
be byte[,], or even short[,]. The compiler doesn`t allow conversion of
array in list, to a different type than it is. I mean, if i pass a list of
arrays of type double to the method showed at the beginning - it is working
well. The problem comes when I pass a list of arrays of type byte (or
short) - different than a double.

I thought that it is possible to convert i.e byte[,] -> double[,] but as I
can see it isn`t. And so I`ve no idea how should I solve it
 
J

Jon Skeet [C# MVP]

Macin said:
I have to change the second..." the method already shown which is
*consuming* that list." ;)
The thing is, as I`ve written before, the method is not doing it`s task
properly. The problem lies simply in expression: "temp =
(double[,])lista[n]"; which is not working. The reason of that is, because
not alwalys elements of list are double[,]. As I said they sometimes have to
be byte[,], or even short[,]. The compiler doesn`t allow conversion of
array in list, to a different type than it is. I mean, if i pass a list of
arrays of type double to the method showed at the beginning - it is working
well. The problem comes when I pass a list of arrays of type byte (or
short) - different than a double.

I thought that it is possible to convert i.e byte[,] -> double[,] but as I
can see it isn`t. And so I`ve no idea how should I solve it

You could cast to Array, use GetValue(int, int) to retrieve the value,
and then Convert.ToDouble to convert whatever type it is to double. It
won't be quite as efficient as testing for the different array types
and then dealing with them without boxing, but it probably won't be a
problem.
 
M

Macin

I am really sorry Jon, that I didn`t catch your idea few hours ago (I mean
the one with testing the array type). I was still thinking about it and
finally caught it :))) Now it`s working as it should work. I`m very
gratefull. Thanks once again. Have a good day.

I`m attaching changed piece of code:

object item = lista[n];
double[,] tempD = item as double[,];
byte[,] tempB = item as byte[,];
short[,] tempS = item as short[,];
n+=1;
for (int i=0;i<8;i++)
{
for (int j=0;j<8;j++)
{
if (tempD != null) duza[i+k,l+j]=tempD[i,j];
else if (tempB != null) duza[i+k,l+j]=tempB[i,j];
else if (tempS != null) duza[i+k,l+j]=tempS[i,j];
}
}

Uzytkownik "Jon Skeet said:
Macin said:
I have to change the second..." the method already shown which is
*consuming* that list." ;)
The thing is, as I`ve written before, the method is not doing it`s task
properly. The problem lies simply in expression: "temp =
(double[,])lista[n]"; which is not working. The reason of that is,
because
not alwalys elements of list are double[,]. As I said they sometimes have
to
be byte[,], or even short[,]. The compiler doesn`t allow conversion of
array in list, to a different type than it is. I mean, if i pass a list
of
arrays of type double to the method showed at the beginning - it is
working
well. The problem comes when I pass a list of arrays of type byte (or
short) - different than a double.

I thought that it is possible to convert i.e byte[,] -> double[,] but as
I
can see it isn`t. And so I`ve no idea how should I solve it

You could cast to Array, use GetValue(int, int) to retrieve the value,
and then Convert.ToDouble to convert whatever type it is to double. It
won't be quite as efficient as testing for the different array types
and then dealing with them without boxing, but it probably won't be a
problem.
 

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