vb.net - 如何在 vb.net 中引用另一个类中的类变量

标签 vb.net byref

我有以下内容(经过简化以便于阅读)

头等舱:

Class MainWindow
     Private mFile As myFile 'myFile is a class containing a bunch of stuff

     Sub go()
          dim editFiles as New EditFiles(mFile)
     End Sub
End Class

第二类:

Public Class EditFiles
    Private mFile As myFile 'myFile is a class containing a bunch of stuff
Sub New(ByRef passedFile As myFile)

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    mFile = passedFile

End Sub

我希望发生的是我对第二类中的 mFile 所做的任何更改也可以更改第一类中的 mFile,我认为通过在初始化中传递它的 ByRef 会发生但显然不会。

我想知道什么是使这项工作合适的方法?我知道我可以创建一个全局变量,但必须有一种方法可以从第一个类传递 mFile 的指针,以便第二个类中的 mFile 基本相同。

如果您能通过编辑上面的代码向我展示一个简单的示例,我将不胜感激!

最佳答案

你应该在第二类中创建一个第一类的对象。此外,您还需要一种方法来更改第一类中 mFile 的值。它应该类似于以下内容。

Class MainWindow
     Private  mFile As myFile 'myFile is a class containing a bunch of stuff

     Sub go()
          dim editFiles as New EditFiles(mFile)

     End Sub

     sub setMFile(_mfile as myfile)
        me.mfile = _mfile
End Class

二等舱

Public Class EditFiles
    Private mFile As myFile 'myFile is a class containing a bunch of stuff
    Sub New(ByRef passedFile As myFile)

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    mFile = passedFile
    dim newObject as new MainWindow
    newobject.setMFile(mFile)
End Sub

关于vb.net - 如何在 vb.net 中引用另一个类中的类变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13479321/

相关文章:

c# - 在 .NET TextBox 上设置光标的当前行

c# - 将 vb.net 转换为 c#-优化参数传递

arrays - 为什么 VBA 类不能通过 ByRef Array()?

VB.NET:如果我将一个 String ByVal 传递给一个函数但不更改该字符串,那么内存中是否有一个或两个字符串?

vb.net - My.Computer.Audio.Play上的FileNotFoundException异常

html - 对象引用未设置为对象的实例 - vb.net

c - 如何引用 C 程序编码字节数组?

javascript - 如何将文件上传选择插入到 GridView 中,然后添加新行?

javascript - 使用 byref 传递的数组

autohotkey - 获取传递的 ByRef 变量的字符串值