我定义了一个函数,如下:
                public string[]GetGF(string str)
{
Qryzhanzhan="select Line_ID  from Busline where Stop_Name ="+"'"+str+"'"; 
Qrycnn=new SqlConnection(this.Qryzhanzhanstr); 
this.Qrycnn.Open(); 
SqlCommand cmd=new SqlCommand(Qryzhanzhan,Qrycnn);   
SqlDataReader reader=cmd.ExecuteReader(); 
        int o = 0; 
string[] gf=new string[100];
while(reader.Read())  

gf[o]=reader["Line_ID"].ToString(); o++; 

this.Qrycnn.Close();
return gf;
 }
我在按键里调用时:
               string[] cf=new string[100];int g;
     cf=GetGF(this.textBox2.Text.ToString().Trim());
这样应该把上面所有读取的字符串都放到cf中了吧?但是怎么读取cf的长度,函数只是返回这个字符串组,没有返回长度是多少?请高手帮忙一下

解决方案 »

  1.   

    string []cf=GetGF(this.textBox2.Text.ToString().Trim()); 
      

  2.   

    由于你定了数组的长度string[] gf=new string[100]; return gf;所以cf.Length只会是100.
    建议你使用ArrayListpublic string[]GetGF(string str) 

    Qryzhanzhan="select Line_ID  from Busline where Stop_Name ="+"'"+str+"'";  
    Qrycnn=new SqlConnection(this.Qryzhanzhanstr);  
    this.Qrycnn.Open();  
    SqlCommand cmd=new SqlCommand(Qryzhanzhan,Qrycnn);    
    SqlDataReader reader=cmd.ExecuteReader();  
            int o = 0;  
    ArrayList gf=new ArrayList[100]; 
    while(reader.Read())   
    {  
    gf.Add(reader["Line_ID"].ToString()); //把要的东西add进去,gf.Count会自动累加
    }  
    this.Qrycnn.Close(); 
    return gf; 
     } 
    ArrayList cf=GetGF(this.textBox2.Text.ToString().Trim()); 
    cf.Count 就是你要的东西了。获取每一个得到字符串是:cf[i].ToString()或(string)cf[i]
      

  3.   

    用list<string>进行返回 最后取它的count
      

  4.   

    MSDN上很详细 呵呵
      exp:  using System.Collections.Generic;
      list<string> myStrList = new list<string>();
      for(int i=0;i<10;i++)
         {
           myStrList.Add(i.tostring());
         }
      int i = myStrList.Count;
      
      

  5.   


    public List<string> GetGF(string str)  
    {  
    Qryzhanzhan="select Line_ID  from Busline where Stop_Name ="+"'"+str+"'";   
    Qrycnn=new SqlConnection(this.Qryzhanzhanstr);   
    this.Qrycnn.Open();   
    SqlCommand cmd=new SqlCommand(Qryzhanzhan,Qrycnn);     
    SqlDataReader reader=cmd.ExecuteReader();   
            int o = 0;   
    List<string> gf=new List<string>();
    while(reader.Read())    
    {   
    gf.Add(reader["Line_ID"].ToString()); //把要的东西add进去,gf.Count会自动累加 
    }   
    this.Qrycnn.Close();  
    return gf;  
     }  
    ArrayList cf=GetGF(this.textBox2.Text.ToString().Trim());  
      

  6.   

    5楼的高手,怎么提示说“System.Arry并不包含对Add的定义啊?根本就没有Add啊?
      

  7.   

    tianrui456 ,我加了 "using System.Collections.Generic;“,但是<string>这样的格式是非法的啊,系统根本读不了"<>",是不是还要调用系统的什么啊?
      

  8.   

    不用啊  list<T>就是System.Collections.Generic命名空间下的不用加什么特别的东西
      
      

  9.   


    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Collections.Generic;
    public List<string> GetVehicleList(string customerID)
        {
            string sqlStr;
            SqlDataReader Reader;
            List<string> vehicleList = new List<string>();
            string currVehicle;        DBCN = new SqlConnection(SqlConnectStr);
            sqlStr = "select VehI_VehicleNO from Tab_VehicleInfo where VehI_CustomerID =" + customerID;        try
            {
                DBCN.Open();
                CMD = new SqlCommand();
                CMD.CommandText = sqlStr;
                CMD.Connection = DBCN;
                CMD.CommandType = CommandType.Text;            Reader = CMD.ExecuteReader();
                CMD.Dispose();
                while (Reader.Read())
                {
                    currVehicle =SQLHelper.NotNull(Reader["VehI_VehicleNO"]);
                    vehicleList.Add(currVehicle);
                }
            }
            finally
            {
                DBCN.Close();
            }
            return vehicleList;    }
      

  10.   

    你这个是不是.NET 2005的啊?我是2003的,怎么就不能用“using System.Collections.Generic;“,而且系统自动生成了”using System.Collections;“了!!
      

  11.   

    ....... 我的就是2005的
    注意:此类在 .NET Framework 2.0 版中是新增的。 
    表示可通过索引访问的对象的强类型列表。提供用于对列表进行搜索、排序和操作的方法。 命名空间:System.Collections.Generic
    程序集:mscorlib(在 mscorlib.dll 中)
      

  12.   

    用5楼的 把ArrayList gf=new ArrayList[100];  
    改成 ArrayList gf=new ArrayList();  就行了 然后取它的Count
      

  13.   

    什么都不用改,它的命名空间是:
    using System.Collections;写上就行了。
      

  14.   

    string []cf=GetGF(this.textBox2.Text.ToString().Trim());
    int len = cf.Length; 
      

  15.   

    其实你把鼠标点一下类 ArrayList ,最后一个字符 t 的下面的个东西,点它会出来两个选项,一个是帮你using,一个是帮你写上:System.Collections.ArrayList list=....
      

  16.   

    噢,方法头忘了改,string[]换成ArryList还有如21楼所说。我只顾着替换方法体的string[]。
      

  17.   

    首先,谢谢五楼,虽然你的代码不是很正确,但是给我指明方向了,其次谢谢tianrui456 ,你太热心了,最后也是你的提醒,我把结果弄出来了,最后谢谢后面参与的高手们,谢谢了!!!