2014-04-15 42 views
0

所以我有一個事件處理一個類來幫我處理進度階段,工作潛艇裏面做工作的子類,我有我剛剛從複製他自己的事件的子類主類。 問題是子類中的事件根本不起作用。我不知道爲什麼真的。的RaiseEvent在子類中不工作(vb.net)

那是我的代碼看起來像:

Class FolderHelper 

    'Thats my progressbar eventhandler. It is OK 
    Private Shared _StageCompleted As Integer 

    Public Shared Event MyProgressChanged(ByVal CurStage As Integer) 

    Public Shared Property StageCompleted() As Integer 
     Get 
      Return _StageCompleted 
     End Get 

     Set(ByVal CurProgress As Integer) 
      _StageCompleted = CurStage 
      'This event is OK. 
      RaiseEvent MyProgressChanged(CurStage) 
     End Set 
    End Property 

    Private Sub RefreshProgress() Handles Me.MyProgressChanged 
     'Some progressbar stuff here 
    End Sub 

    'This is my subclass with malfunction event 
    Public Class ParseNames 

     Private Shared _FilePath As String 

     Public Shared Event NewFilePath(ByVal NewFile As String) 

     Public Shared Property FilePath() As String 
      Get 
       Return _FilePath 
      End Get 

      Set(ByVal NewFile As String) 
       _FilePath = NewFile 

       'Problem here. Event doesnt fire. 
       'But its completely copies event in class above. 
       RaiseEvent NewFilePath(NewFile) 
      End Set 
     End Property 

     Private Sub AnalyzeNewFile(ByVal NewFile As String) Handles MyClass.NewFilePath 
      'Some work here 
     End Sub 

    End Class 

    'class with some work subs... 
    Public class DoWorks 

     Private Sub DoWork() 
      'Thats what has to call a work 

      'This variable set is NOT ok with firing event 
      Folder_helper.ParseNames.Filepath = SomeNewFile 

      'This one IS ok 
      Folder_helper.StageCompleted +=1 

     End Sub 
    End class 

End Class 

確定。奧斯卡最佳女主角是Plutonix :)

+0

'ParseNames'不是一個子類別,只是一個私有或內部類。它不是實例,並且沒有任何設置可以收聽它可能引發的任何事件。可以使用Shared Props和Subs in(因爲它們是Shared),但需要一個WithEvents對象變量來捕獲任何事件。 – Plutonix

+0

太棒了!在添加「Dim WithEvents Shared PN as new ParseNames」並將我的變量設置爲「PN.Filepath = SomeNewFile」後,所有工作都正常。非常感謝,man =) – Szer

+0

答案不應該是問題的一部分 - 請參閱我的編輯。 – Neolisk

回答

0

代碼是這樣工作的:

Class FolderHelper 
'Create an instance of my private class which is listening to events 
Public Shared WithEvents PN As New ParseNames 

Public Class ParseNames 
     'Same strings as above... 
End class 

'All others strings are the same except... 

Public class DoWorks 

    Private Sub DoWork() 

     'I has to call my newly created instance of ParseNames which is "WithEvents" 
     PN.Filepath = SomeNewFile 
     Folder_helper.StageCompleted +=1 

    End Sub 
End class 

末級

+0

雖然這可能會回答這個問題,但如果您能解釋爲什麼此解決方案適合您,那將會很好。這樣你的答案會對未來的訪客更有用。同時回答你自己的問題,而不用讚揚StackOverflow中幫助你的人(在這種情況下是Plutonix)。考慮讓他們發表一個答案,然後接受。如果您願意,您可以自由發佈您的答案。 – Neolisk