Apoorv Apoorv - 1 year ago 39
Vb.net Question

C# alternative to the VB code

I have this VB code

For i = 0 To OutData.NumMerids - 1
With OutData.MeridData(0)
.NumCurves = InStepFiveData.ConvexSurfaceData.MultiCurveData.NumOzCurves + InStepFiveData.ConvexSurfaceData.MultiCurveData.NumLenticularCurves
ReDim .CurveData(.NumCurves - 1)
End With

I want to generate similar C# code. I used some logic and deducted the code below.

for (i = 0; i <= OutData.NumMerids - 1; i++)
// var _with25 = OutData.MeridData[0];
OutData.MeridData[0].NumCurves =(short) (InStepFiveData.ConvexSurfaceData.MultiCurveData.NumOzCurves + InStepFiveData.ConvexSurfaceData.MultiCurveData.NumLenticularCurves);
// ERROR: Not supported in C#: ReDimStatement
Array.Resize(ref OutData.MeridData[0].CurveData, OutData.MeridData[0].NumCurves - 1);

Just want to know that am I doing something wrong ?


Answer Source

The error lies in the Array.Resize statement ; more specifically in the size given.

In VB.Net you give the last index of the array where in C# you give the length of the array
So there is a shift of 1 between the two.

ReDim .CurveData(.NumCurves - 1)
' equivalent to
ReDim .CurveData(0 To .NumCurves - 1) ' length is (.NumCurves - 1) - 0 + 1 = .NumCurves

This comes from the fact that in VB6 you could have an array indexed in other base than 0 (notably the 1 indexed collection) and so that was valid (it isn't in VB.Net)

Dim someArray(-4 To 5) As Integer ' declares an array of 10 integer indexed from -4 to 5

Back to your code you just need to give the size

//Array.Resize(ref OutData.MeridData[0].CurveData, OutData.MeridData[0].NumCurves - 1);
// replaced by
Array.Resize(ref OutData.MeridData[0].CurveData, OutData.MeridData[0].NumCurves);

As for the comment given by Ashwin Nair ; ReDim has the same behaviour (I wouldn't be surprise ReDim uses Array.Resize internally in .Net)