简体   繁体   English

因为它是一个方法组,所以不能分配给GetDropDownValues?

[英]Cannot assign to GetDropDownValues because it is a method group?

I have a question regarding the code below, am trying to get the dropdown values from a generic query, passing the field and table name. 我对以下代码有疑问,正在尝试从通用查询获取下拉列表值,并传递字段和表名。 I am getting the error Cannot assign to GetDropDownValues because it is a method group and not sure how to resolve? 我收到错误消息无法分配给GetDropDownValues,因为它是一个方法组,不确定如何解决? Can anyone shed some light? 谁能阐明一些想法? Thanks in advance... 提前致谢...

    public static DataTable GetDropDownValues(string pstrConString_Mfg, string pstrTableName, string pstrFieldName, string pstrField = "")
    {
        string strSQL;
        DataTable dtRetTable;

        try
        {
            strSQL = "SELECT DISTINCT " + pstrFieldName;

            if (pstrField != "")
            {
                strSQL = strSQL + " , " + pstrField;
            }

            strSQL = strSQL + " FROM " + pstrTableName + " ORDER BY " + pstrFieldName;

            dtRetTable = ExecuteDataTable(pstrConString_Mfg, CommandType.Text, strSQL);
            if (dtRetTable != null)
            {
                if ((dtRetTable.Rows.Count == 0))
                {
                    GetDropDownValues = new DataTable(); <--error
                }
                else
                {
                    dtRetTable.BeginLoadData();
                    dtRetTable.Rows.InsertAt(dtRetTable.NewRow, 0); 
                    dtRetTable.EndLoadData();
                    dtRetTable.AcceptChanges();
                    GetDropDownValues = dtRetTable;
                }
            }
            else
            {
                GetDropDownValues = new DataTable();
            }
        }
        catch (Exception ex)
        {
            throw ex;

        }
    }

--================================================== - ================================================ ==

Also am getting error on line ' dtRetTable.Rows.InsertAt(dtRetTable.NewRow, 0);' 行'dtRetTable.Rows.InsertAt(dtRetTable.NewRow,0);上也出现错误 cannot convert from 'method group' to 'DataRow'. 无法从“方法组”转换为“ DataRow”。 I am assuming it is related to above error? 我假设它与上述错误有关?

Where you wrote GetDropDownValues = dtRetTable; 您在哪里编写了GetDropDownValues = dtRetTable; and GetDropDownValues = new DataTable(); GetDropDownValues = new DataTable(); looks like older Visual Basic (also Visual Basic for Applications, VBA) syntax for having a function return a value. 看起来更像旧的Visual Basic(也是Visual Basic for Applications,VBA)语法,具有使函数返回值的功能。

With C# (and C, C++, Java, JavaScript and a lot of others) you need to return a value and not assign it to the function name. 使用C#(以及C,C ++,Java,JavaScript和其他许多语言),您需要return一个值,而不是将其分配给函数名称。

Do this instead: 改为这样做:

return dtRetTable;

and

new DataTable();

Edit to address your other question... 编辑以解决您的其他问题...

For the error on the line dtRetTable.Rows.InsertAt(dtRetTable.NewRow, 0) doing as @Ed Plunkett suggested will fix this problem. 对于dtRetTable.Rows.InsertAt(dtRetTable.NewRow, 0)行上的错误,建议使用@Ed Plunkett将解决此问题。 As you said you are converting older code to work in C#, it will be easy to forget to add the trailing brackets () as required by C# but the error message you get of method group - Well, I've only ever seen it when I forget to put the brackets in, so it will be easier for you to understand what is going on. 正如您说的那样,您正在转换较旧的代码以在C#中工作,很容易忘记按C#的要求添加尾括号() ,但是您从method group收到的错误消息-恩,我只在以下情况下见过它我忘了放括号,所以您会更容易了解发生了什么。

And finally, do not catch Exceptions unless you intend to do something about the exception there and then. 最后,除非打算在那儿对异常做些事情,否则不要捕获异常。 You should always catch the Exception in your Interface methods so you can show a nicer message to your users and prevent the app from crashing out. 您应该始终在Interface方法中捕获Exception,以便向用户显示更好的消息并防止应用崩溃。

Where you wrote throw ex; 你写过throw ex; , it would be better to just write throw or if you are willing, remove the try/catch block entirely. ,最好只写throw或者如果愿意,可以完全删除try / catch块。 When you throw ex you will destroy the correct StackTrace which is essential for debugging and figuring out what the problem is. 当您throw ex您将销毁正确的StackTrace ,这对于调试和弄清楚问题出在哪里至关重要。 There is lots of information (and a lot more in-depth) on this in StackOverflow as well as the rest of the Web. 在StackOverflow上以及在Web的其余部分中,有很多信息(并且有很多更深入的信息)。

Happy translating, and remember we love to help those who make an attempt to help themselves first. 祝您翻译愉快,并记住我们喜欢帮助那些先尝试自助的人。

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

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