我只是试图循环所有
结果对象中的项[x](我自己返回此对象的函数,它基本上只返回一个字符串列表)。这来自我为Web服务目的创建的vb.net函数。我认为做一个foreach循环会更容易做到最有效但我不能这样做,因为我的类没有实现IEnumerable。但是我想知道是否有更简单的方法来循环这个而不使用iEnumerable。 非常感谢您的帮助。 这就是这个类看起来像是通过我的函数返回的。
Public Class GetErrors
Public FWWDIR_ERR_VENDOR As String = String.Empty
Public FWWDIR_ERR_INVOICE As String = String.Empty
Public FWWDIR_ERR_ACCOUNT As String = String.Empty
Public FWWDIR_ERR_FUND As String = String.Empty
Public FWWDIR_ERR_ORGN As String = String.Empty
Public FWWDIR_ERR_PROG As String = String.Empty
Public FWWDIR_ERR_ADDRSS_TYP As String = String.Empty
Public FWWDIR_ERR_ADDRSS_SEQ As String = String.Empty
Public FWWDIR_ERR_LOCATION As String = String.Empty
Public FWWDIR_ERR_ACTIVITY As String = String.Empty
Public FWWDIR_ROW As String = String.Empty
Public service_error As String = String.Empty
Public FWWDIR_ERR_PO As String = String.Empty
Public FWWDIR_ERR_CHKVEND As String = String.Empty
Public FWWDIR_ERR_FSYR As String = String.Empty
Public FWWDIR_ERR_FSPD As String = String.Empty
Public FWWDIR_ERR_ONTM_VEND As String = String.Empty
Public FWWDIR_ERR_ONTM_ADRS1 As String = String.Empty
Public FWWDIR_ERR_ONTM_CITY As String = String.Empty
Public FWWDIR_ERR_ONTM_STATE As String = String.Empty
Public FWWDIR_ERR_ONTM_ZIP As String = String.Empty
Public FWWDIR_ERR_ENCUM_AMT As String = String.Empty
Public FWWDIR_ERR_ENCUM_VEND As String = String.Empty
Public FWWDIR_ERR_ITEM_ERROR As String = String.Empty
Public FWWDIR_POHD As String = String.Empty
End Class
我试过的短代码snipet。
ServiceApUtils _apUtils = new ServiceApUtils();
var results = _apUtils.ADP_Processor(l_taxcode.ToArray(), l_amount.ToArray(), l_row.ToArray());
//Response.Write(results[0].)
if (results[0].service_error!="")
{
Response.Write(results[0].service_error);
}
else if (results.Count > 0)//
{
for (int x = 0; x < results.Count; x++ ) //typecast x a get error to loop the results
{
TableRow tr = new TableRow();
Table1.Rows.Add(tr);
foreach (string value in results[x])//fails here with Ienumerable error
{
TableCell tc = new TableCell();
tc.Text = results[x].FWWDIR_ROW + results[x].FWWDIR_ERR_INVOICE + results[x].FWWDIR_ERR_ACCOUNT;
tr.Cells.Add(tc);// = new TableCell();
}
}
}
无论我尝试iEnumerable错误或者我根本不知道如何在每个数组项内循环,我都会收到错误。
使用类作为返回的函数正在执行此操作
Public Function ADP_Processor(
ByVal w_taxcode As String(), _
ByVal w_Amount As String(), _
ByVal w_row As String()) As List(Of GetErrors)
答案 0 :(得分:2)
尝试删除你的foreach并拥有:
for (int x = 0; x < results.Count; x++ ) //typecast x a get error to loop the results
{
TableRow tr = new TableRow();
Table1.Rows.Add(tr);
GetErrors value = results[x];
TableCell tc = new TableCell();
tc.Text = results[x].FWWDIR_ROW + results[x].FWWDIR_ERR_INVOICE + results[x].FWWDIR_ERR_ACCOUNT;
tr.Cells.Add(tc);// = new TableCell();
}
看来你正在尝试使用类似方法的类,因此像字符串数组一样使用字段。 “results”中的每个项目都是GetErrors类的一个实例,除非您实现IEnumerable并让GetEnumerator返回每个字段中的值,否则不能在foreach中使用它。
答案 1 :(得分:2)
我认为问题出现在你的循环中:
foreach (string value in results[x])//fails here with Ienumerable error
{
TableCell tc = new TableCell();
tc.Text = results[x].FWWDIR_ROW + results[x].FWWDIR_ERR_INVOICE + results[x].FWWDIR_ERR_ACCOUNT;
tr.Cells.Add(tc);// = new TableCell();
}
你正在循环结果集的数组,但是你不能对结果[x]做一个foreach,因为foreach要求结果[x]是一个不可数的。基本上如果你要写:
foreach (string value in results){
//logic here
}
然后value获取存储在结果集的一个实例中的所有值。现在该值是您的字符串数组,您可以使用简单的for循环遍历值,请参阅下文:
ServiceApUtils _apUtils = new ServiceApUtils();
var results = _apUtils.ADP_Processor(l_taxcode.ToArray(), l_amount.ToArray(), l_row.ToArray());
//Response.Write(results[0].)
if (results[0].service_error!="")
{
Response.Write(results[0].service_error);
}//if the procedure has service errors
else if (results.Count > 0)//
{
foreach (var value in results){
TableRow tr = new TableRow();
Table1.Rows.Add(tr);
for (int i = 0; i < value.Length; i++{
//might have to update this logic as well:
TableCell tc = new TableCell();
tc.Text = value.FWWDIR_ROW + value.FWWDIR_ERR_INVOICE + value.FWWDIR_ERR_ACCOUNT;
tr.Cells.Add(tc);// = new TableCell();
}
}