0

I have this VB code

 Public Function InitJobTicketConcaveItemsType() As JobTicketConcaveItemsType
        Dim OutData As JobTicketConcaveItemsType
        With OutData
            .NumJobItems = 1
            ReDim .JobItems(.NumJobItems - 1)
            .JobItems(0) = JobDataConcaveEnum.JDBDryData
        End With
        Return OutData
    End Function

The converted C# code

public static JobTicketConcaveItemsType InitJobTicketConcaveItemsType()
        {
            JobTicketConcaveItemsType OutData = default(JobTicketConcaveItemsType);
            var _with25 = OutData;

            _with25.NumJobItems = 1;
            // ERROR: Not supported in C#: ReDimStatement - replaced with the statement below
            Array.Resize(ref _with25.JobItems, _with25.NumJobItems - 1);

            _with25.JobItems[0] = FrontEndEnums.JobDataConcaveEnum.JDBDryData;
            return OutData;
        }

When I try to run the application , I get a the Error IndexOutOfRangeException was unhandled. I have made sure to use Array.Resize() to reallocate the array

The code in VB doesn´t give errors. Any clues ?

JDBDryData has been defined as below

public enum JobDataConcaveEnum
        {
            JDBWetData = 0,
            JDBDryData,
            JDBWetCylinder,
            JDBAxis
        }

I get the error at the statement

_with25.JobItems[0]=FrontEndEnums.JobDataConcaveEnum.JDBDryData;
Apoorv
  • 2,023
  • 1
  • 19
  • 42

5 Answers5

1

Why you resize the _with25.JobItems array to size 0 in following line of your code.

Array.Resize(ref _with25.JobItems, _with25.NumJobItems - 1);

then what happen is your array size become zero. when it comes to line

_with25.JobItems[0] = FrontEndEnums.JobDataConcaveEnum.JDBDryData;

which you get the ArrayIndexOutOfBound exception, you can see, you are trying to set a value to zero'th element of an array where there is no elements at all(array of zero size)

remove the Array.Resize(ref _with25.JobItems, _with25.NumJobItems - 1); Or edit the same line as follows Array.Resize(ref _with25.JobItems, _with25.NumJobItems); and see.

Dehan Wjiesekara
  • 3,152
  • 3
  • 32
  • 46
1

In VB array declarations, you specify the last valid index:

'Declare a single-dimension array of 5 values
Dim numbers(4) As Integer 

In C# array declarations, you specify the length of the array.

That means that you should always add 1 when converting a VB array declaration to a C# one. So, simply, don't subtract that 1:

Array.Resize(ref _with25.JobItems, _with25.NumJobItems);
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
1

The problem is that you cannot resize array to size 0, as other answers mentioned already. Also, you need to know that Array size declared in VBscript is 1 more than C#. So make sure that you provide the proper size. Therefore, in your case you cannot blatantly convert _with25.NumJobItems - 1 from VBScript to C# without adjusting the number.

Regarding Array size in VBScript and C#:

  • VB script:

    Dim arr(5)

    ' Size is 6, and you can access them by arr(0), arr(1), ... arr(5)

  • C#

    MyType[] arr = new MyType[5];

    // Size is 5, and you can access them by arr[0], arr[1], ... arr[4]

kurakura88
  • 2,185
  • 2
  • 12
  • 18
0

You won´t need to use Array.Resizeat all, simply copy the content from the existing array into a new one and store this into the original one using Array.CopyTo:

var tmp = with25.JobItems;

with25.JobItems = new MyType[_with25.NumJobItems]; 
tmp.CopyTo(with25.JobItems, 0);
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • No, [`With` blocks](https://msdn.microsoft.com/en-us/library/wc500chb.aspx) are just shorthand for avoiding having to specify the same variable (or variable + chain or property accessors) for multiple statements that interact with the same object. Nothing "transactional" here. – Damien_The_Unbeliever Jul 12 '16 at 07:12
0

Most of the answers are confusing 'ReDim' with 'ReDim Preserve'. For the conversion of a simple 'ReDim', no Array.Resize call is necessary or even recommended - the C# equivalent is just:

public JobTicketConcaveItemsType InitJobTicketConcaveItemsType()
{
    JobTicketConcaveItemsType OutData = new JobTicketConcaveItemsType();
    OutData.NumJobItems = 1;
    OutData.JobItems = new foo[OutData.NumJobItems];
    OutData.JobItems[0] = JobDataConcaveEnum.JDBDryData;
    return OutData;
}
Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28