gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:14:19 25 4
gpt4 key购买 nike

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

头等舱:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com