Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. foreach loop behavior C#

foreach loop behavior C#

Scheduled Pinned Locked Moved C#
helpcsharp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    picasso2
    wrote on last edited by
    #1

    Hello experts
    I have this behavior that I cannot figure it out. The following code loops through and extracts some firewall settings. It is triggered by a button

    try
    {

    Type typeFWPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
    INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(typeFWPolicy2);

    foreach (INetFwRule rule in fwPolicy2.Rules)
    {
    lvItems.RuleName = rule.Name; ;
    lvItems.RemoteAddress = rule.RemoteAddresses;
    lvItems.Protocol = rule.Protocol.ToString();
    lvItems.LocalPort = rule.LocalPorts;
    dataGrid1.Items.Add(lvItems);
    //MessageBox.Show("close me");
    }

    }
    catch (Exception ex)
    {

    MessageBox.Show("something went wrong");

    }
    When I run it, the datagrid is populated with the same record (only the first) entry that is repeated several times.
    then I added a MessageBox to troubleshoot, then Datagrid is populated correctly but I need to acknowledge each Messagebox !!!!. It is like it just needs that extra mouse click.
    any help is greatly appreciated

    P OriginalGriffO L 3 Replies Last reply
    0
    • P picasso2

      Hello experts
      I have this behavior that I cannot figure it out. The following code loops through and extracts some firewall settings. It is triggered by a button

      try
      {

      Type typeFWPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
      INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(typeFWPolicy2);

      foreach (INetFwRule rule in fwPolicy2.Rules)
      {
      lvItems.RuleName = rule.Name; ;
      lvItems.RemoteAddress = rule.RemoteAddresses;
      lvItems.Protocol = rule.Protocol.ToString();
      lvItems.LocalPort = rule.LocalPorts;
      dataGrid1.Items.Add(lvItems);
      //MessageBox.Show("close me");
      }

      }
      catch (Exception ex)
      {

      MessageBox.Show("something went wrong");

      }
      When I run it, the datagrid is populated with the same record (only the first) entry that is repeated several times.
      then I added a MessageBox to troubleshoot, then Datagrid is populated correctly but I need to acknowledge each Messagebox !!!!. It is like it just needs that extra mouse click.
      any help is greatly appreciated

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You are overwriting the lvItems instance each time you go through the loop. You should move the instantiation of lvItems inside the loop so you have a new instance created each time.

      Advanced TypeScript Programming Projects

      1 Reply Last reply
      0
      • P picasso2

        Hello experts
        I have this behavior that I cannot figure it out. The following code loops through and extracts some firewall settings. It is triggered by a button

        try
        {

        Type typeFWPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
        INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(typeFWPolicy2);

        foreach (INetFwRule rule in fwPolicy2.Rules)
        {
        lvItems.RuleName = rule.Name; ;
        lvItems.RemoteAddress = rule.RemoteAddresses;
        lvItems.Protocol = rule.Protocol.ToString();
        lvItems.LocalPort = rule.LocalPorts;
        dataGrid1.Items.Add(lvItems);
        //MessageBox.Show("close me");
        }

        }
        catch (Exception ex)
        {

        MessageBox.Show("something went wrong");

        }
        When I run it, the datagrid is populated with the same record (only the first) entry that is repeated several times.
        then I added a MessageBox to troubleshoot, then Datagrid is populated correctly but I need to acknowledge each Messagebox !!!!. It is like it just needs that extra mouse click.
        any help is greatly appreciated

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        That's because you add the same item each time round the loop. Unless you create a new instance of lvItems inside the loop, each time you go round you overwrite the values in the same one - so when you exit the loop, you have one item added five times, say - and they will all display the "latest value" which will be the last rule you processed inside the loop. Add something like

        lvItems = new TypeOfLVItemThatIDontKnowAndYouDo();

        to the top of the loop, and it's use a different one each time.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • P picasso2

          Hello experts
          I have this behavior that I cannot figure it out. The following code loops through and extracts some firewall settings. It is triggered by a button

          try
          {

          Type typeFWPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
          INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(typeFWPolicy2);

          foreach (INetFwRule rule in fwPolicy2.Rules)
          {
          lvItems.RuleName = rule.Name; ;
          lvItems.RemoteAddress = rule.RemoteAddresses;
          lvItems.Protocol = rule.Protocol.ToString();
          lvItems.LocalPort = rule.LocalPorts;
          dataGrid1.Items.Add(lvItems);
          //MessageBox.Show("close me");
          }

          }
          catch (Exception ex)
          {

          MessageBox.Show("something went wrong");

          }
          When I run it, the datagrid is populated with the same record (only the first) entry that is repeated several times.
          then I added a MessageBox to troubleshoot, then Datagrid is populated correctly but I need to acknowledge each Messagebox !!!!. It is like it just needs that extra mouse click.
          any help is greatly appreciated

          L Offline
          L Offline
          lmoelleb
          wrote on last edited by
          #4

          As the actual problem has already been pointed out, I will just add: Use a debugger. Using a debugger is not something complicated you need to learn down the line. It is the first skill you need to acquire as it is really easy to lean - 5 minutes and you have the basic - and it will save you hours. First look at the debugger - Visual Studio | Microsoft Docs[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups