C# foreach syntax error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Douglas Gage

    C# foreach syntax error

    How can I do this in one line

    foreach((string answer in myansList) && (string qKey in qKeyList))
    {
    //do something
    }

    Thanks


  • Philip Rieck

    #2
    Re: C# foreach syntax error

    You can not.

    foreach(string answer in myansList)
    {
    foreach(string qKey in qKeyList)
    {
    //do something -- for each answer, iterate all items in qKeyList)
    }
    }


    "Douglas Gage" <haiaggie@neo.t amu.edu> wrote in message
    news:OyWSUUi2DH A.2544@TK2MSFTN GP10.phx.gbl...[color=blue]
    > How can I do this in one line
    >
    > foreach((string answer in myansList) && (string qKey in qKeyList))
    > {
    > //do something
    > }
    >
    > Thanks
    >
    >[/color]


    Comment

    • William Ryan

      #3
      Re: C# foreach syntax error

      Depending on what you are trying to do, either nest the second one within
      the first one...or fire them seperately. If you had 10 items in each and
      you nested them, you'd have 100 iterations which, if that's what you want by
      the && than will work. Otherwise split them out.

      HTH,

      Bill
      "Douglas Gage" <haiaggie@neo.t amu.edu> wrote in message
      news:OyWSUUi2DH A.2544@TK2MSFTN GP10.phx.gbl...[color=blue]
      > How can I do this in one line
      >
      > foreach((string answer in myansList) && (string qKey in qKeyList))
      > {
      > //do something
      > }
      >
      > Thanks
      >
      >[/color]


      Comment

      • Mark Johnson

        #4
        Re: C# foreach syntax error

        I don't think you can because you are using two conditions, but only one
        condition is supported.
        Use two foreach loops.

        Mark Johnson, Berlin Germany
        mj10777@mj10777 .de
        "Douglas Gage" <haiaggie@neo.t amu.edu> schrieb im Newsbeitrag
        news:OyWSUUi2DH A.2544@TK2MSFTN GP10.phx.gbl...[color=blue]
        > How can I do this in one line
        >
        > foreach((string answer in myansList) && (string qKey in qKeyList))
        > {
        > //do something
        > }
        >
        > Thanks
        >
        >[/color]


        Comment

        Working...