foreach loop behavior C#
-
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 buttontry
{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 -
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 buttontry
{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 appreciatedYou 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.
-
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 buttontry
{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 appreciatedThat'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!
-
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 buttontry
{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 appreciatedAs 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[^]