如何在结构中初始化二维数组 [英] How to initialize two dimension array in structure

查看:105
本文介绍了如何在结构中初始化二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Private Structure ResponseFrame
       Dim InPtr As Byte
       Dim Outptr As Byte
       Dim TxBuff(RESPONSE_BUFF_SIZE, 12) As Byte
       Dim TxIpAddress(RESPONSE_BUFF_SIZE) As String
       Dim TxPortNo(RESPONSE_BUFF_SIZE) As String
       Dim TxSize As Byte
   End Structure









加粗部分我收到错误









in bold part i getting error


And RESPONSE_BUFF_SIZE is my constant   out side structure

Private Const RESPONSE_BUFF_SIZE As Byte = 200

推荐答案

你有什么错误?



首先想到的是你不能将具有指定大小的数组声明为结构成员。你必须在结构的构造函数中初始化数组,但这有点像黑客,因为你必须用参数声明一个New,即使你不使用参数:

What error are you getting??

The first thing that comes to mind is that you cannot declare an array with a specified size as a structure member. You have to initialize the array in the constructor of the structure, but this is a bit of a hack because you must declare a New with a parameter, even if you don''t use the parameter:
Private Structure ResponseFrame
    Dim InPtr As Byte
    Dim Outptr As Byte
    Dim TxBuff(,) As Byte
    Dim TxIpAddress() As String
    Dim TxPortNo() As String
    Dim TxSize As Byte
 
    '' dummy is never used
    Sub New(dummy As Integer)
        ReDim TxBuff(RESPONSE_BUFF_SIZE, 12)
        ReDim TxIpAddress(RESPONSE_BUFF_SIZE)
        ReDim TxPortNo(RESPONSE_BUFF_SIZE)
    End Sub
End Structure





现在,当你使用Struct时,你必须新建一个结构实例才能运行它的New方法:



Now, when you use the Struct, you have to new up an instance of the structure to get its New method to run:

Dim myInstance As New ResponseFrame(0) '' Pass in dummy value





第二件事就是RESPONSE_BUFF _SIZE对结构不可见。由于它是私有的,它只能在容器中看到它所定义的。



更好的解决方法是转换结构因为,为了使用它,你无论如何都会把这个结构视为一个类。



The second thing that comes to mind is that RESPONSE_BUFF_SIZE is not visible to the structure. Since it''s Private, it''s only visible to the container that it''s defined in.

The better way around this is to convert the structure to a class since, in order to use it, you''ll be treating the structure like a class anyway.


这篇关于如何在结构中初始化二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆