I found the solution, but it was a bit tricky. The hatch was that it
should be typecastet as an object[] array.
Thanks everyone for your time and help.
Here is the code that work:

            ArrayList morlist = new ArrayList();
            ManagedObjectReference vmmor;
            string vmName;

            for (int oci = 0; oci < ocary.Length; oci++) {
               oc = ocary[oci];
               mor = oc.obj;
               pcary = oc.propSet;

               log.LogLine("Object Type : " + mor.type);
               log.LogLine("Reference Value : " + mor.Value);

               morlist = clientInfo.SvcUtil.GetDecendentMoRefs(mor,
"VirtualMachine");

               for (int vm = 0; vm <= morlist.Count - 1; vm++)
               {
                   Object[] objary = (Object[])morlist[vm];
                   vmmor = (ManagedObjectReference)objary[0];
                   vmName = (string)objary[1];
                   Console.WriteLine(vmName.ToLower());
               }


On 11 Maj, 21:50, Karsten_Markmann <[email protected]> wrote:
> I have now tried casting each object but the same result:
>             object dp = null;
>             string vmName;
>
>             for (int oci = 0; oci < ocary.Length; oci++) {
>                oc = ocary[oci];
>                mor = oc.obj;
>                pcary = oc.propSet;
>
>                log.LogLine("Object Type : " + mor.type);
>                log.LogLine("Reference Value : " + mor.Value);
>
>                al = clientInfo.SvcUtil.GetDecendentMoRefs(mor,
> "VirtualMachine");
>
>                for (int vm = 0; vm <= al.Count - 1; vm++)
>                {
>                    dp = (object)al[vm][0];  <------ Compiler says:
> Cannot apply indexing with [] to an expression of type 'object'
>                    vmName = (string)al[vm][1]; <------ Compiler says:
> Cannot apply indexing with [] to an expression of type 'object'
>                    //I can see in the debugger that the [1] parameter
> of the al[vm] is of type string, and it's this stringvalue I need.
>
>                    Console.WriteLine(vmName);
>                }
>
> On 11 Maj, 20:40, "[email protected]" <[email protected]> wrote:
>
>
>
> > Cerebrus,
>
> > Would the item not be automatically cast to an indexable type if
> > vmName were initially declared as such?
> > I was working under the assumption that it would.
>
> > Steve-0
>
> > On May 11, 12:25 pm, Cerebrus <[email protected]> wrote:
>
> > > An ArrayList holds objects (instances of type Object). It does not
> > > enforce strong typing. Therefore, you cannot directly set the item at
> > > index "i" to vmName (whatever type that variable is defined as).
>
> > > You will need to cast the object at position "i" to the type of object
> > > at each index. Only then can it be queried further.
>
> > > On May 11, 5:33 pm, Karsten_Markmann <[email protected]> wrote:
>
> > > > Hi
> > > > I am having some third party code that returns me an ArrayList.
> > > > In this list there are X number of objects that each hold 2 objects.
> > > > I must retrieve the object on postion [1] of each object returned in
> > > > the list.
>
> > > > I have the following code snippet, which gives me a compiler error:
> > > > Cannot apply indexing with [] to an expression of type 'object'
>
> > > >  ArrayList al = new ArrayList();
>
> > > >  al = clientInfo.SvcUtil.GetDecendentMoRefs(mor, "VirtualMachine");
>
> > > >                for (int vm = 0; vm <= al.Count - 1; vm++)
> > > >                {
> > > >                    vmName = al[vm];
>
> > > >                    Console.WriteLine(vmName[1]);
> > > >                }
>
> > > > I am a bit stuck, solving this problem. Any help is appriciated.
> > > > Kind Regards
> > > > Karsten Markmann- Skjul tekst i anførselstegn -
>
> > - Vis tekst i anførselstegn -- Skjul tekst i anførselstegn -
>
> - Vis tekst i anførselstegn -

Reply via email to