When is an array not an array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jerry Negrelli

    #1

    When is an array not an array?

    I was enumerating a collection and printing the value of
    each data item when I noticed that some of the objects in
    the collection returned "System.Obj ect[]" from
    their .ToString() method.

    Assuming that they aren't literally strings with the
    value "System.Obj ect[]", how can I convert one of these
    objects to an object array? Reflection?
    Convert.ChangeT ype?

    My code:

    DirectoryEntry parent = new DirectoryEntry
    (@"LDAP://RootDSE");

    IEnumerator bob =
    parent.Properti es.PropertyName s.GetEnumerator ();
    while(bob.MoveN ext()){
    if(parent.Prope rties[bob.Current.ToS tring
    ()].Value.GetType( ).IsArray){
    Response.Write( "my array: " + parent.Properti es
    [bob.Current.ToS tring()].Value.ToString ());
    }
    }

    JER
  • Jon Skeet [C# MVP]

    #2
    Re: When is an array not an array?

    Jerry Negrelli <jerry.negrelli @removethispart datascientific. com> wrote:[color=blue]
    > I was enumerating a collection and printing the value of
    > each data item when I noticed that some of the objects in
    > the collection returned "System.Obj ect[]" from
    > their .ToString() method.
    >
    > Assuming that they aren't literally strings with the
    > value "System.Obj ect[]", how can I convert one of these
    > objects to an object array? Reflection?
    > Convert.ChangeT ype?[/color]

    Just cast to object[]. You don't need to convert the objects at all -
    they *are* arrays.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Jerry Negrelli

      #3
      Re: When is an array not an array?

      I'd tried that but was getting an invalid cast exception
      at runtime. It turns out there is also a byte array in
      the collection, so casting the byte array to an object
      array is what was causing the error.

      Thanks Jon!

      Jerry[color=blue]
      >-----Original Message-----
      >Jerry Negrelli[/color]
      <jerry.negrelli @removethispart datascientific. com> wrote:[color=blue][color=green]
      >> I was enumerating a collection and printing the value[/color][/color]
      of[color=blue][color=green]
      >> each data item when I noticed that some of the objects[/color][/color]
      in[color=blue][color=green]
      >> the collection returned "System.Obj ect[]" from
      >> their .ToString() method.
      >>
      >> Assuming that they aren't literally strings with the
      >> value "System.Obj ect[]", how can I convert one of[/color][/color]
      these[color=blue][color=green]
      >> objects to an object array? Reflection?
      >> Convert.ChangeT ype?[/color]
      >
      >Just cast to object[]. You don't need to convert the[/color]
      objects at all -[color=blue]
      >they *are* arrays.
      >
      >--
      >Jon Skeet - <skeet@pobox.co m>
      >http://www.pobox.com/~skeet
      >If replying to the group, please do not mail me too
      >.
      >[/color]

      Comment

      Working...