473,473 Members | 2,153 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Conversion problem object to array

Hi there,

I use a Custom OPC interface. Aftre reading multiple data I get an object
which includes an array. But in design time this is only declared as object
(OPC definition, can not change this).
I want to assign this object to a DataRow of the same size. The size is
fixed. I tryed to assign this object directly to a datarow, but i received a
narrow conversion error!
I tried a foreach statement but got an compiler error.
During runtime the object I receive is of type array. I tested this with the
following lines

class-definition for better understanding
public class s
{
public object DataValue
public int error
}

int[] newobject=new int[base.Size];

if (s.DataValue.GetType().IsArray) <- TRUE
{
//int j = 0;
//object[] values = ((object[])s.DataValue); <- not allowed
//foreach (object val in s.DataValue)
//{
// newobject[j] = System.Convert.ToInt32(val);
// j++;
//}
}

How can I convert this object to an array of integer ?

Thanx in advance
Wolfgang Baldauf
Nov 15 '05 #1
2 2626
Wolfgang,

If it's actually an int array, you can do

int[] values = (int[])s.DataValue;

If not, this may work better

foreach (object val in (Array)s.DataValue)
....

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #2
Wolfgang,

If it's actually an int array, you can do

int[] values = (int[])s.DataValue;

If not, this may work better

foreach (object val in (Array)s.DataValue)
....

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #3

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

Similar topics

4
by: Jim Hubbard | last post by:
I have some C# code that is supposed to wrap the defrag APIs and I am trying to convert it to VB.Net (2003). But, I keep having problems. The C# code is relatively short, so I'll post it...
1
by: William Starr Moake | last post by:
This is for a browser-based WYSIWYG editor to convert image file paths from absolute to relative when the page under construction is saved. From inside the save function I get error message:...
2
by: Sankar Nemani | last post by:
Why does the following code throw an exception? object o = new object{"sankar", "sankar2"}; string s; s =(string)o; The C# language spec says something different on MSDN: For any two...
9
by: Codemonkey | last post by:
Hi, Sorry for a stupid question, but is it possible to do a narrowing conversion with an object array with Option Strict On in VB? E.g: ------------------ Dim aBase as Base() = {New...
4
by: Petro | last post by:
Good morning The following code generates the exception: "Option Strict On disallows implicit conversion from System.Array to 1-dimensional array of 'String'. I've been unable to find the...
2
by: sunshine | last post by:
I'm having trouble converting this FOR loop from VB.NET to C#: Here's the VB.NET code: Dim localCart As Object localCart = Session("cart") For i = 0 To UBound(localCart, 2) If...
11
by: jyck91 | last post by:
// Base Conversion // Aim: This program is to convert an inputted number // from base M into base N. Display the converted // number in base N. #include <stdio.h> #include <stdlib.h>...
6
by: Michael Bray | last post by:
I'm trying to figure out what is the easiest way in C# 2.0 to convert an object array (object, int, anything) to a string array (string or List<string>) in one line of code. At first I thought I...
1
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
1
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.