2

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.

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.

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

user1839684
  • 104
  • 1
  • 2
  • 7
  • From the first class, you do not instantiate 'myFile' before passing it to the object 'EditFiles', so it wont work... Is this a simplifing code mistake ? – Minus Nov 20 '12 at 18:38

3 Answers3

3

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.

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
Austin Davis
  • 3,516
  • 10
  • 27
  • 47
  • Thanks this got me thinking in the right direction, I ended up passing the first class to the second class when I initialized it and then after I was done with all my work I could set mFile equal to each other and done! – user1839684 Nov 20 '12 at 21:29
  • So if found my answer to be the right one then please click on the check mark next to my answer. If not then please post your own answer with some example code, to help future viewers of this post. – Austin Davis Nov 20 '12 at 21:54
2

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

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

Meta-Knight
  • 17,626
  • 1
  • 48
  • 58
1

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
user1839684
  • 104
  • 1
  • 2
  • 7
  • Was there anything preventing you from initializing mFile in MainWindow before creating the EditFiles instance? Because it would have been much simpler. – Meta-Knight Nov 21 '12 at 14:17
  • Well, it's not the best solution, but it does work. I think it would have been better to create a public property in the second class that I reference in the first class to get mfile. – user1839684 Jul 14 '14 at 15:17