Reference into an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Jones

    Reference into an array

    Hi,

    Say I have a managed array:
    int[] myArray = { 1, 2, 3, 4, 5 };

    How can I define a reference to say the 3rd element of that array? I should
    be able to use this reference to obtain the value of the 3rd element even
    when the value stored there changes.

    Thanks,

    TJ




  • Richard Blewett [DevelopMentor]

    #2
    Re: Reference into an array

    "Tom Jones" <bittwiddler35@ hotmail.com> wrote in message
    news:unG244T5FH A.4012@TK2MSFTN GP14.phx.gbl...[color=blue]
    > Hi,
    >
    > Say I have a managed array:
    > int[] myArray = { 1, 2, 3, 4, 5 };
    >
    > How can I define a reference to say the 3rd element of that array? I
    > should be able to use this reference to obtain the value of the 3rd
    > element even when the value stored there changes.
    >
    > Thanks,
    >
    > TJ
    >[/color]

    myArray[2] ?

    or are you asking for a direct pointer to the 3rd element rather than using
    the array index. The latter isn't possible without using unsafe code and
    unsafe code will mean pinning the array.
    Regards

    Richard Blewett - DevelopMentor






    Comment

    • Bill Butler

      #3
      Re: Reference into an array


      "Tom Jones" <bittwiddler35@ hotmail.com> wrote in message
      news:unG244T5FH A.4012@TK2MSFTN GP14.phx.gbl...[color=blue]
      > Hi,
      >
      > Say I have a managed array:
      > int[] myArray = { 1, 2, 3, 4, 5 };
      >
      > How can I define a reference to say the 3rd element of that array? I should be able to use this
      > reference to obtain the value of the 3rd element even when the value stored there changes.
      >[/color]

      For Value types like int you will need to Re-access the array every time since value types are
      copied when you read them:
      int x = myArray[2]; // copies the third element to x

      Bill


      Comment

      • Tom Jones

        #4
        Re: Reference into an array


        "Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
        dot uk> wrote in message news:eL08gFU5FH A.472@TK2MSFTNG P15.phx.gbl...[color=blue]
        > "Tom Jones" <bittwiddler35@ hotmail.com> wrote in message
        > news:unG244T5FH A.4012@TK2MSFTN GP14.phx.gbl...[color=green]
        >> Hi,
        >>
        >> Say I have a managed array:
        >> int[] myArray = { 1, 2, 3, 4, 5 };
        >>
        >> How can I define a reference to say the 3rd element of that array? I
        >> should be able to use this reference to obtain the value of the 3rd
        >> element even when the value stored there changes.
        >>
        >> Thanks,
        >>
        >> TJ
        >>[/color]
        >
        > myArray[2] ?
        >
        > or are you asking for a direct pointer to the 3rd element rather than
        > using the array index. The latter isn't possible without using unsafe code
        > and unsafe code will mean pinning the array.
        > Regards[/color]

        Hi Richard,

        Thanks for the reply. Yes I am talking about a "pointer". I know that I
        can do this:

        unsafe static void Main(string[] args)
        {
        int[] myArray = { 1, 2, 3, 4, 5 };

        fixed(int* pi = &myArray[3])
        {
        Console.WriteLi ne(*pi); // displays "4"
        myArray[3] = 6;
        Console.WriteLi ne(*pi); // displays "6"
        }
        }

        Why is creating a reference to a reference considered so "bad" that I have
        to resort to this type of thing?

        Thanks,
        BT


        Comment

        • Daniel Jin

          #5
          Re: Reference into an array



          "Tom Jones" wrote:
          [color=blue]
          >
          > "Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
          > dot uk> wrote in message news:eL08gFU5FH A.472@TK2MSFTNG P15.phx.gbl...[color=green]
          > > "Tom Jones" <bittwiddler35@ hotmail.com> wrote in message
          > > news:unG244T5FH A.4012@TK2MSFTN GP14.phx.gbl...[color=darkred]
          > >> Hi,
          > >>
          > >> Say I have a managed array:
          > >> int[] myArray = { 1, 2, 3, 4, 5 };
          > >>
          > >> How can I define a reference to say the 3rd element of that array? I
          > >> should be able to use this reference to obtain the value of the 3rd
          > >> element even when the value stored there changes.
          > >>
          > >> Thanks,
          > >>
          > >> TJ
          > >>[/color]
          > >
          > > myArray[2] ?
          > >
          > > or are you asking for a direct pointer to the 3rd element rather than
          > > using the array index. The latter isn't possible without using unsafe code
          > > and unsafe code will mean pinning the array.
          > > Regards[/color]
          >
          > Hi Richard,
          >
          > Thanks for the reply. Yes I am talking about a "pointer". I know that I
          > can do this:
          >
          > unsafe static void Main(string[] args)
          > {
          > int[] myArray = { 1, 2, 3, 4, 5 };
          >
          > fixed(int* pi = &myArray[3])
          > {
          > Console.WriteLi ne(*pi); // displays "4"
          > myArray[3] = 6;
          > Console.WriteLi ne(*pi); // displays "6"
          > }
          > }
          >
          > Why is creating a reference to a reference considered so "bad" that I have
          > to resort to this type of thing?
          >[/color]

          it's not that this is "bad". just when you program in a managed
          environment, there are certain rules you need to follow. with the presence
          of a garbage collector, you just can't randomly hold pointers to memory
          locations because things get moved around all the time. that's the tradeoff,
          you gain certain features in the managed world by giving up other features.
          [color=blue]
          > Thanks,
          > BT
          >
          >
          >[/color]

          Comment

          • Mattias Sjögren

            #6
            Re: Reference into an array

            Tom,
            [color=blue]
            >Say I have a managed array:
            >int[] myArray = { 1, 2, 3, 4, 5 };[/color]
            [color=blue]
            >How can I define a reference to say the 3rd element of that array?[/color]

            How would you use such as thing if it was supported? Can't you just
            define your own type that encapsulates the array reference and an
            index? Something like

            class ArrayElement<T>
            {
            private T[] _array;
            private int _idx;

            public ArrayElement(T[] array, int idx)
            {
            Debug.Assert(ar ray != null);
            _array = array;
            _idx = idx;
            }

            public T Value
            {
            get { return _array[_idx}; }
            set { _array[_idx] = value; }
            }

            // Add conversion operators and stuff if you want to
            // make it a bit more transparent.
            }

            ArrayElement<in t> third = new ArrayElement<in t>(myArray, 2);
            third.Value = 42;


            Mattias

            --
            Mattias Sjögren [MVP] mattias @ mvps.org
            http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
            Please reply only to the newsgroup.

            Comment

            Working...