locked
Event declaration RRS feed

  • Question

  • Hello,

    I did a recursive function in order to browse folders. I want it to raise an event at each directory change.

    But how to declare, and use this event ?
    i found this but i can't understand any part.


    Informations :
    the recursive function if in a class named : classe
    the class is in a namespace named : namesp
    the whole is for a dll project named : dFx

    I want to access to my event from a form in a project where dFx.dll is in references.


    So :
    what about declaration, and use ?


    Thanks anyway.
    dFx
    Thursday, November 20, 2008 2:54 PM

Answers

  •  
    Option Explicit On 
    Option Strict On 
     
    Imports System.IO  
     
    Public Class DirectoryTraverser  
        ' Declare the event. Indicate that the event provides details in the form   
        ' of an object of type VisitingDirectoryEventArgs.   
        Public Event VisitingDirectory As EventHandler(Of VisitingDirectoryEventArgs)  
     
        Public Sub Traverse(ByVal path As String)  
            ' Raise event.   
            OnVisitingDirectory(New VisitingDirectoryEventArgs(path))  
     
            ' Process child directories.   
            For Each s As String In Directory.GetDirectories(path)  
                Traverse(s)  
            Next s  
        End Sub 
     
        ' It is a standard practice to make a protected virtual method   
        ' actually responsible for raising the event. (If your class will   
        ' not be inherited, this is unimportant.)   
        Protected Overridable Sub OnVisitingDirectory(ByVal e As VisitingDirectoryEventArgs)  
            RaiseEvent VisitingDirectory(Me, e)  
        End Sub 
    End Class 
     
    ' This is the event argument class for providing the detailed   
    ' information about the event to the handler.   
    Public Class VisitingDirectoryEventArgs  
        Inherits EventArgs  
     
        Private mPath As String 
     
        Public ReadOnly Property Path() As String 
            Get 
                Return mPath  
            End Get 
        End Property 
     
        Public Sub New(ByVal path As String)  
            mPath = path  
        End Sub 
    End Class 
     
    Public Class Form1  
     
        Private Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgs) Handles Button1.Click  
            Dim dt As New DirectoryTraverser()  
            AddHandler dt.VisitingDirectory, AddressOf DirectoryTraverser_VisitingDirectory  
            dt.Traverse("c:\test")  
        End Sub 
     
        Private Sub DirectoryTraverser_VisitingDirectory(ByVal sender As ObjectByVal e As VisitingDirectoryEventArgs)  
            Debug.WriteLine(e.Path)  
        End Sub 
     
    End Class 
     
    • Marked as answer by Zhi-Xin Ye Tuesday, November 25, 2008 3:39 AM
    • Unmarked as answer by -dFx- Wednesday, December 3, 2008 10:45 PM
    • Marked as answer by -dFx- Thursday, December 4, 2008 12:03 PM
    Friday, November 21, 2008 11:43 PM

All replies

  • Here is some sample code, where I try to follow the Microsoft standards for events.  I have included a lot of comments in the code.

    public class DirectoryTraverser  
    {  
        // Declare the event. Indicate that the event provides details in the form  
        // of an object of type VisitingDirectoryEventArgs.  
        public event EventHandler<VisitingDirectoryEventArgs> VisitingDirectory;  
     
        public void Traverse(string path)  
        {  
            // Raise event.  
            OnVisitingDirectory(new VisitingDirectoryEventArgs(path));  
     
            // Process child directories.  
            foreach (String s in Directory.GetDirectories(path))   
            {  
                Traverse(s);  
            }  
        }  
     
        // It is a standard practice to make a protected virtual method  
        // actually responsible for raising the event. (If your class will  
        // not be inherited, this is unimportant.)  
        protected virtual void OnVisitingDirectory(VisitingDirectoryEventArgs e)  
        {  
            if (VisitingDirectory != null// If there are any handlers...  
            {  
                VisitingDirectory(this, e); // Raise the event.  
            }  
        }  
    }  
     
    // This is the event argument class for providing the detailed  
    // information about the event to the handler.  
    public class VisitingDirectoryEventArgs : EventArgs  
    {  
        public string Path { getprivate set; }  
     
        public VisitingDirectoryEventArgs(string path)  
        {  
            this.Path = path;  
        }  
    }  
     

    This shows how to call the above code:

            private void button1_Click(object sender, EventArgs e)  
            {  
                DirectoryTraverser dt = new DirectoryTraverser();  
                dt.VisitingDirectory += DirectoryTraverser_VisitingDirectory;  
                dt.Traverse(@"c:\test");  
            }  
     
            private void DirectoryTraverser_VisitingDirectory(object sender, VisitingDirectoryEventArgs e)  
            {  
                Debug.WriteLine(e.Path);  
            } 
    Friday, November 21, 2008 12:53 AM
  • Many thanks. i just forgot a thing : i'm coding in vb.net

    but i think 'll be able to convert it.

    dFx
    Friday, November 21, 2008 11:35 AM
  • Finaly i can't.

    That's my recursive to find files in directories :

    1        Shared Sub find_files(ByVal path As StringByRef found() As String
    2            If found Is Nothing Then 
    3                Exit Sub 
    4            End If 
    5            If path Is Nothing Or Not IO.Directory.Exists(path) Then 
    6                Exit Sub 
    7            End If 
    8            RaiseEvent dir_changed(path) ' i want to pass the current browsing path here. 
    9            Application.DoEvents() 
    10            Dim dir As New IO.DirectoryInfo(path) 
    11            Dim dirs_in(0) As IO.DirectoryInfo 
    12            Dim dir_ex As IO.DirectoryInfo 
    13            Try 
    14                dirs_in = dir.GetDirectories 
    15                For Each dir_ex In dirs_in 
    16                    find_files(dir_ex.FullName, found) 
    17                Next 
    18                Dim files_in() As IO.FileInfo 
    19                Dim file_ex As IO.FileInfo 
    20                files_in = dir.GetFiles("*.cfg"
    21                For Each file_ex In files_in 
    22                    ReDim Preserve found(found.Length) 
    23                    found(found.Length - 1) = file_ex.FullName 
    24                Next 
    25            Catch ex As Exception 
    26            End Try 
    27        End Sub 
    Declaration of the event :

            Shared Event dir_changed(ByVal directory As String


    what should i add ?

    dFx
    • Marked as answer by -dFx- Thursday, December 4, 2008 12:03 PM
    • Unmarked as answer by -dFx- Thursday, December 4, 2008 12:03 PM
    Friday, November 21, 2008 1:47 PM
  •  
    Option Explicit On 
    Option Strict On 
     
    Imports System.IO  
     
    Public Class DirectoryTraverser  
        ' Declare the event. Indicate that the event provides details in the form   
        ' of an object of type VisitingDirectoryEventArgs.   
        Public Event VisitingDirectory As EventHandler(Of VisitingDirectoryEventArgs)  
     
        Public Sub Traverse(ByVal path As String)  
            ' Raise event.   
            OnVisitingDirectory(New VisitingDirectoryEventArgs(path))  
     
            ' Process child directories.   
            For Each s As String In Directory.GetDirectories(path)  
                Traverse(s)  
            Next s  
        End Sub 
     
        ' It is a standard practice to make a protected virtual method   
        ' actually responsible for raising the event. (If your class will   
        ' not be inherited, this is unimportant.)   
        Protected Overridable Sub OnVisitingDirectory(ByVal e As VisitingDirectoryEventArgs)  
            RaiseEvent VisitingDirectory(Me, e)  
        End Sub 
    End Class 
     
    ' This is the event argument class for providing the detailed   
    ' information about the event to the handler.   
    Public Class VisitingDirectoryEventArgs  
        Inherits EventArgs  
     
        Private mPath As String 
     
        Public ReadOnly Property Path() As String 
            Get 
                Return mPath  
            End Get 
        End Property 
     
        Public Sub New(ByVal path As String)  
            mPath = path  
        End Sub 
    End Class 
     
    Public Class Form1  
     
        Private Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgs) Handles Button1.Click  
            Dim dt As New DirectoryTraverser()  
            AddHandler dt.VisitingDirectory, AddressOf DirectoryTraverser_VisitingDirectory  
            dt.Traverse("c:\test")  
        End Sub 
     
        Private Sub DirectoryTraverser_VisitingDirectory(ByVal sender As ObjectByVal e As VisitingDirectoryEventArgs)  
            Debug.WriteLine(e.Path)  
        End Sub 
     
    End Class 
     
    • Marked as answer by Zhi-Xin Ye Tuesday, November 25, 2008 3:39 AM
    • Unmarked as answer by -dFx- Wednesday, December 3, 2008 10:45 PM
    • Marked as answer by -dFx- Thursday, December 4, 2008 12:03 PM
    Friday, November 21, 2008 11:43 PM
  • i'm still unable to understand the procedure and how to reproduce it.

    lets try on a simple sub.

    there is a sub :

    private sub make_rand()
        dim number as new System.Random
        for i as integer = 0 to 1000
            number.NextDouble()
        next
    end sub


    If i want to create an event, without derivative class without any complicated thing.

    What is the most simple declaration usable (event used in same class) ?


    Thanks

    dFx
    Wednesday, December 3, 2008 10:51 PM
  • Don't use an event in the same class.  Just call a method.  Events are for other classes to get notified about something interesting, customers that your class is not aware of.  Also hit the books and read up on virtual methods, which I think is what you are looking for.
    Hans Passant.
    • Marked as answer by -dFx- Thursday, December 4, 2008 11:01 AM
    • Unmarked as answer by -dFx- Thursday, December 4, 2008 11:01 AM
    Wednesday, December 3, 2008 11:44 PM
  • your true, my example was wrong.

    The code used in the sub, is normaly to find files in path. The sub is a recursive.

    This sub is contained in a simple class + namespace. This class is in a .dll project.

    what i want is, when the sub is called, each time its browse an other path, it triggers an event that informs my program that the path changed and what is it.

    I don't have an idea of derivative class. I don't use it ( i think).



    dFx
    Thursday, December 4, 2008 11:05 AM
  • ideFixxx said:

    I don't have an idea of derivative class. I don't use it ( i think).

    Which is perhaps the point.  You got good answers in this thread but you don't know how to use them.  You'll have to educate yourself first.

    Hans Passant.
    Thursday, December 4, 2008 11:21 AM
  • right.

    A start point suggested ?

     

    dFx
    Thursday, December 4, 2008 11:24 AM
  • Did you consider a school?  Or your local library?
    Hans Passant.
    Thursday, December 4, 2008 11:47 AM
  • i thought that msdn got greats how to tutorials ...

    school ? for a free time coder ?

    dFx
    Thursday, December 4, 2008 12:02 PM
  • after many shearches and some free source code views, i created this :

    The code returns only a random.

    code in dll :
        Public Class EventSpl 
            Public Event dong(ByVal Msg As String
     
            Public Sub do_dong() 
                Dim rand As System.Random 
                rand = New Random 
                RaiseEvent dong(rand.NextDouble) 
            End Sub 
        End Class 

    code in apps :
        Dim WithEvents maclass As New dFx2.EventSpl 'dFx2 is the namespace of my dll 
     
        Private Sub Btn_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Btn.Click 
            maclass.do_dong() 
        End Sub 
     
        Private Sub maclass_Dong(ByVal message As StringHandles maclass.dong 
            MsgBox(message) 
        End Sub 

    So my question is :
    Is there an other way to declare my event that declaring it with a dim in the using-class ?



    dFx
    Thursday, December 4, 2008 2:40 PM
  • I don't see a Dim in your event declaration.  Nor a "using-class".  The code is otherwise correct.
    Hans Passant.
    Thursday, December 4, 2008 6:34 PM
  • worried about this :

    Dim WithEvents maclass As New dFx2.EventSpl 

    There isn't  any other methods to declare it ?
    Isn't it possible to declare it in a sub and to manage the event in the same sub ?

    Thanks


    dFx
    Thursday, December 4, 2008 11:49 PM