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

How to retrieve values from an array assigned to object type

I have a component that implements a property called obj of type object.

The intent is that a user can pass in any value or array of values and I
will be able to retrieve the values.

If the user creates an array and passes it to the obj property, I'm having
trouble figuring out an easy way to obtain the values in the array without
using a conditional for each type.

Is there a built-in method to obtain the values of an array assigned to
object without a giant conditional for each system type?

The following code demonstrates the problem.

// Create an object
object obj;
// Create an array type
short[] sArray = new short[10] {1,2,3,4,5,6,7,8,9,0};
// Assign sArray reference to obj
obj = sArray;

// The next line prints out 'System.Int16[]' indicating that the type for
obj is a short array
Console.WriteLine(obj.GetType().ToString());

// However, the iteration that follows will result in a compiler exception
// error CS1579: foreach statement cannot operate on variables of type
'object' because 'object' does not contain a definition for 'GetEnumerator',
or it is inaccessible
foreach (short s in obj)
Console.WriteLine(s.ToString());

// I can use a giant conditional like the code shown below but it is
cumbersome and repetitive
// switch is not allowed on System.Type, so an if..else if is used
if (obj.GetType().GetElementType() == typeof(short))
{
foreach (short s in (short[])obj)
Console.WriteLine(s.ToString());
}

Sep 27 '05 #1
2 1745
Hi HairlipDog,

Thanks for your post.

Yes, with getting the array with System.Object type, we can get the benifit
of polymorphism. In order to get the element in the array, we can cast the
object into IList interface(which all array implemented). Then loop through
the list to get each element. Sample code listed below:
// Create an object
object obj;
// Create an array type
short[] sArray = new short[10] {1,2,3,4,5,6,7,8,9,0};
// Assign sArray reference to obj
obj = sArray;

if(obj.GetType().GetElementType()!=null)
{
IList il=obj as IList;
for(int i=0;i<il.Count ;i++)
{
Console.WriteLine(il[i].ToString());
}
}

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Sep 28 '05 #2
Hi HairlipDog,

Does my reply make sense to you? Is your problem resolved? Please feel free
to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Oct 3 '05 #3

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
6
by: steveneng | last post by:
C++ Primer Plus Programming Exercises 4th Ed - Prate Help I'm trying to refresh myself and I'm stuck on this problem (not homework/school related but for personal advancement). 6: Do...
5
by: Denis Perelyubskiy | last post by:
Hello, I need to make an array of elements accross forms. My javascript skills, as evident from this question, are rather rudimentary. I tried to make an associative array and index it with...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
2
by: HairlipDog58 | last post by:
I have a component that implements a property called obj of type object. The intent is that a user can pass in any value or array of values and I will be able to retrieve the values. If the...
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
10
by: Joel | last post by:
Is it true that if we don't specify a default constructor for our class, then the C# compiler provides us with its own that zeroes (or assigns default values) to the data members? I wrote a...
2
by: phpachu | last post by:
Hi, I hav created a group of textboxes using a loop and its names are unique and names are assigned using variable ( like <input type=text name=$name1>). Then How can i retrieve the values in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.