473,320 Members | 1,916 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,320 software developers and data experts.

Help - Accessing the UInt16 array ref'ed in a collectionitem

I have a collection that can have a reference to various types of
arrays.

String was easy:
...
string[] sPropertyArray = (string[])oReturn[oProperty.Name];
foreach( string sItem in sPropertyArray )
{
...

How do I handle a UInt16 array?

I have tried( and various permutations):
...
uint[] iPropertyArray = (uint[])TypeConverter.ConvertTo(
oReturn[oProperty.Name] , typeof( System.UInt32[] ) );
foreach( uint iItem in iPropertyArray )
{
...

Continue to get:

error CS0120: An object reference is required for the nonstatic
field, method, or property
'System.ComponentModel.TypeConverter.ConvertTo(obj ect, System.Type)'

Thanks,
Norman
Nov 15 '05 #1
3 1596
Norman <ni********@hotmail.com> wrote:
I have a collection that can have a reference to various types of
arrays.

String was easy:
...
string[] sPropertyArray = (string[])oReturn[oProperty.Name];
foreach( string sItem in sPropertyArray )
{
...

How do I handle a UInt16 array?

I have tried( and various permutations):
...
uint[] iPropertyArray = (uint[])TypeConverter.ConvertTo(
oReturn[oProperty.Name] , typeof( System.UInt32[] ) );
foreach( uint iItem in iPropertyArray )
{
...


Why did you try that? Why not just:

uint[] iPropertyArray = (uint[]) oReturn[oProperty.Name];

What does oReturn[oProperty.Name] actually give you?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
uint[] iPropertyArray = (uint[]) oReturn[oProperty.Name];
You get this at run time:

Unhandled Exception: System.InvalidCastException: Specified cast is
not valid.

If you try this:

Console.WriteLine( oReturn[oProperty.Name] )

Output
System.UInt16[]

If it happened to be a string array:

Output:
System.String[]

That's why I cast it and looped through the string array and output
the actual values.

I need to do the same thing with UInt16[], UInt32[] and UInt8[].

Jon Skeet <sk***@pobox.com> wrote in message >

Why did you try that? Why not just:

uint[] iPropertyArray = (uint[]) oReturn[oProperty.Name];

What does oReturn[oProperty.Name] actually give you?

Norman <ni********@hotmail.com> wrote:
I have a collection that can have a reference to various types of
arrays.

String was easy:
...
string[] sPropertyArray = (string[])oReturn[oProperty.Name];
foreach( string sItem in sPropertyArray )
{
...

How do I handle a UInt16 array?

I have tried( and various permutations):
...
uint[] iPropertyArray = (uint[])TypeConverter.ConvertTo(
oReturn[oProperty.Name] , typeof( System.UInt32[] ) );
foreach( uint iItem in iPropertyArray )
{
...

Nov 15 '05 #3
Norman <ni********@hotmail.com> wrote:
uint[] iPropertyArray = (uint[]) oReturn[oProperty.Name];
You get this at run time:

Unhandled Exception: System.InvalidCastException: Specified cast is
not valid.
Then presumably it hasn't returned an array of uints! The same would go
for your string case if you weren't returning strings.
If you try this:

Console.WriteLine( oReturn[oProperty.Name] )

Output
System.UInt16[]


Ah... those are ushorts, not uints. Change your code to use ushort
instead of uint and you should be fine.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

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

Similar topics

1
by: Matthew Hood | last post by:
Here's the situation. I am developing an ASP.NET web application. Most of my forms will be accessing a database (MS Access) for either record creation/deletion/updating or for list lookups. The...
3
by: HLong | last post by:
I am trying to understand an example where the Least Significant Bit is replaced. The code is in C# and I am having problems with a line that reads: B= (byte) (Value == 1 ? B | (1 << poss) : B & ~...
2
by: edself | last post by:
Greetings, I am semi-new to Access and have a query question. I presume the solution is easy, but need some help. I have created a database with a Contact table. The contact table contains...
2
by: edself | last post by:
Greetings, I am semi-new to Access and have a query question. I presume the solution is easy, but need some help. I have created a database with a Contact table. The contact table contains...
3
by: Gabe Covert | last post by:
I'm a new C# developer, and am developing an application which will utilize a COM library from a third party. I have two following SDK calls from the 3rd-party SDK which I can't get to work under...
3
by: edmitchell | last post by:
I'm getting an error when I pass "User" in "report.HasBeenAccesedBy(ref User,ref UserN);" I have created the iL to see if there is something incorrect. ..method public hidebysig newslot abstract...
13
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't...
5
by: Ed Jay | last post by:
I have a switch statement that controls which of several containers is displayed or not. It currently looks like: function showHelp(n) { show('vhelp'); //makes parent container visible switch...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.