structure element by index

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ahmet AKGUN

    #1

    structure element by index


    Hi;

    I have a structure like

    public struct MyStr
    {
    string elem1;
    string elem2;
    string elem3;
    }

    I want to have referance to this elements by index.
    MStr.GetElem(2) must return a refarence to elem2.

    is that possible ?



  • Jon Skeet

    #2
    Re: structure element by index

    Ahmet AKGUN <akgun_ahmet@ho tmail.com> wrote:[color=blue]
    > I have a structure like
    >
    > public struct MyStr
    > {
    > string elem1;
    > string elem2;
    > string elem3;
    > }
    >
    > I want to have referance to this elements by index.
    > MStr.GetElem(2) must return a refarence to elem2.
    >
    > is that possible ?[/color]

    Not *very* easily - you'd need to use reflection, which is feasible but
    doesn't perform well and isn't generally something to use without good
    cause. Any reason you don't want to make it a string array internally?

    --
    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

    • Ahmet AKGUN

      #3
      Re: structure element by index


      Then I will lookup reflection api
      thanks.


      "Jon Skeet" <skeet@pobox.co m> wrote in message
      news:MPG.19bd2d caf294f0cd98a46 3@news.microsof t.com...[color=blue]
      > Ahmet AKGUN <akgun_ahmet@ho tmail.com> wrote:[color=green]
      > > unfotunately I must get referances to structer elements and their types.
      > > I have a common function that must do this for any structure given as
      > > parameter
      > > independent of structure elements.
      > >
      > > So i need these for structure, element count, element referances and[/color][/color]
      element[color=blue][color=green]
      > > data types.[/color]
      >
      > In that case I suggest you investigate reflection - but I don't know
      > whether reflection gives *any* guarantees about the order in which it
      > reports fields etc.
      >
      > --
      > 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...