Arraylist Copyto() InvalidCastException

P

pengbsam

Hello All:
Having a problem with arraylist.copyto function. And here's a
sample of my code in C#:
In global-->
public struct point
{
int x;
string y;

}


static public point point;
static public point pointarray[];
static public Arraylist myal = new Arraylist();

in sub_function -->
for (i=0; i< some limites; i++)
{
global.point.x = some limites.some value;
global.point.y = some limites.some string;
global.myal.Add(global.point);



}


in Main function -->

int a;
string b;


global.pointarray = new global.point[global.myal.Count];
global.myal.Copyto(pointarray);
for (i=0; i<global.myal.count; i++)
{
a = pointarray.x;
b = pointarray.y;



}


global.myal.Clear();
pointarray = null;

This is basically the logic of the steps that I am trying to make a
dynamic array struct, of course I understand there are different ways
of doing this, but this one seems easiest if I was to pick and choose
what needed to be inserted into my arraylist in main / sub function. It

works fine on my computer (xp pro .net 2.0), however it doesn't work on

my company's server computer (running server2003.net 2.0) Saying there
there is a casting exception on the global.myal.Copyto(pointarray)
statement. I spend a lot of time trying to understand why this differ
from machine to machine. But came up with nothing in MSDN or other
resources. Have anyone done something like this? Please let me know!!
Thank You!


Also, it keep on telling me that I have a casting problem, so I tried
to explicitly cast the array out of arraylist instead of copyto()..
Doesn't work either. Any ideas how to do that? Very much appreciated
it!!
 
J

Jon Shemitz

in Main function -->
global.pointarray = new global.point[global.myal.Count];
global.myal.Copyto(pointarray);
works fine on my computer (xp pro .net 2.0), however it doesn't work on
my company's server computer (running server2003.net 2.0) Saying there
there is a casting exception on the global.myal.Copyto(pointarray)
statement.

Are the main method and the global class (struct?) in the same
assembly? Or is "global" in a library? If the latter, is it possible
that there is a version mismatch between what you're compiling with
and testing against on your development machine and what you're using
at runtime on the server?
 
P

pengbsam

Sorry to say, I am not sure what do you mean. It's in the same C#
project file? It's not from another DLL or calling another class! Is
that same Assembly?
 
J

Jon Shemitz

Sorry to say, I am not sure what do you mean. It's in the same C#
project file? It's not from another DLL or calling another class! Is
that same Assembly?

Yes, though I feel sort of obliged to point out that, in general,
you're not going to get much response to such very basic questions.
Few people are going to take the time to answer something that you
could easily Google, or answer via the VS help system, or even by
checking the index of just about any basic .NET book. For example, I'm
only answering that because I tried to answer your original query.

To get back to that, if it's not some sort of version mismatch, I'm
not really sure why it would run on your development system and not on
a server. It might help if you posted compilable code, not your
snippets.

One other possibility is that you compiled under 2.0, and the server
only has 1.1 installed ... or vice versa. This shouldn't give the
error you see, though ....
 

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