C#中的Redim Statement替代方法 - 代码给出错误

时间:2016-06-30 07:16:19

标签: c# vb.net winforms

我知道C#中没有Redim语句。我用VB编写了这个旧代码,然后使用Telerik Tool将其转换为C#。现在,在遇到Redim语句时,在线工具发表了一些评论。 VB代码和C#代码如下。

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#代码

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 已被定义为

 Dim OZCurveData() As ConvexOZCurveDataType

ConvexOZCurveDataType

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

我在上述行中遇到错误。错误是

  

最佳重载方法匹配&#39; Microsoft.VisualBasic.FileSystem.FileGet(int,ref System.DateTime,long)&#39;有一些无效的论点

Redim也是一个问题。

1 个答案:

答案 0 :(得分:0)

VB.NET是“宽容的”,因此您可以传递ByRef预期类型的​​派生类型。在我们的例子中,在VB中FileGet(Integer, ByRef ValueType)被调用。 C#不允许这样做,因此您需要创建ValueType类型的临时对象并为其分配OZCurveData / CurveData,然后将其传递给{{ 1}}然后将临时对象的值分配给FileGet / OZCurveData。例如:

CurveData

关于ValueType vObj = _with30.OZCurveData[i]; Microsoft.VisualBasic.FileSystem.FileGet(1, ref vObj); _with30.OZCurveData[i] = (ConvexOZCurveDataType)vObj ; ,您可以使用Redim(正如@Gaurav Singh Jantwal所说):

Array.Resize