0

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.

Apoorv
  • 2,023
  • 1
  • 19
  • 42
  • See [this](http://stackoverflow.com/q/967423/1997232) regarding redim. – Sinatr Jun 30 '16 at 07:18
  • how did you get that background color under redim word ? – Apoorv Jun 30 '16 at 07:24
  • Usage `Array.Resize(ref array, new size);` – G J Jun 30 '16 at 07:28
  • What is the type of `_with30.CurveData[i]`? And the one of `_with30.OZCurveData[i]`? – Matteo Umili Jun 30 '16 at 07:58
  • public ConvexCurveDataType[] CurveData; public ConvexOZCurveDataType[] OZCurveData; – Apoorv Jun 30 '16 at 08:03
  • @Apoorv So how is it possible that the VB worked? I mean, also in VB.NET code the datatypes were the same, didn't they? And AFAIK there is no overload of FileGet that accepts `ConvexCurveDataType` or `ConvexOZCurveDataType` – Matteo Umili Jun 30 '16 at 08:06
  • thats what I dont understand . – Apoorv Jun 30 '16 at 08:07
  • @Apoorv `ConvexCurveDataType` and `ConvexOZCurveDataType` are structure or classes? – Matteo Umili Jun 30 '16 at 08:14
  • 1
    The *exact* VB definition of CurveData (all the way down to the primitive data types it emcompasses) is the essence of your problem (much more relevant than the lines of code you posted). Please include them in your problem statement, don't make the commenters pull this information out one question at the time. – Paul-Jan Jun 30 '16 at 08:20
  • @MatteoUmili just saw your comment. They are structures. – Apoorv Jun 30 '16 at 08:40
  • `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 ;` – Matteo Umili Jun 30 '16 at 08:45
  • @MatteoUmili it would be great if you could write is as an answer to my question ? – Apoorv Jun 30 '16 at 08:48
  • _with30.OZCurveData = new ConvexOZCurveDataType[_with30.NumOzCurves]; You are wasting your time rewriting this in C#, it does *not* get any better and is quite unnecessary when you can simply add a reference to the assembly. – Hans Passant Jun 30 '16 at 08:54
  • @HansPassant sir can you elaborate as an answer to my question ? – Apoorv Jun 30 '16 at 09:00

1 Answers1

0

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);
Matteo Umili
  • 3,412
  • 1
  • 19
  • 31
  • do I need this line _with30.OZCurveData[i] = (ConvexOZCurveDataType)vObj ; ? do I have to put it after FileSystem.FileGet() , inside for loop ? – Apoorv Jun 30 '16 at 08:58
  • Yes, if you don't do that, `_with30.OZCurveData[i]` value won't change – Matteo Umili Jun 30 '16 at 09:02