How to convert an object that is an array to an array of objects?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using Visual C# 2003. So replies in C#-speak would be appreciated.

I have a function that is passed an object. The object is tested with the
property IsArray. If this test returns true, I need to operate on each of
the elements in the object individually as an object. So I would like to be
able to convert or cast the object into an object array (object []). I tried
the obvious casts but these failed.

How can I convert the object to an array of objects (object[])?

Thanks,
Dave
 
Do you have a concrete example?

note that IIRC (for instance) string[] does not inherit from object[] -
however, they all derive from Array

The following seems to work for me:

public static class Program {
public static void Main() {
string[] test = new string[] { "abc", "def", "ghi" };
Test(test);
Console.ReadLine();

}
public static void Test(object obj) {
if (obj!=null && obj.GetType().IsArray) {
Array array = obj as Array;
if (array != null) { // probably just paranoia
foreach (object item in array) {
Console.WriteLine(item);
}
}
}
}
}

Marc
 
Note: if you genuinely do actually need to get an object[] reference, you
could (under 2.0) use Array.ConvertAll:

public static void Main() {
string[] test = new string[] { "abc", "def", "ghi" };
object[] testAsObjectArray = ConvertToObjectArray(test);
Test(testAsObjectArray);
Console.ReadLine();

}
public static object[] ConvertToObjectArray<T>(T[] arary) {
return Array.ConvertAll<T, object>(arary, delegate(T item) {
return item; });
}
public static void Test(object obj) {
if (obj!=null && obj.GetType().IsArray) {
Array array = obj as Array;
if (array != null) { // probably just paranoia
foreach (object item in array) {
Console.WriteLine(item);
}
}
}
}

Marc Gravell said:
Do you have a concrete example?

note that IIRC (for instance) string[] does not inherit from object[] -
however, they all derive from Array

The following seems to work for me:

public static class Program {
public static void Main() {
string[] test = new string[] { "abc", "def", "ghi" };
Test(test);
Console.ReadLine();

}
public static void Test(object obj) {
if (obj!=null && obj.GetType().IsArray) {
Array array = obj as Array;
if (array != null) { // probably just paranoia
foreach (object item in array) {
Console.WriteLine(item);
}
}
}
}
}

Marc

Dave Leach said:
I am using Visual C# 2003. So replies in C#-speak would be appreciated.

I have a function that is passed an object. The object is tested with
the
property IsArray. If this test returns true, I need to operate on each
of
the elements in the object individually as an object. So I would like to
be
able to convert or cast the object into an object array (object []). I
tried
the obvious casts but these failed.

How can I convert the object to an array of objects (object[])?

Thanks,
Dave
 
Hi Dave,

Wouldn't an Array work?

private void Test(object o)
{
if(o is Array)
{
Array arr = (Array)o;
object p = arr[0]
}
}

I am using Visual C# 2003. So replies in C#-speak would be appreciated.

I have a function that is passed an object. The object is tested with the
property IsArray. If this test returns true, I need to operate on each of
the elements in the object individually as an object. So I would like to be
able to convert or cast the object into an object array (object []). I tried
the obvious casts but these failed.

How can I convert the object to an array of objects (object[])?

Thanks,
Dave
 
Marc,

Thanks for your reply. I keep forgetting about the "as" operator. It does
work for my situation.

Thanks,
Dave

Marc Gravell said:
Do you have a concrete example?

note that IIRC (for instance) string[] does not inherit from object[] -
however, they all derive from Array

The following seems to work for me:

public static class Program {
public static void Main() {
string[] test = new string[] { "abc", "def", "ghi" };
Test(test);
Console.ReadLine();

}
public static void Test(object obj) {
if (obj!=null && obj.GetType().IsArray) {
Array array = obj as Array;
if (array != null) { // probably just paranoia
foreach (object item in array) {
Console.WriteLine(item);
}
}
}
}
}

Marc

Dave Leach said:
I am using Visual C# 2003. So replies in C#-speak would be appreciated.

I have a function that is passed an object. The object is tested with the
property IsArray. If this test returns true, I need to operate on each of
the elements in the object individually as an object. So I would like to
be
able to convert or cast the object into an object array (object []). I
tried
the obvious casts but these failed.

How can I convert the object to an array of objects (object[])?

Thanks,
Dave
 
Marc,

The code in your example looks like it uses C++ templates, so I am assuming
you are using the generics capability of .NET 2.0. We haven't moved to that
yet, but thanks for the glimpse.

Dave

Marc Gravell said:
Note: if you genuinely do actually need to get an object[] reference, you
could (under 2.0) use Array.ConvertAll:

public static void Main() {
string[] test = new string[] { "abc", "def", "ghi" };
object[] testAsObjectArray = ConvertToObjectArray(test);
Test(testAsObjectArray);
Console.ReadLine();

}
public static object[] ConvertToObjectArray<T>(T[] arary) {
return Array.ConvertAll<T, object>(arary, delegate(T item) {
return item; });
}
public static void Test(object obj) {
if (obj!=null && obj.GetType().IsArray) {
Array array = obj as Array;
if (array != null) { // probably just paranoia
foreach (object item in array) {
Console.WriteLine(item);
}
}
}
}

Marc Gravell said:
Do you have a concrete example?

note that IIRC (for instance) string[] does not inherit from object[] -
however, they all derive from Array

The following seems to work for me:

public static class Program {
public static void Main() {
string[] test = new string[] { "abc", "def", "ghi" };
Test(test);
Console.ReadLine();

}
public static void Test(object obj) {
if (obj!=null && obj.GetType().IsArray) {
Array array = obj as Array;
if (array != null) { // probably just paranoia
foreach (object item in array) {
Console.WriteLine(item);
}
}
}
}
}

Marc

Dave Leach said:
I am using Visual C# 2003. So replies in C#-speak would be appreciated.

I have a function that is passed an object. The object is tested with
the
property IsArray. If this test returns true, I need to operate on each
of
the elements in the object individually as an object. So I would like to
be
able to convert or cast the object into an object array (object []). I
tried
the obvious casts but these failed.

How can I convert the object to an array of objects (object[])?

Thanks,
Dave
 
Morten,

No, the simple cast did not work. It will compile but it does not run
successfully, producing a runtime cast exception. Look at the first reply
from Marc. Using the "as" operator does accomplish the conversion.

Thanks,
Dave

Morten Wennevik said:
Hi Dave,

Wouldn't an Array work?

private void Test(object o)
{
if(o is Array)
{
Array arr = (Array)o;
object p = arr[0]
}
}

I am using Visual C# 2003. So replies in C#-speak would be appreciated.

I have a function that is passed an object. The object is tested with the
property IsArray. If this test returns true, I need to operate on each of
the elements in the object individually as an object. So I would like to be
able to convert or cast the object into an object array (object []). I tried
the obvious casts but these failed.

How can I convert the object to an array of objects (object[])?

Thanks,
Dave
 
Actually, it occurred to me you probably don't need this - if you needed an
object[] reference you could probably just create a new object[] of the
correct size, cast your unknown something[] as an Array, and use .CopyTo

Marc
 
Back
Top