foreach on an object[]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Stacey

    foreach on an object[]

    Can you use a foreach to set the elements of an object[] to null? This does
    not work as o is read-only. Is only way to use a for loop?

    object[] buffer = new object[3];
    //...fill buffer.
    foreach (object o in buffer)
    {
    o = null;
    }

    --
    William Stacey



  • Jeff

    #2
    Re: foreach on an object[]

    If it's a small array, you can also do this:

    object[] buffer = {null, null, null};

    --- Jeff

    "William Stacey" <staceyw@mvps.o rg> wrote in message
    news:uydlVFqhDH A.604@TK2MSFTNG P10.phx.gbl...[color=blue]
    > Can you use a foreach to set the elements of an object[] to null? This[/color]
    does[color=blue]
    > not work as o is read-only. Is only way to use a for loop?
    >
    > object[] buffer = new object[3];
    > //...fill buffer.
    > foreach (object o in buffer)
    > {
    > o = null;
    > }
    >
    > --
    > William Stacey
    >
    >
    >[/color]


    Comment

    • Jon Skeet

      #3
      Re: foreach on an object[]

      William Stacey <staceyw@mvps.o rg> wrote:[color=blue]
      > Can you use a foreach to set the elements of an object[] to null? This does
      > not work as o is read-only. Is only way to use a for loop?
      >
      > object[] buffer = new object[3];
      > //...fill buffer.
      > foreach (object o in buffer)
      > {
      > o = null;
      > }[/color]

      Even if o were not readonly, that would not change the buffer itself -
      o is a local variable: changing its value wouldn't change the value of
      anything else.

      However, there's a much better way of doing this. I'm not sure whether
      in your example, the //...fill buffer comment was saying that the
      foreach was doing it, in which case it's unnecessary in the first
      place, but if not, just use Array.Clear (buffer, 0, buffer.Length);

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

      • Leo Lin

        #4
        Re: foreach on an object[]

        I think the easist way is to create a new array
        object[] buffer = new object[3];
        //...fill buffer.
        buffer = new object[3];

        "William Stacey" <staceyw@mvps.o rg> wrote in message
        news:uydlVFqhDH A.604@TK2MSFTNG P10.phx.gbl...[color=blue]
        > Can you use a foreach to set the elements of an object[] to null? This[/color]
        does[color=blue]
        > not work as o is read-only. Is only way to use a for loop?
        >
        > object[] buffer = new object[3];
        > //...fill buffer.
        > foreach (object o in buffer)
        > {
        > o = null;
        > }
        >
        > --
        > William Stacey
        >
        >
        >[/color]


        Comment

        • Stu Smith

          #5
          Re: foreach on an object[]

          Surely all you need to do is:

          object[] buffer = new object[3];

          ....and you'll have an array of three object references, all of which are
          null.


          "Leo Lin" <leolin1@hotmai l.com> wrote in message
          news:eeZb7ErhDH A.1340@tk2msftn gp13.phx.gbl...[color=blue]
          > I think the easist way is to create a new array
          > object[] buffer = new object[3];
          > //...fill buffer.
          > buffer = new object[3];
          >
          > "William Stacey" <staceyw@mvps.o rg> wrote in message
          > news:uydlVFqhDH A.604@TK2MSFTNG P10.phx.gbl...[color=green]
          > > Can you use a foreach to set the elements of an object[] to null? This[/color]
          > does[color=green]
          > > not work as o is read-only. Is only way to use a for loop?
          > >
          > > object[] buffer = new object[3];
          > > //...fill buffer.
          > > foreach (object o in buffer)
          > > {
          > > o = null;
          > > }
          > >
          > > --
          > > William Stacey
          > >
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Humanity

            #6
            Re: foreach on an object[]

            You can do that for large arrays too, Jeff!
            AHAHAHAHAHAHAHA HAHAHAHA
            HAHAHAHAHAHAHAH AHAHAHAH
            AHAHAHAHAHAHAHA HAAHAHAH
            AHAHAHAHAHAHAAH AHA... *crack*

            "Jeff" <jeff@nowhere.c om> wrote in message
            news:#xEEOSqhDH A.3616@TK2MSFTN GP11.phx.gbl...[color=blue]
            > If it's a small array, you can also do this:
            >
            > object[] buffer = {null, null, null};
            >
            > --- Jeff
            >
            > "William Stacey" <staceyw@mvps.o rg> wrote in message
            > news:uydlVFqhDH A.604@TK2MSFTNG P10.phx.gbl...[color=green]
            > > Can you use a foreach to set the elements of an object[] to null? This[/color]
            > does[color=green]
            > > not work as o is read-only. Is only way to use a for loop?
            > >
            > > object[] buffer = new object[3];
            > > //...fill buffer.
            > > foreach (object o in buffer)
            > > {
            > > o = null;
            > > }
            > >
            > > --
            > > William Stacey
            > >
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Jeff

              #7
              Re: foreach on an object[]

              True, but who wants to code something like:

              object[] buffer = {null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, null, null, null,
              null, null, null, null, null};

              :-)

              "Humanity" <username@domai n.com> wrote in message
              news:O3%23urp2h DHA.616@TK2MSFT NGP11.phx.gbl.. .[color=blue]
              > You can do that for large arrays too, Jeff!
              > AHAHAHAHAHAHAHA HAHAHAHA
              > HAHAHAHAHAHAHAH AHAHAHAH
              > AHAHAHAHAHAHAHA HAAHAHAH
              > AHAHAHAHAHAHAAH AHA... *crack*
              >
              > "Jeff" <jeff@nowhere.c om> wrote in message
              > news:#xEEOSqhDH A.3616@TK2MSFTN GP11.phx.gbl...[color=green]
              > > If it's a small array, you can also do this:
              > >
              > > object[] buffer = {null, null, null};
              > >
              > > --- Jeff
              > >
              > > "William Stacey" <staceyw@mvps.o rg> wrote in message
              > > news:uydlVFqhDH A.604@TK2MSFTNG P10.phx.gbl...[color=darkred]
              > > > Can you use a foreach to set the elements of an object[] to null?[/color][/color][/color]
              This[color=blue][color=green]
              > > does[color=darkred]
              > > > not work as o is read-only. Is only way to use a for loop?
              > > >
              > > > object[] buffer = new object[3];
              > > > //...fill buffer.
              > > > foreach (object o in buffer)
              > > > {
              > > > o = null;
              > > > }
              > > >
              > > > --
              > > > William Stacey
              > > >
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • William Stacey

                #8
                Re: foreach on an object[]

                Allocating new arrays is not always the right design choice (even thou it is
                easy), especially in loops when you would need to make many allocations
                (i.e. streams, network objects, etc.) and could reuse the array.

                --
                William Stacey

                "Stu Smith" <stuarts@remove .digita.com> wrote in message
                news:#M0JCjzhDH A.2512@TK2MSFTN GP09.phx.gbl...[color=blue]
                > Surely all you need to do is:
                >
                > object[] buffer = new object[3];
                >
                > ...and you'll have an array of three object references, all of which are
                > null.
                >
                >
                > "Leo Lin" <leolin1@hotmai l.com> wrote in message
                > news:eeZb7ErhDH A.1340@tk2msftn gp13.phx.gbl...[color=green]
                > > I think the easist way is to create a new array
                > > object[] buffer = new object[3];
                > > //...fill buffer.
                > > buffer = new object[3];
                > >
                > > "William Stacey" <staceyw@mvps.o rg> wrote in message
                > > news:uydlVFqhDH A.604@TK2MSFTNG P10.phx.gbl...[color=darkred]
                > > > Can you use a foreach to set the elements of an object[] to null?[/color][/color][/color]
                This[color=blue][color=green]
                > > does[color=darkred]
                > > > not work as o is read-only. Is only way to use a for loop?
                > > >
                > > > object[] buffer = new object[3];
                > > > //...fill buffer.
                > > > foreach (object o in buffer)
                > > > {
                > > > o = null;
                > > > }
                > > >
                > > > --
                > > > William Stacey
                > > >
                > > >
                > > >[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Humanity

                  #9
                  Re: foreach on an object[]

                  Certainly not me!
                  I see your point :)

                  "Jeff" <jeff@nowhere.c om> wrote in message
                  news:e0n9ql3hDH A.1956@TK2MSFTN GP10.phx.gbl...[color=blue]
                  > True, but who wants to code something like:
                  >
                  > object[] buffer = {null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null, null, null, null, null, null, null, null,
                  > null, null, null, null, null};
                  >
                  > :-)
                  >
                  > "Humanity" <username@domai n.com> wrote in message
                  > news:O3%23urp2h DHA.616@TK2MSFT NGP11.phx.gbl.. .[color=green]
                  > > You can do that for large arrays too, Jeff!
                  > > AHAHAHAHAHAHAHA HAHAHAHA
                  > > HAHAHAHAHAHAHAH AHAHAHAH
                  > > AHAHAHAHAHAHAHA HAAHAHAH
                  > > AHAHAHAHAHAHAAH AHA... *crack*
                  > >
                  > > "Jeff" <jeff@nowhere.c om> wrote in message
                  > > news:#xEEOSqhDH A.3616@TK2MSFTN GP11.phx.gbl...[color=darkred]
                  > > > If it's a small array, you can also do this:
                  > > >
                  > > > object[] buffer = {null, null, null};
                  > > >
                  > > > --- Jeff
                  > > >
                  > > > "William Stacey" <staceyw@mvps.o rg> wrote in message
                  > > > news:uydlVFqhDH A.604@TK2MSFTNG P10.phx.gbl...
                  > > > > Can you use a foreach to set the elements of an object[] to null?[/color][/color]
                  > This[color=green][color=darkred]
                  > > > does
                  > > > > not work as o is read-only. Is only way to use a for loop?
                  > > > >
                  > > > > object[] buffer = new object[3];
                  > > > > //...fill buffer.
                  > > > > foreach (object o in buffer)
                  > > > > {
                  > > > > o = null;
                  > > > > }
                  > > > >
                  > > > > --
                  > > > > William Stacey
                  > > > >
                  > > > >
                  > > > >
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • Jeffrey Tan[MSFT]

                    #10
                    RE: foreach on an object[]


                    Hi William,

                    Yes, foreach statement does not allow the user to change the value in the
                    array.
                    I think you can initialize the array like this:

                    object[] buffer = new object[3];
                    for(int i=0;i<buffer.Le ngth ;i++)
                    {
                    buffer[i]=null;
                    }

                    Hope this helps,
                    Best regards,
                    Jeffrey Tan
                    Microsoft Online Partner Support
                    Get Secure! - www.microsoft.com/security
                    This posting is provided "as is" with no warranties and confers no rights.

                    --------------------
                    | From: "William Stacey" <staceyw@mvps.o rg>
                    | Subject: foreach on an object[]
                    | Date: Mon, 29 Sep 2003 11:49:20 -0400
                    | Lines: 15
                    | X-Priority: 3
                    | X-MSMail-Priority: Normal
                    | X-Newsreader: Microsoft Outlook Express 6.00.3790.0
                    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
                    | Message-ID: <uydlVFqhDHA.60 4@TK2MSFTNGP10. phx.gbl>
                    | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
                    | NNTP-Posting-Host: 66.188.59.114.b ay.mi.chartermi .net 66.188.59.114
                    | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP10.phx.g bl
                    | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1880 08
                    | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
                    |
                    | Can you use a foreach to set the elements of an object[] to null? This
                    does
                    | not work as o is read-only. Is only way to use a for loop?
                    |
                    | object[] buffer = new object[3];
                    | //...fill buffer.
                    | foreach (object o in buffer)
                    | {
                    | o = null;
                    | }
                    |
                    | --
                    | William Stacey
                    |
                    |
                    |
                    |

                    Comment

                    • William Stacey

                      #11
                      Re: foreach on an object[]

                      Thanks Jeff.

                      --
                      William Stacey, DNS MVP

                      "Jeffrey Tan[MSFT]" <v-jetan@online.mi crosoft.com> wrote in message
                      news:KCYMhCJiDH A.2076@cpmsftng xa06.phx.gbl...[color=blue]
                      >
                      > Hi William,
                      >
                      > Yes, foreach statement does not allow the user to change the value in the
                      > array.
                      > I think you can initialize the array like this:
                      >
                      > object[] buffer = new object[3];
                      > for(int i=0;i<buffer.Le ngth ;i++)
                      > {
                      > buffer[i]=null;
                      > }
                      >
                      > Hope this helps,
                      > Best regards,
                      > Jeffrey Tan
                      > Microsoft Online Partner Support
                      > Get Secure! - www.microsoft.com/security
                      > This posting is provided "as is" with no warranties and confers no rights.
                      >
                      > --------------------
                      > | From: "William Stacey" <staceyw@mvps.o rg>
                      > | Subject: foreach on an object[]
                      > | Date: Mon, 29 Sep 2003 11:49:20 -0400
                      > | Lines: 15
                      > | X-Priority: 3
                      > | X-MSMail-Priority: Normal
                      > | X-Newsreader: Microsoft Outlook Express 6.00.3790.0
                      > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
                      > | Message-ID: <uydlVFqhDHA.60 4@TK2MSFTNGP10. phx.gbl>
                      > | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
                      > | NNTP-Posting-Host: 66.188.59.114.b ay.mi.chartermi .net 66.188.59.114
                      > | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP10.phx.g bl
                      > | Xref: cpmsftngxa06.ph x.gbl[/color]
                      microsoft.publi c.dotnet.langua ges.csharp:1880 08[color=blue]
                      > | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
                      > |
                      > | Can you use a foreach to set the elements of an object[] to null? This
                      > does
                      > | not work as o is read-only. Is only way to use a for loop?
                      > |
                      > | object[] buffer = new object[3];
                      > | //...fill buffer.
                      > | foreach (object o in buffer)
                      > | {
                      > | o = null;
                      > | }
                      > |
                      > | --
                      > | William Stacey
                      > |
                      > |
                      > |
                      > |
                      >[/color]


                      Comment

                      Working...