如何传递变量名称,而不是其值。在传递之后使用它的值?

时间:2015-06-25 19:19:21

标签: c#

我想将这些字符串传递给循环,我希望变量列表splitSearchString [i]作为变量本身而不是其值传递。一旦进入循环,我想使用它的值。这是可能的还是我以错误的方式处理这个问题?感谢您帮助推出新手。 这是代码:

    string[] splitSearchString = SearchText.Split(' ');
        int i = 0;
        switch (SearchBy)
        {
            case 1:
                SearchByString = "cert_code LIKE '%" + splitSearchString[i] + "%'";
                break;
            case 2:
                SearchByString = "upc LIKE '%" + splitSearchString[i] + "%'";
                break;
            case 3:
                SearchByString = "description LIKE '%" + splitSearchString[i] + "%'";
                break;
            case 4:
                SearchByString = "(cert_code LIKE '%" + splitSearchString[i] + "%' OR upc LIKE '%" + splitSearchString[i] + "%')";
                break;
            case 5:
                SearchByString = "(cert_code LIKE '%" + splitSearchString[i] + "%' OR description LIKE '%" + splitSearchString[i] + "%')";
                break;
            case 6:
                SearchByString = "(upc LIKE '%" + splitSearchString[i] + "%' OR description LIKE '%" + splitSearchString[i] + "%')";
                break;
            default:
                SearchByString = "(upc LIKE '%" + splitSearchString[i] + "%' OR description LIKE '%" + splitSearchString[i] + "%' OR cert_code LIKE '%" + splitSearchString[i] + "%')";
                break;
        }
        SqlConnection sqlConn = new SqlConnection(connectionString);
        sqlConn.Open();
        SqlCommand sqlCmd;
            if (searchString.Contains(' ') == true && SearchExact == false)
            {
                formatedSplitSearchString = "SELECT CASE WHEN t1.longdesc IS NULL OR t1.longdesc = '' THEN t1.desc WHEN t1.longdesc IS NOT NULL AND t1.longdesc <> '' THEN t1.longdesc END AS 'description', t1.upcode FROM Product t1 LEFT JOIN Vendor t2 ON t1.vendor = t2.vendor_no LEFT JOIN Department t3 ON t1.department = t3.dept_no LEFT JOIN Section t4 on t1.section = t4.section WHERE " + SearchByString + VendorFilterStr + DeptFilterStr + SectionFilterStr;
                for (i = 0; i < splitSearchString.Length; i++)
                {
                    if (i > 0)
                    {
                        formatedSplitSearchString += " AND " + SearchByString;
                    }
                }
                formatedSplitSearchString += VendorFilterStr + DeptFilterStr + SectionFilterStr + ";";
                sqlCmd = new SqlCommand(formatedSplitSearchString, sqlConn);
            }
            else
            {
                sqlCmd = new SqlCommand(
                "SELECT CASE WHEN t1.longdesc IS NULL OR t1.longdesc = '' THEN t1.description WHEN t1.longdesc IS NOT NULL AND t1.longdesc <> '' THEN t1.longdesc END AS 'description', t1.upcode FROM Product t1 LEFT JOIN Vendor t2 ON t1.vendor = t2.vendor_no LEFT JOIN Sections t4 on t1.section = t4.section LEFT JOIN Departments t3 ON t1.department = t3.dept_no WHERE " + SearchByString + VendorFilterStr + DeptFilterStr + SectionFilterStr + ";", sqlConn);
            }
        }
        SqlDataReader sqlDr = sqlCmd.ExecuteReader();
        while (sqlDr.Read())
        {
            lvi = new ListViewItem((sqlDr["description"].ToString()));
            lvi.SubItems.Add(sqlDr["upc"].ToString());
            lviList.Add(lvi);
        }
        return lviList;

1 个答案:

答案 0 :(得分:-2)

所以,你想要做的是通过引用传递变量,这样你就可以传递变量的实际内存地址。

更好的选择是创建一个返回数组的函数,并调用该函数来设置数组。