2016-06-15 86 views
0

我有方法返回list,我想从GetAndPost方法传递该列表到Test方法..我不知道该怎么做!如何将列表数据传递给其他方法

代码:

public List<string> GetAndPost() 
    { 
     Connection(); 
     con.Open(); 

     com = new SqlCommand("SELECT Statement ", con); 
     SqlDataReader reader = com.ExecuteReader(); 
     List<string> list = new List<string>(); 
     int i = 0; 
     while (reader.Read()) 
     { 

      TXNID = reader.GetValue(0).ToString(); 
      docType = reader.GetValue(1).ToString(); 
      docNo = reader.GetValue(2).ToString(); 
      ctryIssue = reader.GetValue(3).ToString(); 
      name = reader.GetValue(4).ToString(); 
      expDate = reader.GetValue(5).ToString(); 
      citizen = reader.GetValue(6).ToString(); 
      dob = reader.GetValue(7).ToString(); 

      gender = reader.GetValue(8).ToString(); 
      mbikeNo = reader.GetValue(9).ToString(); 
      mbikeExpDate = reader.GetValue(10).ToString(); 
      branch = reader.GetValue(14).ToString(); 
      status = reader.GetValue(12).ToString(); 
      if (status == "False") 
      { 


      } 

     } 
     reader.Close(); 

     Console.WriteLine(list); 
     return list; 

    } 

    public List<string> Test() 
    { 

    } 
+4

你的列表总是空的,你只需声明它并且不添加任何东西即可。希望这是为了解释... – 3615

回答

0

你可以传递这样

Test(list); 

这是测试方法应该怎么看起来像

public void Test(List<string> list) 
{ 

} 
0
public List<string> Test(List<string> stringList) 
{ 

} 

然后就打电话给你测试方法是将你的列表传递给它,就像;

Test(list); 
0

假设我将声明一个变量来保存GetAndPost函数的输出。

var getAndPostList = GetAndPost(); 

... 
// Pass value to another function 
Test(getAndPostList); 

并为您的Test功能,它应该期待一个字符串列表

public List<string> Test(List<string> getAndPostList) 
{ 
    ... 
} 
0

您可以通过一个列表

public void somefunction(List<YourType> dates) 
{ 
} 

然而,最好是用最普通的(一般情况下,基地)接口可能,所以我会用

public void somefunction(IEnumerable<YourType> dates) 
{ 
} 

public void somefunction(ICollection<YourType> dates) 
{ 
} 
0

Test()功能应该是像这样(在情况下Test()不会有返回值):

public void Test(List<string> stringList) 
{ 

} 

在您GetAndPost()功能:

return list; 

然后致电您的Test()功能:

test(GetAndPost()); 

如果你要拨打的Test()功能withinn您GetAndPost()功能取代你return有:

test(list); 

和变化的GetAndPost()的声明:

public void GetAndPost() 
{ 
//YOUR CODE 
} 
0

Test方法应该期待一个列表参数,如果你想通过这个列表。

public List<string> Test(List<string> list) 

然后从您的GetAndPost方法的任何地方调用此方法。

Test(list); 

您不必从Test方法,即使你是期待改变列表中的内容返回一个列表。 List是一个类类型并作为参考传递。您可以编辑Test方法中的同一列表并从GetAndPost方法访问修改。

相关问题