一点体会

本文探讨了在编程过程中如何通过封装代码到方法中来提高代码质量,减少冗余,以PN系列组合框赋值为例,展示了一种更高效的方法实现,并强调了在数据操作时对数据源进行非空和数据行数的判断的重要性,从而避免潜在的运行时错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刚开始写程序的时候是模仿,别人咋写,也就跟着咋写,要是没有跟对人,就会养成很多不好的习惯,以及使得程序很臃肿。


例如:给下拉条combobox赋值时,新手写法如下:

            ComboBox_gongchengyali.Items.Clear();
            ComboBox_gongchengyali.Items.Add("PN 2.5");
            ComboBox_gongchengyali.Items.Add("PN 6");
            ComboBox_gongchengyali.Items.Add("PN 10");
            ComboBox_gongchengyali.Items.Add("PN 16");
            ComboBox_gongchengyali.Items.Add("PN 25");
            ComboBox_gongchengyali.Items.Add("PN 40");
            ComboBox_gongchengyali.Items.Add("PN 63");
            ComboBox_gongchengyali.Items.Add("PN 100");
            ComboBox_gongchengyali.Items.Add("PN 160");
            ComboBox_gongchengyali.SelectedIndex = 0;


假如数据比较有规律,可以写个循环

            ComboBox_gongchengyali.Items.Clear();
            for (int i = 0; i < 10; i++)
            {
                ComboBox_gongchengyali.Items.Add("PN " + 2.5 * i);
            }
            ComboBox_gongchengyali.SelectedIndex = 0;

当我意识到工程有大量这样的代码时,才明白写一个方法是最好的方式。

一个方法,到处调用。

        /// <summary>
        /// 给下拉条赋值
        /// </summary>
        /// <param name="combobox">combobox</param>
        /// <param name="strArray">赋值数组</param>
        private void SetComboboxValue(ComboBox combobox, string[] strArray)
        {
            if (combobox.Items.Count > 0)
            {
                combobox.Items.Clear();
            }
            if (strArray != null && strArray.Length > 0)
            {
                for (int i = 0; i < strArray.Length; i++)
                {
                    combobox.Items.Add(strArray[i]);
                }
                combobox.SelectedIndex = 0;
            }
        }
调用起来很简单,能为工程瘦身不少。
            string[] strArr = new string[] { "PN 2.5", "PN 6", "PN 10", "PN 16" };
            SetComboboxValue(ComboBox_gongchengyali, strArr);

方法中有对combobox的数据进行判断,假如没有数据,就进行Clear是会报错的。

方法可以集中处理这些东西,包括对象是否为空,数组有没有数据。

下面的就是报错的案例:

                        case "RJ":
                            dataTable = AutoCADDatabase.SelectHGTmifengmianRJ(myConnection, gongchengzhijing, gongchengyali);
                            double.TryParse(dataTable.Rows[0]["f"].ToString().Trim(), out f);
                            double.TryParse(dataTable.Rows[0]["d"].ToString().Trim(), out d);
                            break;
没有对DataTable进行非空和数据行判断,当没有数据时,这里就是bug。

正确写法:

                        case "RJ":
                            dataTable = AutoCADDatabase.SelectHGTmifengmianRJ(myConnection, gongchengzhijing, gongchengyali);
                            if (dataTable != null && dataTable.Rows.Count > 0)
                            {
                                double.TryParse(dataTable.Rows[0]["f"].ToString().Trim(), out f);
                                double.TryParse(dataTable.Rows[0]["d"].ToString().Trim(), out d);
                            }
                            break;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值