How do I get separate items out of an 'object' array?

  • Thread starter Thread starter jay1_z
  • Start date Start date
J

jay1_z

Here is my code in VB:

Return Me.Document.GetItemValue("Subject")(0)

The "GetItemValue" method returns an 'object' array, and the first item
in the array is a string. This works for me in VB but when trying to
convert this to C#:

return doc.GetItemValue("Subject")[0];

it bombs with this error:
" Cannot apply indexing with [] to an expression of type 'object' "

Does anyone know how to pull array items out of an 'object' array?
Thanks in advance.
 
jay1_z,

I'm not na expert in VB, but it won't work even there if you turn on that
*strict* setting in your project.
C# is a strongly typed language you cannot use a methods that are not
defined in the type. If you look at the Object type there is not indexers.
You can:
1. Change your GetItemValue signature so the method return type is a really
an array.
2. Cast the returned type to an array before using it.
3. Cast returned object to an IList.
4. User reflection.

If I had to do it I'd go for (1).
 
are you sure GetItemValue returns an aray of objects? if so this should work.
if it is an object array but is stored within an object reference then you
will need to cast it to the array before using the indexer, like this:

object[] objectArray = (object[])doc.GetItemValue("Subject");
return objectArray[0];

an object 'variable' can store a reference to any .NET type, but you need to
upcast to the specific type if you want to use any functionality which is
specific to that type. in your case, array indexing is only available if the
type of reference derives from System.Array

hth

kh
 
Back
Top