简体   繁体   中英

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? 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);' cannot convert from 'method group' to 'DataRow'. I am assuming it is related to above error?

Where you wrote GetDropDownValues = dtRetTable; and GetDropDownValues = new DataTable(); looks like older Visual Basic (also Visual Basic for Applications, VBA) syntax for having a function return a value.

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.

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. 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.

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.

Where you wrote throw ex; , it would be better to just write throw or if you are willing, remove the try/catch block entirely. When you throw ex you will destroy the correct StackTrace which is essential for debugging and figuring out what the problem is. There is lots of information (and a lot more in-depth) on this in StackOverflow as well as the rest of the Web.

Happy translating, and remember we love to help those who make an attempt to help themselves first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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