Reputation: 83
I have the following code in VB6:
Dim frpdReport() As REPORTDEF
For iCounter = 0 To UBound(frpdReport)
With frpdReport(iCounter)
If .iReportID = iReportID Then
fGetReportFile = .tReportFile
End If
End With
Next iCounter
And I converted to this C# code:
REPORTDEF[] frpdReport = new REPORTDEF[6];
for (iCounter = 0; iCounter < Convert.ToInt32(frpdReport[6]); iCounter++)
{
if (frpdReport[iCounter].iReportID == iReportID)
{
fGetReportFile_return = frpdReport[iCounter].tReportFile;
}
}
return fGetReportFile_return;
When debugging I get the following error in the for Statement - "Index was outside the bounds of the array." And I cannot figure out why since the index of the array is 6.
Any help please?
Upvotes: 3
Views: 3009
Reputation: 2463
Why not use the .length property?
for (iCounter = 0; iCounter < frpdReport.Length; iCounter++)
or if you don't need the counter value, a for each
foreach (REPORTDEF frpReportItem in frpdReport)
Or if you are looking for a specific item, use LINQ
REPORTDEF fGetReportFile_return = frpdReport.Where( fR => fR.iReportID == iReportID).Single();
Upvotes: 5
Reputation: 24263
You can get the length of an array using arrayName.Length
.
for (iCounter = 0; iCounter < frpdReport.Length; iCounter++)
{
if (frpdReport[iCounter].iReportID == iReportID)
{
fGetReportFile_return = frpdReport[iCounter].tReportFile;
}
}
return fGetReportFile_return;
Or the simple foreach
construct:
foreach (REPORTDEF frpdReportItem in frpdReport)
{
if (frpdReportItem.iReportID == iReportID)
{
fGetReportFile_return = frpdReportItem.tReportFile;
}
}
return fGetReportFile_return;
Upvotes: 2
Reputation: 75296
This line is wrong:
for (iCounter = 0; iCounter < Convert.ToInt32(frpdReport[6]); iCounter++)
It should be:
for (iCounter = 0; iCounter < 6; iCounter++)
frpdReport
was defined as a six-element array; in C#, arrays are zero-based, so frpdReport[6]
will generate an error, since there are only elements 0 through 5.
Upvotes: 1
Reputation: 78155
REPORTDEF[6]
defines 6 elements, from 0 to 5.
Convert.ToInt32(frpdReport[6])
does not seem to make sense at all. You are converting a structure to a number.
Upvotes: 0