2014-05-23 19 views
0

我正在移植使用複雜類型來包含結構化消息系統的VB6應用程序。我正在工作的當前問題是將其轉換爲VB.net結構。以下是聲明:將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這在隨後包括WayPointRec在MessageStore的航點陣列中的是,其中我目前停留

也如何最好地處理固定長度字符串

編輯的「HGIinfo」 2D字符串數組

省略了一個更詳細 - 頂層

Public MsgArray() As MessageStore 

其中MsgArray成長爲

redim preserve MsgArray(x) 

與X受限0-100

+0

是有與所述C程序使用了該數據的任何靈活性,或這基本上是第三方事件嗎?這並不是打開「在網絡中全部做好」的大門,但其中一些問題非常棘手。 – Plutonix

+0

C程序運行跨平臺,VAX-VMS,AIX 4.2,SYSTEM V R4 unix,Linux –

回答

0
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陣列與界限符。即: 公共HGIinfo(MAX_HGI,3)(5)作爲字符串
  • 可使用結構陣列的未初始化尺寸
  • 不能初始化數組邊界(1〜#),陣列從0在vb.net啓動。

可以使用的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 

也有上限初始化字符串()陣列,你需要的東西像:

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繼承的Classes,並設置Capacity屬性。但整體效率不高。

,並且還可以利用來指定下界陣列:

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

見(再次,真難看):http://msdn.microsoft.com/en-us/library/vstudio/x836773a

+0

請參見編輯問題結束。 MsgArray是MsgStores的一個動態數組,它的最大容量限制爲100 MsgStore –

+0

您可以在vb.net中以相同方式執行此操作,也可以使用實現「iList」接口的「ArrayList」:http://msdn.microsoft .COM/EN-US /庫/ vstudio/system.collections.arraylist – porkchop