Is this a safe implementation: accessing subroutine in window1 from window2
-
This implementation seems to work just great, I am however worried about the correctness of this implementation. I want to ensure that there is no possibility of a problem down the road... Window1 creates and shows Window2:
Class Window1 'member variables Private myWindow2 As Window2 = Nothing Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded myWindow2 = New Window2(Me) End Sub Private Sub SomeButtonClick(....) myWindow2.ShowYourself() End Sub End Class
Window2 looks like this:Partial Public Class Window2 'member variables Private m_oParent As Object Public Sub New(ByRef OParent As Object) ' This call is required by the Windows Form Designer. InitializeComponent() m_oParent = OParent End Sub Public Sub ShowYourself() 'some other stuff happens here to build the UI, once done this shows the window Me.Show() End Sub Private Sub ButtonSubmit_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ButtonSubmit.Click 'refresh the UI of Window1 with the updated data that we edited in Window2 Dim OWindow1 As Window1 = DirectCast(m_oParent, Window1) OWindow1.SetStringData("Hello from Window2") OWindow1.RefreshStackPanel() 'and hide this window Me.Hide() 'ensure Window1 is immediately shown OWindow1.Focus() End Sub End Class
What I'm concerned about is accessing Window1 from Window2 as can be seen in ButtonSubmit_Click() Should I have used Invoke? If so, how is that done ? -
This implementation seems to work just great, I am however worried about the correctness of this implementation. I want to ensure that there is no possibility of a problem down the road... Window1 creates and shows Window2:
Class Window1 'member variables Private myWindow2 As Window2 = Nothing Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded myWindow2 = New Window2(Me) End Sub Private Sub SomeButtonClick(....) myWindow2.ShowYourself() End Sub End Class
Window2 looks like this:Partial Public Class Window2 'member variables Private m_oParent As Object Public Sub New(ByRef OParent As Object) ' This call is required by the Windows Form Designer. InitializeComponent() m_oParent = OParent End Sub Public Sub ShowYourself() 'some other stuff happens here to build the UI, once done this shows the window Me.Show() End Sub Private Sub ButtonSubmit_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ButtonSubmit.Click 'refresh the UI of Window1 with the updated data that we edited in Window2 Dim OWindow1 As Window1 = DirectCast(m_oParent, Window1) OWindow1.SetStringData("Hello from Window2") OWindow1.RefreshStackPanel() 'and hide this window Me.Hide() 'ensure Window1 is immediately shown OWindow1.Focus() End Sub End Class
What I'm concerned about is accessing Window1 from Window2 as can be seen in ButtonSubmit_Click() Should I have used Invoke? If so, how is that done ?This code will work, so long as it works. That is, you never check if window2 is null, or if oWindow1 is null. You should check to make sure that everything is in the state you are hoping.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.