467,222 Members | 1,416 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,222 developers. It's quick & easy.

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
  • viewed: 1476
Share:
2 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by steveneng@earthlink.net | last post: by
5 posts views Thread by Denis Perelyubskiy | last post: by
4 posts views Thread by songkv@willowglen.com.sg | last post: by
204 posts views Thread by Alexei A. Frounze | last post: by
10 posts views Thread by Joel | last post: by
reply views Thread by Adict | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.