Compiler skipping foreach() loop during compilatio

2019-09-16 20:00发布

I created a method in which i am configuring a networking device via telnet.The problem is that i have multiple values which i am configuring using foreach() loop but the compiler is skipping that loop.I am using C# WPF and pattern is MVVM. Here is my code:

public ObservableCollection<VLANSPropertyClass> vlan { get; set; }

 public bool VLAN()
        {
            vlan = new ObservableCollection<VLANSPropertyClass>();

            string _command;

            _command = "config t \n";
            WriteAPCommand(_command);

            ReadAPCommand();

            foreach(VLANSPropertyClass v in vlan)
            {
                Console.WriteLine("-----------------FOREACH STARTED----------------");
                _command = "vlan " + v.vid + "\n";
                WriteAPCommand(_command);

                ReadAPCommand();

                _command = "name " + v.vname + "\n";
                WriteAPCommand(_command);

                ReadAPCommand();

                _command = "interface " + v.vid + "\n";
                WriteAPCommand(_command);

                ReadAPCommand();

                _command = "ip address " + v.ip + " " + v.mask + "\n";
                WriteAPCommand(_command);

                ReadAPCommand();

                _command="exit\r";
                WriteAPCommand(_command);

                ReadAPCommand();

                _command = "interface " + v.vports + "\n";
                WriteAPCommand(_command);

                ReadAPCommand();

                _command = "switchport access " + v.vid + "\n";
                WriteAPCommand(_command);

                ReadAPCommand();

                _command = "copy runningconfig startupconfig\n";
                WriteAPCommand(_command);

                ReadAPCommand();

            }
            Console.WriteLine("-----------------FOREACH COMPLETED----------------");
            return true;

        }

VLANSProperty class is:

public class VLANSPropertyClass
    {
        public string vname { get; set; }

        public int S_No { get; set; }

        public string vid { get; set; }

        public string ip { get; set; }

        public string mask { get; set; }

        public string vports { get; set; }
    }

bool Vlan() function is present in Model and it is calling from ViewModel.Can anyone tell me why my code is not executing correctly.

标签: c# wpf mvvm
1条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-16 20:16

You're creating a new ObservableCollection<VLANSPropertyClass>(), but it's empty. Therefore when you iterate over it, you never end up in the body of the loop. You need to populate your collection somewhere, before it's useful to iterate over it. (It's possible that you're expecting ReadAPCommand to populate it, but you haven't told us anything about that.)

Think about where you're expecting the data to come from, and how you expect it to get into your collection.

You should also consider your property and method names carefully - currently neither of them follow normal .NET conventions, and your VLAN method doesn't describe what it's meant to do at all.

查看更多
登录 后发表回答