简体   繁体   English

从VB转换为C#的代码中的IndexOutOfRange异常

[英]IndexOutOfRange Exception in code converted from VB to C#

I have this VB code 我有这个VB代码

 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 转换后的C#代码

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. 当我尝试运行应用程序时,出现未处理的Error IndexOutOfRangeException错误。 I have made sure to use Array.Resize() to reallocate the array 我确保使用Array.Resize()重新分配数组

The code in VB doesn´t give errors. VB中的代码不会给出错误。 Any clues ? 有什么线索吗?

JDBDryData has been defined as below JDBDryData的定义如下

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. 为什么在代码的下一行中将_with25.JobItems数组的大小调整为0

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) 您会看到ArrayIndexOutOfBound异常,可以看到,您正在尝试将值设置为数组的第零个元素,而根本没有任何元素(大小为零的数组)

remove the Array.Resize(ref _with25.JobItems, _with25.NumJobItems - 1); 删除Array.Resize(ref _with25.JobItems, _with25.NumJobItems - 1); Or edit the same line as follows Array.Resize(ref _with25.JobItems, _with25.NumJobItems); 或按如下Array.Resize(ref _with25.JobItems, _with25.NumJobItems);编辑同一行: Array.Resize(ref _with25.JobItems, _with25.NumJobItems); and see. 看看。

In VB array declarations , you specify the last valid index: VB数组声明中 ,您指定最后一个有效索引:

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

In C# array declarations , you specify the length of the array. C#数组声明中 ,您可以指定数组的长度。

That means that you should always add 1 when converting a VB array declaration to a C# one. 这意味着将VB数组声明转换为C#时,应始终加1。 So, simply, don't subtract that 1: 因此,简单地说,不要减去1:

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

The problem is that you cannot resize array to size 0, as other answers mentioned already. 问题是您无法将数组的大小调整为0,就像已经提到的其他答案一样。 Also, you need to know that Array size declared in VBscript is 1 more than C#. 另外,您需要知道VBscript中声明的数组大小比C#大1。 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. 因此,在您情况下,您不能在不调整数字的情况下将_with25.NumJobItems - 1从VBScript转换为C#。

Regarding Array size in VBScript and C#: 关于VBScript和C#中的数组大小:

  • VB script: VB脚本:

    Dim arr(5) 昏暗的人(5)

    ' Size is 6, and you can access them by arr(0), arr(1), ... arr(5) '大小为6,您可以通过arr(0),arr(1),... arr(5)访问它们

  • C# C#

    MyType[] arr = new MyType[5]; MyType [] arr = new MyType [5];

    // Size is 5, and you can access them by arr[0], arr[1], ... arr[4] //大小为5,您可以通过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 : 您根本不需要使用Array.Resize ,只需将内容从现有数组复制到一个新数组中,然后使用Array.Resize将其存储到原始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'. 大多数答案使“ ReDim”与“ ReDim Preserve”混淆。 For the conversion of a simple 'ReDim', no Array.Resize call is necessary or even recommended - the C# equivalent is just: 对于简单的“ ReDim”的转换,不需要甚至不需要Array.Resize调用-C#等效项仅是:

public JobTicketConcaveItemsType InitJobTicketConcaveItemsType()
{
    JobTicketConcaveItemsType OutData = new JobTicketConcaveItemsType();
    OutData.NumJobItems = 1;
    OutData.JobItems = new foo[OutData.NumJobItems];
    OutData.JobItems[0] = JobDataConcaveEnum.JDBDryData;
    return OutData;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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