473,414 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Exception when doing CopyTo

Hi

This code snippet fails with "Object cannot be stored in an array of
this type." whether I type the symbol 'items' as Size[] or just plain
Array or anything sensible.

Any ideas please?

Hashtable h = new Hashtable();

for(int i = 0; i < 10; i++)
{
h[i] = i;
}

Size[] items = new Size[h.Count];
h.Keys.CopyTo( items, 0 );

Tx

Nov 17 '05 #1
2 3424
<em**************@fastmail.fm> wrote:
This code snippet fails with "Object cannot be stored in an array of
this type." whether I type the symbol 'items' as Size[] or just plain
Array or anything sensible.

Any ideas please?

Hashtable h = new Hashtable();

for(int i = 0; i < 10; i++)
{
h[i] = i;
}

Size[] items = new Size[h.Count];
h.Keys.CopyTo( items, 0 );


Well, the keys are all Int32s, not Sizes. If you declare items to be an
int[], it'll work fine. It would also work if you declared items to be
an object[].

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
> <em**************@fastmail.fm> wrote:
This code snippet fails with "Object cannot be stored in an array of
this type." whether I type the symbol 'items' as Size[] or just plain
Array or anything sensible.

Any ideas please?

Hashtable h = new Hashtable();

for(int i = 0; i < 10; i++)
{
h[i] = i;
}

Size[] items = new Size[h.Count];
h.Keys.CopyTo( items, 0 );


Well, the keys are all Int32s, not Sizes. If you declare items to be an
int[], it'll work fine. It would also work if you declared items to be
an object[].


Jon - you're right. I 'adapted' this snippet from some original code
but it has the same problem. What was I thinking of?!

Cheers

Emma

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: | last post by:
I have a collectiond derived from NameObjectCollectionBase. FxCop is complaining that I need to implement a strongly typed CopyTo(MyObjectType, int) How do I do this? I can't seem to find any...
0
by: Glenn Venzke | last post by:
I have an aspx page that is set up to copy backed-up DB files from a shared directory to a local folder. For some reason, it is being denied access to the network share. I have the web app running...
2
by: .Net Believer | last post by:
I using the routine below to copy file to a network drive for a regular backup process. Before calling this routine I using another function to check the presence of the LAN connection and the...
2
by: Richard Collette | last post by:
Hi, I have a service, that runs perfectly when executed outside of the web service environment. When called as a web service I get the exception listed below sporadically. A call to the web...
6
by: KWienhold | last post by:
I'm currently working on a project in C# (VS 2003 SP1, .Net 1.1) that utilizes IStream/IStorage COM-Elements. Up to now I have gotten everything to work to my satisfaction, but now I have come...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
2
by: =?Utf-8?B?TmFjaA==?= | last post by:
I am new to .net can someone teranslate the foloowing in .net Ls and Lt will be listboxes. Sub ListCpSelected(Ls As Control, Lt As Control) 'Copies selected items from one list to another Dim I...
2
by: james | last post by:
Hi Guys, Would anyone mind telling me the difference between Array.Clone and Array.CopyTo? I was asked this during an interview and got partial credit for saying they were basically the same...
3
by: Michael Howes | last post by:
I have many double that each have a few thousand numbers in them. I need to concatenate groups of these double arrays into a new set of double. I don't know the total # of points. I thought it...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.