2012-12-13 49 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)确实有效!非常感谢你们! –