Vb.net 通过文件/函数传递结构作为参数

Vb.net 通过文件/函数传递结构作为参数,vb.net,Vb.net,我无法像在早期MS basic版本中那样,通过单个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 Publi

我无法像在早期MS basic版本中那样,通过单个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
文件“mysetup.dat”是可访问的,数据保存正确,我可以通过HxD看到。 但阅读部分似乎并没有达到预期效果


请根据上面的示例提供可靠的传递结构作为参数,而不使用太多公共元素的帮助。

我强烈建议您重写代码,以使用
System.IO.File
类中的新.NET IO方法,但除此之外,我认为您现有代码的问题在于需要将
mmt
参数从
ByVal
更改为
ByRef

嗨,史蒂夫,我不太明白什么是.Net IO,因为我对VB.Net很陌生。但是通过引用传递我的结构,我使我的代码按预期工作,谢谢。
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