【问题标题】:reimplimenting a VB6 nested type as a structure in VB.net将 VB6 嵌套类型重新实现为 VB.net 中的结构
【发布时间】:2014-05-23 23:42:40
【问题描述】:

我正在移植一个使用复杂类型来包含结构化消息系统的 VB6 应用程序。我正在解决的当前问题是将其转换为 VB.net 结构。这是声明:

Global Const MAX_MSGS = 150     ' Max number of messages per set
Global Const MAX_SETS = 100     ' Max number of message sets
Global Const MAX_HGI = 96       ' Max number of HowGozIt messages per set
Global Const MAX_WPS = 96       ' Max number of way points per message set
Global Const MAX_FLS = 15       ' Max number of flight levels per waypoint

Type MessageRec
 MessType As Integer           'Message Type
 MessIndex As Integer          'Message Index (not used currently)
 MessNextBit As Integer        'last bit for Mops messages
 MessTotal As Integer          'Mess total
 MultMessBit As Integer        'Multiple message bit position
 MessTitle As String * 25      'Message title
 MessNo As String              'Message Numbers and data for mops edit
 MessText As String            'I/F displayable text
 MessStr As String             'message bits for mops msgs, can be file for acars,aoc
 MessLabel As String * 4       'Label and sublabel
 TimeDelay As Integer          'Delayed time in seconds before message is delivered
 Active As Boolean             'Flag determines active/deactive
 AutoInit As Boolean            'Flag if part of auto init bundle
 AIDelay As Integer             'Auto Init time delay (secs)
End Type

Type WayPointRec
 Name As String * 7
 DeltaTime As String * 5
 FltLevel As String * 5
 DeltaFuel As String * 5
 Lat As String * 6                   ' Latitude of waypoint
 Long As String * 7                  ' Longitude of waypoint
 SAT As String * 3                   ' Standard Air temp (no need to save in database)
 Via As String
 NumAlts As String * 2                ' Numer of altitudes for following weather data
 FL(0 To MAX_FLS) As String * 3      ' Flight level for this weather
 WindDir(0 To MAX_FLS) As String * 3 ' Wind direction
 WindSpd(0 To MAX_FLS) As String * 3 ' Wind speed
 Temp(0 To MAX_FLS) As String * 3    ' Temperature
End Type

Type MessageStore                  
 Nmsgs As Integer              'Number of messages in the message set
 Fltno As Integer              'Flight Number
 SubFltNo As String * 1        'For multiple messages with same flight number
 CityPair As String * 7        'City Pair
 Descr As String * 12          'Message Set Description
 Hot As Integer                'Hot/Cold flag (true => hot, use second weather message set)
 DWndFL(0 To 3) As Integer     'Descent wind flight level (4 altitudes)
 DWndDir(0 To 3) As Integer    'Descent wind direction (4 altitudes)
 DWndSpd(0 To 3) As Integer    'Descent wind speed (4 altitudes)
 WndSpd(0 To 9) As Integer     'Start/end wind speed (5 altitudes)
 WndDir(0 To 9) As Integer     'Start/end wind direction (5 altitudes)
 TempDev(0 To 1) As Integer    'Start/end Temperature deviation from standard
 altn(0 To 1) As String * 3    'Alternate airports for area weather information
 nHGIwpts As Integer           'Number of waypoints for howgozit info
 HGIinfo(1 To MAX_HGI, 0 To 3) As String * 5  'Howgozit information
 MsgData(1 To MAX_MSGS) As MessageRec 'Message data mlw 7/12/02 changed 30 to 60
 nwpts As Integer              'Number of waypoints for winds/temp info
 WayPoints(1 To MAX_WPS) As WayPointRec 'For canned messages
 Index As Integer
 Active As Boolean             'Flag determines active/deactive
 DeltaArrivalTime As Integer   'add or subtract to get actual arrival time
 TimeToGate As Integer         'Time to gate from T/D for HOWGOZIT
End Type

MessageStore 结构是另一个 C 程序读取的磁盘格式(所有字段都转换为字符串以进行写入)。

我没有问题转换为简单字符串的固定长度字符串,但转换 WayPointRec 中的固定长度字符串数组,然后包含在 MessageStore 中 WayPointRec 的 WayPoints 数组中,这是我目前卡住的地方

还有如何最好地处理固定长度字符串的“HGIinfo”二维字符串数组

编辑

省略了更多细节 - 顶级

Public MsgArray() As MessageStore

MsgArray 增长的地方

redim preserve MsgArray(x)

x 限制在 0-100

【问题讨论】:

  • C 程序使用数据是否有任何灵活性,或者它基本上是第 3 方事务?这并不是要为“只在网络中做这件事”的回应敞开大门,但其中一些事情是非常有问题的。
  • C程序跨平台运行,VAX-VMS,AIX 4.2,SYSTEM V R4 unix,Linux

标签: arrays vb.net vb6 structure


【解决方案1】:
Public Class MessageStore
    Public MsgData As MessageRec
    Public WayPoints As WayPointRec
    Public DWndFL(3) As Integer
    Public HGIinfo(MAX_HGI, 3)() As String
End Class

Public Class WayPointRec
    Public FL(MAX_FLS)() As String
End Class

但是,最好将数组重新设计成这样的形式(或者,将它们全部转换为类,但工作量太大):

Private _HGIinfo(MAX_HGI, 3, 5) As String

此外,不要使用结构,您将无法初始化数组的大小,请参阅:http://msdn.microsoft.com/en-us/library/2hkbth2a%28v=vs.71%29.aspx

还有:http://msdn.microsoft.com/en-us/library/wak0wfyt.aspx#BKMK_CreatingAnArray

应该涵盖转换,需要推断休息。

3 个问题:

  • 无法使用边界说明符在 .net 中初始化数组。 IE: 公共 HGIinfo(MAX_HGI,3)(5) 作为字符串
  • 无法使用结构初始化数组的大小
  • 无法初始化数组边界(1 到 #),数组在 vb.net 中从 0 开始。

您可以通过使用 getter/setter 属性来解决它们(但这会很难看):

Private _HGIinfo(MAX_HGI, 3)() As String
Public Property HGIinfo(ByVal a As Integer, ByVal b As Integer) As String()
    Get
        ' perform some checks on value or MAX_HGI
        Return _HGIinfo(a, b)
    End Get
    Set(ByVal value As String())
        ' perform some checks on value or MAX_HGI
        _HGIinfo(a, b) = value
    End Set
End Property
HGIinfo(0, 1)(3).SubString(0, 4) ' would get the first 4 chars of the 3rd string, at position 0,1 in the _HGIinfo array

此外,要使用上限初始化 String() 数组,您需要类似以下内容:

Private _HGIinfoStr(5) As String
Private _HGIinfo(MAX_HGI, 3)() As String
Public Sub New()
    _HGIinfo(0, 0) = _HGIinfoStr ' would need to loop through 0 to MAX_HGI, and also 0-3 to init all the members of the _HGIinfo array with bounds.
End Sub

再一次,那真的很混乱。类方法供参考:

Public Class HGIinfoClass
    Public _HGIinfoStr(5) As String
End Class
Private _HGIinfo(MAX_HGI, 3) As HGIinfoClass
Public Sub test()
    _HGIinfo(3, 3)._HGIinfoStr(3) = "test"
End Sub

事实上,我正在考虑一种方法来使用属性来维护当前函数并为新的数组边界重新格式化并返回,不幸的是,这并不是那么简单。将需要数组副本或一些复杂的循环。

编辑:另外,另一种选择是将它们变成从 ArrayList 继承的类,并设置容量属性。但总体而言效率较低。

还可以使用以下方法指定数组的下限:

Array.CreateInstance(GetType(String), New Integer(){5}, New Integer(){1}).

见(再次,真的很丑):http://msdn.microsoft.com/en-us/library/vstudio/x836773a

【讨论】:

  • 见问题末尾的编辑。 MsgArray 是 MsgStore 的动态数组,最大大小为 100 个 MsgStore
  • 您可以在 vb.net 中以相同的方式执行此操作,也可以使用实现iList 接口的ArrayListmsdn.microsoft.com/en-us/library/vstudio/…
猜你喜欢
  • 2018-02-01
  • 1970-01-01
  • 2011-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2015-09-18
相关资源
最近更新 更多