Start New Discussion within our Software Development Community

I got an error in ths code...
cannot implicitly convert type string to string[]....

public string[] SelectTable(String pSqlstmt)
        {
            string[] sTable;
            string str;
            if(pSqlstmt.Contains("from"))
            {
                int strlen=pSqlstmt.Length;
                int strindfrom = pSqlstmt.IndexOf("from");
                if (pSqlstmt.Contains("where"))
                {
                    int strindwhere = pSqlstmt.IndexOf("where");
                    sTable = pSqlstmt.Substring(strindfrom, strindwhere);
                 }
                else
                {
                    sTable = pSqlstmt.Substring(strindfrom, strlen);
                    
                }
                
            }
            return sTable;
        }

Consider this modification of your code.

public string[] SelectTable(String pSqlstmt)
        {
            string str = null; // you declared this variable before but did not use it
            if (pSqlstmt.Contains("from"))
            {
                int strlen = pSqlstmt.Length;
                int strindfrom = pSqlstmt.IndexOf("from");
                if (pSqlstmt.Contains("where"))
                {
                    int strindwhere = pSqlstmt.IndexOf("where");
                    str = pSqlstmt.Substring(strindfrom, strindwhere);
                }
                else
                {
                    str = pSqlstmt.Substring(strindfrom, strlen);
                }
            }

            if (!string.IsNullOrEmpty(str))
                return new string[] { str };

            return null;
        }

On that note, you have no else clause for the statement if (pSqlstmt.Contains("from")) , so your function may return nothing useful. I assume you're prepared for that.

This article has been dead for over six months. Start a new discussion instead.