我可以传递数组的子集以在 VB.NET 中运行吗?

发布于 2024-09-07 10:03:07 字数 641 浏览 2 评论 0原文

我有一个简单的 VB.NET 表单,它充当具有公共 API 的控件库的接口。

其中一个 API 调用采用 UIntegers ByRef 数组:

Public Function Get_Values(source_id As Byte, first_value_address As Byte, number_of_values As Byte, ByRef valuesOut As UInteger()) As Integer

调用后,valuesOut 将保存从 source_id 开始、从 first_value_addressnumber_of_values 值列表>。

有一个硬件驱动的限制,即无论请求的长度如何,返回值的最大数量为 15。我需要从 VB.NET 代码中获取 28 个值到一个数组中。

是否可以仅将数组变量的一部分发送到类似于以下 C 代码的函数?

uint[28] values;
Get_Values(0, 0, 15, values); // Get first part
Get_Values(0, 15, 13, &values[15]); // Get second part of data

I have a simple VB.NET form that acts as an interface to a control library with a public API.

One of the API calls takes an array of UIntegers ByRef:

Public Function Get_Values(source_id As Byte, first_value_address As Byte, number_of_values As Byte, ByRef valuesOut As UInteger()) As Integer

After the call, valuesOut will hold a list of values number_of_values long from source_id starting at first_value_address.

There is a hardware-driven limitation that the maximum number of values returned is 15 regardless of requested length. I need to get 28 values into an array from my VB.NET code.

Is it possible to send only part of an array variable to the function similar to the following C code?

uint[28] values;
Get_Values(0, 0, 15, values); // Get first part
Get_Values(0, 15, 13, &values[15]); // Get second part of data

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有心的人 2024-09-14 10:03:07

不,你具体要求的是不可能的。与 C/C++ 样式数组(简化后只是等于 sizeof(Type) * n 的内存块,其中 n 是元素数量)不同,.NET数组不能通过指针算术引用或偏移*。因此,如果公共 API 没有为您提供指示数组中偏移量的方法,那么您将必须将中间数组传递给函数,并在完成后自行重新组装它们。

但是,您可以将调用包装在您自己的函数版本中:

public int GetValues(byte source_id, byte first_value_address, byte number_of_values, uint[] buffer, int offset)
{
    uint[] temp = new uint[number_of_values];

    int retValue = GetValues(source_id, first_value_address, number_of_values, temp);

    Array.Copy(temp, 0, buffer, offset, number_of_values);

    return retValue;
}

还应该注意的是,ByValByRef 代表调用约定,而不是类型是否为一个值类型。除非您有特定的原因(这里看起来您没有),否则不需要在数组参数上指定 ByRef 。唯一需要的类型是当函数将更改变量的实际值并且您希望将其反映在调用代码中时。您调用它的方式似乎是分配数组,将其传递给函数,然后使用其内容。函数唯一一次修改变量值的情况就是将其设置为等于不同的数组实例;仅更改数组值不需要 ByRef 调用代码即可查看结果。

**除非您实际上使用不安全代码并自己执行基于指针的数组,但这超出了您的问题范围*

No, what you're specifically asking for is not possible. Unlike C/C++-style arrays (which, simplified are just blocks of memory equal to sizeof(Type) * n, where n is the number of elements), .NET arrays cannot be referred to or offset by pointer arithmetic*. As a result, if the public API does not provide you with a way to indicate an offset in the array, then you're going to have to pass intermediate arrays to the function and reassemble them yourself once you've finished.

You could, however, wrap the call in your own version of the function:

public int GetValues(byte source_id, byte first_value_address, byte number_of_values, uint[] buffer, int offset)
{
    uint[] temp = new uint[number_of_values];

    int retValue = GetValues(source_id, first_value_address, number_of_values, temp);

    Array.Copy(temp, 0, buffer, offset, number_of_values);

    return retValue;
}

It should also be noted that ByVal and ByRef represent calling conventions, not whether or not the type is a value type. Unless you have a specific reason to (and here it appears that you do not), you don't need to specify ByRef on your array argument. The only type ByRef is required is when the function will change the actual value of the variable and you want that reflected in the calling code. The way that you're calling it, it appears that you allocate the array, pass it to the function, then use its contents. The only time the function would be modifying the variable's value is if it set it equal to a different array instance; simply changing the array values does not require ByRef in order for the calling code to see the results.

**Unless you actually use unsafe code and do pointer-based arrays yourself, but that's outside the scope of your question*

双马尾 2024-09-14 10:03:07

据我所知,VB 中仅数组不具备此功能。不过我可能是错的,我自己对 VB 相当陌生 - 解决方法是将数组转换为列表(因为你的数组看起来相当小,这不应该是问题),调用 addRange 例程,然后是 toArray() 方法。

 Dim list as New List(Of Object)
 list.addRange(theArray)
 theArray = list.GetRange(0, 2).ToArray() ' Gets only the first two indices and puts them into an array

As far as I know, Arrays alone don't have this capability in VB. I could be wrong though, I'm fairly new to VB myself - a workaround would be to convert the array to a list (since your array looks to be fairly small, this shouldn't be a problem), call the addRange routine, then the toArray() method.

 Dim list as New List(Of Object)
 list.addRange(theArray)
 theArray = list.GetRange(0, 2).ToArray() ' Gets only the first two indices and puts them into an array
岁月静好 2024-09-14 10:03:07

您必须创建一个固定大小的缓冲区数组并将其放入您的函数中。然后复制到main,它可以是任意大小。

You have to create a fixed-size buffer array and put it to your function. Then copy to main, which can be any size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文