简体   繁体   中英

IndexOutOfRange Exception in code converted from VB to C#

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;

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.

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);

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]

You won´t need to use Array.Resize at 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);

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM