我无法像在早期 MS 基本版本中使用的那样,通过单个 vbnet 项目的不同文件中的子/函数将结构作为参数传递。
这是情况的简短示例:
模块1.vb
Imports System.IO
Structure mymultitry
Dim index As Integer
<VBFixedString(6)> Dim name As String
Dim weight As Double
End Structure
Module Module1
Public mysetupfile = "mysetup.dat"
Public Sub rwfile(ByVal rw As Integer, ByVal myrecord As Integer, ByVal mmt As mymultitry)
'EDIT: Thanks to SteveDog - proper line should be:
'Public Sub rwfile(ByVal rw As Integer, ByVal myrecord As Integer, ByRef mmt As mymultitry)
Dim fnum As Integer
fnum = FreeFile()
FileOpen(fnum, mysetupfile, OpenMode.Random, OpenAccess.ReadWrite, OpenShare.Shared, Len(mmt))
If rw Then
FilePut(fnum, mmt, myrecord)
Else
FileGet(fnum, mmt, myrecord)
End If
FileClose(fnum)
End Sub
End Module
Form1.vb
Public Class Form1
Dim mmt As mymultitry
Dim mmt1 As mymultitry
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With mmt
.index = 4
.name = "Helga"
.weight = 128.1445
End With
rwfile(1, 1, mmt) 'write
rwfile(0, 1, mmt1) 'read
'all zero here !?!
Debug.Print(mmt1.index)
Debug.Print(mmt1.name)
Debug.Print(mmt1.weight)
End Sub
End Class
文件“mysetup.dat”是可访问的,并且数据已正确保存,我可以使用 HxD 看到。但阅读部分似乎无法按预期工作。
请任何关于可靠传递结构作为参数的帮助,没有太多基于上例的公共元素。