简体   繁体   English

在这样的C#函数中使用Params是一个好习惯

[英]Using Params in function like this C# is it a good practice

FUNCTION ** 功能**

private void GetComboxItems(params int[] type) 
        {   
            try
            {
                /* DEPARTMENT CODE */
                if (type[0] == 1)
                {
                    cmbDept.Items.Clear();
                    using (SFCDataContext SFC = new SFCDataContext())
                    {
                        var Dept = (from i in SFC.Systems_SettingsDepartments
                                    orderby i.Department_ID
                                    select i);

                        foreach (var q in Dept)
                        {
                            cmbDept.Items.Add(q.Department_ID);
                        }

                        SFC.Connection.Close();
                    }                    
                }
                /* CORRECTIVE ACTION RECORD CODE */
                if (type[1] == 1)
                {
                    cmbCARNo.Items.Clear();
                    using (SFCDataContext SFC = new SFCDataContext())
                    {
                        var CarNo = (from i in SFC.Systems_CARLogSheets
                                     orderby i.CARDocNo
                                     where i.PostStatus == 0
                                     select new
                                     {
                                         Code = i.CARDocNo
                                     });
                        foreach (var w in CarNo)
                        {
                            cmbCARNo.Items.Add(w.Code);
                        }

                        SFC.Connection.Close();
                    }
                }
                /* MEASUREMENT CODE */
                if (type[2] == 1)
                {
                    cmbMeas.Items.Clear();
                    using (SFCDataContext SFC = new SFCDataContext())
                    {

                        var Measure = (from i in SFC.Systems_SettingsMeasurements
                                       orderby i.Measurement_ID
                                       where i.CategoryType == "Measurement"
                                       select new
                                       {
                                           DESC = i.Measurement
                                       });
                        foreach (var e in Measure)
                        {
                            cmbMeas.Items.Add(e.DESC);
                        }
                        SFC.Connection.Close();
                    }
                }
                /* SUB-MEASUREMENT CODE */
                if (type[3] == 1)
                {
                    cmbSubMeas.Items.Clear();
                    using (SFCDataContext SFC = new SFCDataContext())
                    {
                        var SubMeas = (from i in SFC.Systems_SettingsMeasurements
                                       orderby i.Measurement_ID
                                       where i.CategoryType == "Sub-Measurement"
                                       select new
                                       {
                                           DESC = i.Measurement
                                       });
                        foreach (var r in SubMeas)
                        {
                            cmbSubMeas.Items.Add(r.DESC);
                        }
                        SFC.Connection.Close();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message.ToString()); }
        }

* FORM LOAD ** *表格加载**

private void frmSQMProductivityReports_Load(object sender, EventArgs e)
        {
            GetComboxItems(1, 0, 1, 0);
        }

why is it that at this code.. my 1st if statement is "True" so it does what follows the code inside the if statement and it does. 为什么在此代码上..我的第一个if语句为“ True”,所以它按照if语句内的代码执行操作,然后执行。 now the 2nd if statement is "False" which it skips the function inside it. 现在第二个if语句为“ False”,它将跳过其中的函数。 but then now the 3rd if statement is "True" which is it should have do same as the 1st but as i have checked couple times it skips the function inside the if statement why is it? 但是,现在第三个if语句为“ True”,它应该与第一个相同,但是由于我检查了几次,它跳过了if语句中的函数,为什么呢? is there something wrong in my codes i tried looking at it its seems ok to me.. 我尝试查看它的代码中是否有错误,对我来说似乎没问题。

According to your input, the if conditions that meets the criteria are the first and the third.. note that some statements can be "skipped" if an exception is throw, so placing breakpoints there or printing logs may help you understand better what is happening. 根据您的输入,满足条件的if条件是第一个和第三个条件。.请注意,如果引发异常,则可以“跳过”某些语句,因此在该处放置断点或打印日志可以帮助您更好地了解正在发生的情况。

Side notes: 旁注:

  1. The use of params seems to be redundant in this case (it's mostly used when an unknown #arguments should be passed) since the number of arguments is fixed. 由于参数的数量是固定的,因此在这种情况下,使用params似乎是多余的(通常在应传递未知的#arguments时使用)。
  2. use bool type rather then int for flags 使用bool类型而不是int作为标志

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM