Reputation: 2043
I have this VB code
Try
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
Next
...
I want to generate similar C# code. I used some logic and deducted the code below.
try
{
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 ?
Thanks
Upvotes: 4
Views: 188
Reputation: 8665
A few things you can correct/improve:
C# for
loops require you to declare the loop variable.
for(var i = 0; i < 100; i++) {
This might not be an issue if you've already declared i
somewhere above, but I would recommend keeping the iterator variable limited to the scope of the loop if possible.
As a substitution for VB's With
I would use a local variable. (I believe this is what With
actual does behind the scenes.) It looks like you commented that out for some reason. Every time you reference OutData.MeridData[0]
or InStepFiveData.ConvexSurfaceData.MultiCurveData
you have to deference several objects, and depending on the structure ofthose objects that could be inefficient. Plus it's hard to read.
@Dave Doknjas got the Redim
substitution part right, you just need to use a constructor. Redim
actually throws out the old array and creates a new one, so its name is actually a bit misleading. To Redim Preserve
you would need to use Array.Copy
to move values from the old array to the new one.
try {
for (var i = 0; i <= OutData.NumMerids - 1; i++) {
var meridData = OutData.MeridData[0];
var curveData = InStepFiveData.ConvexSurfaceData.MultiCurveData;
meridData.NumCurves = (short)(curveData.NumOzCurves + curveData.NumLenticularCurves);
meridData.CurveData = new Foo[meridData.NumCurves];
}
}
Upvotes: 1
Reputation: 5049
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)
As Dave said in it's answer Array.Resize is the translation of Redim Preserve
not Redim
(which wipes the data away)
Upvotes: 2
Reputation: 6542
You (and the accepted answer) are confusing 'ReDim' with 'ReDim Preserve'. Your 'ReDim' statement is just converted using a 'new' statement to create the array:
OutData.MeridData[0].CurveData = new Foo[OutData.MeridData[0].NumCurves];
(The type of 'CurveData' is unknown to me, so I used 'Foo' - you'll have to replace with the appropriate type).
Upvotes: 2