private void GetQuerySql(out string strSql)
        {
            switch (auditType.ToLower())
            {
                case "auditdept":
                    if (appUsers.AuditRole == 0 || appUsers.AuditRole == 1)
                    {
                        strSql = " where applydept='" + appUsers.UserDept + "' and (applystatus<>0 or (applystatus=0 and auditdept=2))";
                    }
                    else if (appUsers.AuditRole == 2)
                    {
                        strSql = " where applygroup=2 and (applystatus<>0 or (applystatus=0 and auditdept=2))";
                    }
                    else if (appUsers.AuditRole == 3)
                    {
                        strSql = " where applygroup=1 and (applystatus<>0 or (applystatus=0 and auditdept=2))";
                    }
                    else if (appUsers.AuditRole == 4)
                    {
                        strSql = " where applystatus<>0 or (applystatus=0 and auditdept=2) ";
                    }
                    else if (appUsers.AuditRole == 5)
                    {
                        strSql = " where applystatus<>0 or (applystatus=0 and auditdept=2)";
                    }
                    else
                    {
                        strSql = " where 1=2";
                    }
                    break;
                case "auditmanager":
                    if (appUsers.AuditRole == 2)
                    {
                        strSql = " where applygroup=2 and (( applystatus <>0 and applystatus<>1) or (applystatus=0 and auditmanager=2)) ";
                    }
                    else if (appUsers.AuditRole == 3)
                    {
                        strSql = " where applygroup=1 and (( applystatus <>0 and applystatus<>1) or (applystatus=0 and auditmanager=2)) ";
                    }
                    else if (appUsers.AuditRole == 4)
                    {
                        strSql = " where ( applystatus <>0 and applystatus<>1) or (applystatus=0 and auditmanager=2)) ";
                   }
                    else if (appUsers.AuditRole == 5)
                    {
                        strSql = " where ( applystatus <>0 and applystatus<>1) or (applystatus=0 and auditmanager=2)) ";
                    }
                    else
                    {
                        strSql = " where 1=2";
                    }
                    break;
                case "auditleader":
                    if (appUsers.AuditRole == 4)
                    {
                        strSql = " where (applystatus<>0 and applystatus<>1 and applystatus<>2)  or (applystatsu=0 and auditleader=2) ";
                    }
                    else if (appUsers.AuditRole == 5)
                    {
                        strSql = " where (applystatus<>0 and applystatus<>1 and applystatus<>2)  or (applystatsu=0 and auditleader=2) ";
                    }
                    else
                    {
                        strSql = " where 1=2";
                    }
                    break;
               
                }
        }
程序編譯的時候出錯,錯誤提示:“控制離開當前方法之前必須對out參數“strSql”賦值”。

解决方案 »

  1.   

    switch (auditType.ToLower()) 之前加上:
    strSql="";
      

  2.   

    在方法开始第一行定义:strSql=null
    试试看!
      

  3.   

    在方法开始第一行定义:strSql=""
    试试看!
      

  4.   

    問題已經找到,寫在帶返回參數函數中的switch,必須帶上default。
      

  5.   

    out string strSql
    ---------
    你用了out这个标识,说明strSql在离开本函数时必须给他赋值。而你的switch 最后没有default项,也就是说如果没有对应的项,那strSql就没有得到赋值,所以无法编译,解决此问题的方法有几个,我说几个:
    1、在GetQuerySql刚开始,也就是switch之前你就给strSql="";这样赋值。
    2、你为switch增加一个default项,在这项里给strSql赋值。
    3、你不要用out,改用ref(但ref必须在外面先赋值)。把函数的参数out string strSql改为ref string strSql
      

  6.   

    記住了,謝謝!剛轉向.NET,有不少地方還是不了解,還請兄才你多多指教。