Iterating through an array returned from a Web Service

  • Thread starter Thread starter Mark Rae
  • Start date Start date
M

Mark Rae

Hi,

I have a Web Service with a WebMethod called getRepaymentSchedule which
returns an array. Internally, the array is built up using a struct, as
follows:

public struct RepaymentSchedule
{
public int nPeriod;
public double dRepaymentAmt;
public double dInterestAmt;
}

The WebMethod is as follows:

[WebMethod]
public RepaymentSchedule[] getRepaymentSchedule(int nStartingPeriod,
double dCapital, double dRepayment, double dIntRate, int nPeriod, int nTerm)
{
RepaymentSchedule[] Repayments = null;
double dRepaymentAmt = 0; // repayment amount of monthly repayment
double dInterestAmt = 0; // interest amount of monthly repayment
int intPeriod = 0;

if(dIntRate > 1)
{
dIntRate = dIntRate / 100;
}

Repayments = new RepaymentSchedule[nPeriod];
for(intPeriod = 1; intPeriod <= nTerm; intPeriod++)
{
if(intPeriod <= nPeriod)
{
dRepaymentAmt = Microsoft.VisualBasic.Financial.PPmt(dIntRate / 12,
intPeriod, nTerm, -dCapital, 0, Microsoft.VisualBasic.DueDate.EndOfPeriod);
dRepaymentAmt = Convert.ToDouble(((dRepaymentAmt + 0.005) * 100) /
100);
dInterestAmt = dRepayment - dRepaymentAmt;
dInterestAmt = Convert.ToDouble(((dInterestAmt + 0.005) * 100) / 100);
Repayments[intPeriod -1].nPeriod = (nStartingPeriod - 1) + intPeriod;
Repayments[intPeriod -1].dRepaymentAmt = dRepaymentAmt;
Repayments[intPeriod -1].dInterestAmt = dInterestAmt;
}
}
return Repayments;
}

No problem to call this Web Service from another C# Web app or C# Windows
app, as follows:

Array aSchedule = getRepaymentSchedule(1, dCapital, dMR1, dIR1, nPeriod1,
nTerm);

That returns me an array. I can interrogate its Length, but I don't know how
to reference the individual parts of each element in the array. From the
Debug window, I can write something like:

?aSchedule.GetValue(0).nPeriod

which works, but this creates an error if I add it to the body of the code.
I'm obviously missing something totally obvious, and it's driving me crazy!

Any assistance gratefully received.

Best regards,

Mark Rae
 
When you add a web reference to your client application, VS.NET generates a
web service proxy for you. In the same namespace with the web service proxy
class there is a copy of the return type class.

In other words, if the namespace of your web service proxy is
MyWebServiceNS, then in the client you can write code like this:

// instantiate proxy
MyWebServiceNS.MyWebService wsproxy = new MyWebServiceNS.MyWebService();
// call web service
MyWebServiceNS.RepaymentSchedule[] repayments =
wsproxy.getRepaymentSchedule((1, dCapital, dMR1, dIR1, nPeriod1, nTerm);

// iterate through results
foreach (MyWebServiceNS.RepaymentSchedule repayment in repayments)
{
Console.WriteLine(repayment.nPeriod);
Console.WriteLine(repayment.dRepaymentAmt);
Console.WriteLine(repayment.dInterestAmt);
}

hth,
Sami
www.capehill.net

Mark Rae said:
Hi,

I have a Web Service with a WebMethod called getRepaymentSchedule which
returns an array. Internally, the array is built up using a struct, as
follows:

public struct RepaymentSchedule
{
public int nPeriod;
public double dRepaymentAmt;
public double dInterestAmt;
}

The WebMethod is as follows:

[WebMethod]
public RepaymentSchedule[] getRepaymentSchedule(int nStartingPeriod,
double dCapital, double dRepayment, double dIntRate, int nPeriod, int nTerm)
{
RepaymentSchedule[] Repayments = null;
double dRepaymentAmt = 0; // repayment amount of monthly repayment
double dInterestAmt = 0; // interest amount of monthly repayment
int intPeriod = 0;

if(dIntRate > 1)
{
dIntRate = dIntRate / 100;
}

Repayments = new RepaymentSchedule[nPeriod];
for(intPeriod = 1; intPeriod <= nTerm; intPeriod++)
{
if(intPeriod <= nPeriod)
{
dRepaymentAmt = Microsoft.VisualBasic.Financial.PPmt(dIntRate / 12,
intPeriod, nTerm, -dCapital, 0, Microsoft.VisualBasic.DueDate.EndOfPeriod);
dRepaymentAmt = Convert.ToDouble(((dRepaymentAmt + 0.005) * 100) /
100);
dInterestAmt = dRepayment - dRepaymentAmt;
dInterestAmt = Convert.ToDouble(((dInterestAmt + 0.005) * 100) / 100);
Repayments[intPeriod -1].nPeriod = (nStartingPeriod - 1) + intPeriod;
Repayments[intPeriod -1].dRepaymentAmt = dRepaymentAmt;
Repayments[intPeriod -1].dInterestAmt = dInterestAmt;
}
}
return Repayments;
}

No problem to call this Web Service from another C# Web app or C# Windows
app, as follows:

Array aSchedule = getRepaymentSchedule(1, dCapital, dMR1, dIR1, nPeriod1,
nTerm);

That returns me an array. I can interrogate its Length, but I don't know how
to reference the individual parts of each element in the array. From the
Debug window, I can write something like:

?aSchedule.GetValue(0).nPeriod

which works, but this creates an error if I add it to the body of the code.
I'm obviously missing something totally obvious, and it's driving me crazy!

Any assistance gratefully received.

Best regards,

Mark Rae
 
Back
Top