Apoorv Apoorv - 5 months ago 25
Vb.net Question

Redim Statement alternative in C#- Code giving errors

I know there is no

Redim
Statement in C#. I had this old code written in VB and I converted it into C# using Telerik Tool. Now the online tool gave some comments when it encountered
Redim
statement. The VB code and the C# code is given below.

Public Function ReadMultiCurvedDataFromFile(ByRef filenumber As Short) As ConvexMultiCurveDataType
Dim i As Short
Dim OutData As ConvexMultiCurveDataType = InitConvexMultiCurveDataType()
Try
With OutData
FileGet(filenumber, .BlendFactor)
FileGet(filenumber, .EdgeRadius)
FileGet(filenumber, .OZShapeFactor)
FileGet(filenumber, .OZPrismOffset)
FileGet(filenumber, .NumOzCurves)
If .NumOzCurves > 0 Then
ReDim .OZCurveData(.NumOzCurves - 1)
For i = 0 To .NumOzCurves - 1
FileGet(filenumber, .OZCurveData(i))
Next
End If
FileGet(filenumber, .NumLenticularCurves)
If .NumLenticularCurves > 0 Then
ReDim .CurveData(.NumLenticularCurves - 1)
For i = 0 To .NumLenticularCurves - 1
FileGet(filenumber, .CurveData(i))
Next
End If
End With
Catch ex As Exception

End Try
Return OutData
End Function


C# code

public static Mold_Power_Suite.Model.ConvexSurfaceStructures.ConvexMultiCurveDataType ReadMultiCurvedDataFromFile(ref short filenumber)
{
int filenumber1 = filenumber;
long i = 0;
Mold_Power_Suite.Model.ConvexSurfaceStructures.ConvexMultiCurveDataType OutData =ConvexSurfaceStructures. InitConvexMultiCurveDataType();
try
{
var _with30 = OutData;
FileSystem.FileGet(filenumber,ref _with30.BlendFactor);
FileSystem.FileGet(filenumber,ref _with30.EdgeRadius);
FileSystem.FileGet(filenumber,ref _with30.OZShapeFactor);
FileSystem.FileGet(filenumber, ref _with30.OZPrismOffset);
FileSystem.FileGet(filenumber,ref _with30.NumOzCurves);

if (_with30.NumOzCurves > 0)
{
//Array.Resize();
// ERROR: Not supported in C#: ReDimStatement

for ( i = 0; i <= _with30.NumOzCurves - 1; i++)
{

FileSystem.FileGet(filenumber1, ref _with30.OZCurveData[i]); // GETTING MENTIONED ERROR HERE
}
}
FileSystem.FileGet(filenumber,ref _with30.NumLenticularCurves);
if (_with30.NumLenticularCurves > 0)
{
// ERROR: Not supported in C#: ReDimStatement

for (i = 0; i <= _with30.NumLenticularCurves - 1; i++)
{
FileSystem.FileGet(filenumber1, ref _with30.CurveData[i]); //GETTING MENTIONED ERROR HERE
}
}

}
catch (Exception ex)
{
}
return OutData;
}


OZCurveData has been defined as

Dim OZCurveData() As ConvexOZCurveDataType


and ConvexOZCurveDataType as

Public Structure ConvexOZCurveDataType
Dim SphereRadius As Double
Dim CylRadius As Double
Dim Diameter As Double
Dim Addition As Double
End Structure


I am getting error at the mentioned lines. The errors is


The best overloaded method match for 'Microsoft.VisualBasic.FileSystem.FileGet(int, ref System.DateTime, long)' has some invalid arguments


Redim
is also an issue.

Answer

VB.NET is "permissive", so you are allowed to pass ByRef a derived type of the expected one. In our case, in VB FileGet(Integer, ByRef ValueType) gets called. C# doesn't allow this, so you'll need to create a temporary object of type ValueType and assign to it OZCurveData/CurveData, then pass it to FileGet and then assign the value of the temporary object to OZCurveData/CurveData. Eg:

ValueType vObj = _with30.OZCurveData[i];
Microsoft.VisualBasic.FileSystem.FileGet(1, ref vObj);
_with30.OZCurveData[i] = (ConvexOZCurveDataType)vObj ;

About Redim, you can use Array.Resize (as @Gaurav Singh Jantwal said):

Array.Resize(ref _with30.OZCurveData, _with30.NumOzCurves - 1);
Comments