Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GetEnumerator error

Status
Not open for further replies.

Rowse

Technical User
Dec 20, 2002
62
GB

Not sure if this is the right place for this

I'm creating a webservice in c# for asp.net v2, and using this line:

foreach (WS_RentalStreamRecord WSR in Payments)

But am getting this error:

foreach statement cannot operate on variables of type 'Service.WS_RentalStreamRecord' because 'Service.WS_RentalStreamRecord' does not contain a public definition for 'GetEnumerator'


What am I doing wrong?
 
foreach requires an IEnumerable or IEnumerable<T> implementation. [tt]Payments[/tt] doesn't implement either of these interfaces.

the easiest way to enumerate objects is using an IList<T>, DataTable, or ArrayList. You can also create your own custom enumerations for objects.

If you post a more complete code listing we may be able to help.



Jason Meckley
Programmer
Specialty Bakers, Inc.
 
here's the code exerts:

public struct WS_RentalStreamRecord
{
public double Amount;
}

public ServerDocResult DocumentSvc1(WS_RentalStreamRecord Payments)
{
foreach (WS_RentalStreamRecord WSR in Payments)
{

}}

that make sense? don't want to post it all
 
Rowse said:
don't want to post it all
if you're going to ask for help you have to supply relevant information.

how is the service being called? whereever your calling DocumentSvc1 from it's not passing a list of WS_RentalStreamRecords.



Jason Meckley
Programmer
Specialty Bakers, Inc.
 
here's the code:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using QuoteSystem;
using System.Net.Mail;
using System.Xml;
using System.Text;
using System.IO;

[WebService(Namespace = "]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service()
{
//Dev Web Location for test http:\\
//Uncomment the following line if using designed components
//InitializeComponent();
}

ServerDocResult sdr;
object unit;
object extend;

public struct WS_RentalStreamRecord
{
public double Amount;
public bool Capitalised;
public int CashFlowType;
public bool Collected;
public double CommissionAmount;
public double CommissionPercentage;
public int DescriptionCode;
public DateTime EndDate;
public int Frequency;
public bool IncludeApr;
public bool IncludeRate;
public bool IncludeRate1;
public bool IncludeRate2;
public bool IncludeRate3;
public bool IncludeRate4;
public bool IncludeRate5;
public bool IncludeRate6;
public bool InternalOnly;
public int NumberOfPayments;
public int RefNum;
public DateTime StartDate;
public double TotalAmount;
public double TotalCommission;
public bool Valid;

}

[WebMethod]
public ServerDocResult DocumentSvc1(WS_RentalStreamRecord Payments)
{
try
{
foreach (WS_RentalStreamRecord WSR in Payments)
{
}
}


catch (Exception ex) {
}
}

}
 
Code:
public ServerDocResult DocumentSvc1([COLOR=red]WS_RentalStreamRecord[/color] Payments)
{
   try
   {
      foreach (WS_RentalStreamRecord WSR in Payments)
      {
      }
   }
   catch (Exception ex) 
   {
   }
}
this object is not ienumerable. instead it should read
[tt]IList<WS_RentalStreamRecord> Payments[/tt].
This will also require you to update the calling methods as well. they will need to pass a list of WS_RentalStreamRecords.
There are a couple basic and specialize objects that implement Ilist<>. List<> is the simplest.

your other option is to implment the IEnumerable Interface on the WS_RentalStreamRecord object. However this might not make sense as WS_RentalStreamRecord represents excatly 1 record.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top