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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13479321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:14:10  来源:igfitidea点击:

How to reference a class variable from another class in vb.net

vb.netbyref

提问by user1839684

I have the following (simplified to make this easy to read)

我有以下内容(简化以使其易于阅读)

first class:

第一类:

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

Second 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

what I would like to happen is any changes I make to mFile in the second class to also change mFile in the first class, I thought that by passing it ByRef in the initialization that would happen but apparently not.

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

What I am wondering is what is the appropriate method to make this work? I know I could create a global variable but there must be a way to pass the pointer of mFile from the first class so that mFile in the second class is essentially the same.

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

If you could show me a simple example perhaps by editing the above code I would really appreciated it!

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

采纳答案by user1839684

Here is how I ended up solving my problem:

这是我最终解决问题的方法:

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

 Sub go()
      dim editFiles as New EditFiles(me, mFile)
 End Sub

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

Second class

二等舱

Public Class EditFiles
Private mainWindow As mainWindow
Private mFile as myFile
Sub New(ByVal sourceWindow As mainWindow, byVal sourceFile as myFile)

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

    ' Add any initialization after the InitializeComponent() call.
    mainWindow = sourceWindow
    mFile = sourceFile

end Sub
Sub setFile
    mainWindow.setMFile(mFile)
End Sub

回答by Austin Davis

You should create an object of the first class in the second class. Also you need a method that changes the value of mFile in the first class. It should be something like the following.

您应该在第二个类中创建第一个类的对象。您还需要一个方法来更改第一类中 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

Second 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

回答by Meta-Knight

You need to make sure MainWindow's mFile variable is initialized beforepassing it to the EditFiles object.

您需要确保 MainWindow 的 mFile 变量将其传递给 EditFiles 对象之前已初始化。

Also, if myFile is a Class, you don't even need to pass it ByRef.

此外,如果 myFile 是一个类,您甚至不需要通过 ByRef 传递它。