2012-12-13 52 views
0

我在使用Option Strict時打開的二進制文件中讀取結構數據的功能有問題。複製結構數據

Public Function arh_setup(ByVal rw As Boolean) As Integer 
    Dim retval As Integer = 0 
    Dim fnum As Integer = FreeFile() 
    Dim temp As ValueType = CType(New aSetup, ValueType) 
Try 
     FileOpen(fnum, setup_file, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.Shared, Len(asT)) 
     If rw Then 
      FilePut(fnum, asT, 1) 
     Else 
      FileGet(fnum, temp, 1) 
     End If 
     FileClose(fnum) 
    Catch ex As Exception 
     retval = -1 
    End Try 

' Now is question here 
' How to get data from temp to asT?? 
EDIT: 
Solution is asT = CType(temp, aSetup) 

    temp= Nothing   
    Return retval 
End Function 

AST被全局聲明應當從臨時含有(正確)readed數據取數據類型aSetup(結構)的變量。

問題是如何將數據從本地temp變量複製到全局asT變量,最好是沒有循環(逐字節)或循環,如果不可能,否則?

+1

這是如此接近VB6,你可能會稱之爲VB6。如果您只是使用內置的.NET方法(如IO.File.ReadAllBytes),則問題將不存在。 – test

+0

不幸的是,我們有「現狀」的情況。 –

回答

1

嘗試鑄造:

asT = CType(temp, aSetup) 

此外,退出方法之前不設置局部變量Nothing,這是完全沒有意義的。你應該使用.NET的FileStream等代替VB6兼容性方法來讀取文件。

+0

編譯器說:Op​​tion Strict On禁止從'System.ValueType'到'myProg.arh_Setup'的隱式轉換Casting(包含所有東西的東西)也不會去。 –

+1

@ user973238:'arh_Setup'?你確定它說'asT = CType(temp,aSetup)',那麼? – Ryan

+0

對不起,是! asT = CType(temp,aSetup)確實有效!非常感謝你們! –