Results 1 to 7 of 7

Thread: [RESOLVED] Duplicate methods, would like to know how to prevent

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Resolved [RESOLVED] Duplicate methods, would like to know how to prevent

    Hey Guys,

    The Project: TreeGUI, I am currently coding has a Windows Service and a GUI. Both do the same job so I have about 99% of the code shared. There is only one method I don't know how to share with both Windows.Service project and Windows.Form project.

    Here is the code in the Windows.Form

    VB Code:
    1. Private Sub IndexNowTreeNetLib(ByVal myReader As cReader)
    2.  
    3.         Dim where As String
    4.  
    5.         Dim folderList As New ArrayList
    6.         folderList = myReader.GetConfig.FolderList
    7.         Dim treeNetLib As New cTreeNetLib(myReader)
    8.  
    9.         Select Case mIndexMode
    10.  
    11.             Case cReader.IndexingMode.INDEX_FILE_IN_EACH_DIR
    12.  
    13.                 For i As Integer = 0 To myReader.GetConfig.FolderList.Count - 1
    14.                     Dim strDirPath As String = myReader.GetConfig.FolderList.Item(i)
    15.                     where = strDirPath + "\" + myReader.GetConfig.GetIndexFileName
    16.                     Try
    17.                         Dim sw As New StreamWriter(where, False)
    18.                         Select Case myReader.GetConfig.IndexFileExtension
    19.                             Case Is = ".html"
    20.                                 'MessageBox.Show(myReader.GetConfig.mCssFilePath)
    21.                                 treeNetLib.mBooFirstIndexFile = True
    22.                                 treeNetLib.IndexFolderToHtml(strDirPath, sw, True)
    23.                             Case Else
    24.                                 treeNetLib.IndexFolderToTxt(strDirPath, sw, True)
    25.                         End Select
    26.                         sw.Close()
    27.                         If myReader.GetConfig.ZipFilesInEachDir Then
    28.                             myReader.ZipAdminFile(where)
    29.                         End If
    30.                         [COLOR=Red]bwIndexer.ReportProgress(0.0, strDirPath)[/COLOR]
    31.                     Catch ex As System.UnauthorizedAccessException
    32.                     End Try
    33.                 Next
    34.  
    35.             Case cReader.IndexingMode.INDEX_FILE_IN_ONE_FOLDER
    36.  
    37.                 If myReader.GetConfig.isMergeFiles Then
    38.  
    39.                     where = myReader.GetConfig.GetIndexFilePath
    40.  
    41.                     Dim sw As New StreamWriter(where, False)
    42.  
    43.                     If myReader.GetConfig.FolderList.Count > 1 Then
    44.                         For i As Integer = 0 To myReader.GetConfig.FolderList.Count - 2
    45.                             Dim strDirPath As String = myReader.GetConfig.FolderList.Item(i)
    46.                             Dim dir As New cDir(strDirPath)
    47.                             Select Case myReader.GetConfig.IndexFileExtension
    48.                                 Case Is = ".html"
    49.                                     treeNetLib.mBooMoreFilesToCome = True
    50.                                     treeNetLib.IndexFolderToHtml(strDirPath, sw, False)
    51.                                 Case Else
    52.                                     treeNetLib.IndexFolderToTxt(strDirPath, sw, False)
    53.                             End Select
    54.                             [COLOR=Red]bwIndexer.ReportProgress(0.0, strDirPath)[/COLOR]
    55.                         Next
    56.                     End If
    57.  
    58.                     Dim lastDir As New cDir(myReader.GetConfig.FolderList.Item(myReader.GetConfig.FolderList.Count - 1))
    59.                     Select Case myReader.GetConfig.IndexFileExtension
    60.                         Case Is = ".html"
    61.                             treeNetLib.mBooFirstIndexFile = False
    62.                             treeNetLib.IndexFolderToHtml(lastDir.GetDirPath, sw, True)
    63.                         Case Else
    64.                             treeNetLib.IndexFolderToTxt(lastDir.GetDirPath, sw, True)
    65.                     End Select
    66.                     sw.Close()
    67.                     If myReader.GetConfig.ZipMergedFile Then
    68.                         myReader.ZipAdminFile(where)
    69.                     End If
    70.  
    71.                     [COLOR=Red]bwIndexer.ReportProgress(0.0, lastDir.GetDirPath)[/COLOR]
    72.                 End If
    73.  
    74.         End Select
    75.  
    76.     End Sub

    And here is the code for in the Windows.Service:

    VB Code:
    1. Private Sub IndexNowTreeNetLib(ByVal myReader As cReader)
    2.  
    3.         Dim where As String
    4.  
    5.         Dim folderList As New ArrayList
    6.         folderList = myReader.GetConfig.FolderList
    7.         Dim treeNetLib As New cTreeNetLib(myReader)
    8.  
    9.         Select Case mIndexMode
    10.  
    11.             Case cReader.IndexingMode.INDEX_FILE_IN_EACH_DIR
    12.  
    13.                 For i As Integer = 0 To myReader.GetConfig.FolderList.Count - 1
    14.                     Dim strDirPath As String = myReader.GetConfig.FolderList.Item(i)
    15.                     where = strDirPath + "\" + myReader.GetConfig.GetIndexFileName
    16.                     Try
    17.                         Dim sw As New StreamWriter(where, False)
    18.                         Select Case myReader.GetConfig.IndexFileExtension
    19.                             Case Is = ".html"
    20.                                 'MessageBox.Show(myReader.GetConfig.mCssFilePath)
    21.                                 treeNetLib.mBooFirstIndexFile = True
    22.                                 treeNetLib.IndexFolderToHtml(strDirPath, sw, True)
    23.                             Case Else
    24.                                 treeNetLib.IndexFolderToTxt(strDirPath, sw, True)
    25.                         End Select
    26.                         sw.Close()
    27.                         If myReader.GetConfig.ZipFilesInEachDir Then
    28.                             myReader.ZipAdminFile(where)
    29.                         End If                        
    30.                     Catch ex As System.UnauthorizedAccessException
    31.                     End Try
    32.                 Next
    33.  
    34.             Case cReader.IndexingMode.INDEX_FILE_IN_ONE_FOLDER
    35.  
    36.                 If myReader.GetConfig.isMergeFiles Then
    37.  
    38.                     where = myReader.GetConfig.GetIndexFilePath
    39.  
    40.                     Dim sw As New StreamWriter(where, False)
    41.  
    42.                     If myReader.GetConfig.FolderList.Count > 1 Then
    43.                         For i As Integer = 0 To myReader.GetConfig.FolderList.Count - 2
    44.                             Dim strDirPath As String = myReader.GetConfig.FolderList.Item(i)
    45.                             Dim dir As New cDir(strDirPath)
    46.                             Select Case myReader.GetConfig.IndexFileExtension
    47.                                 Case Is = ".html"
    48.                                     treeNetLib.mBooMoreFilesToCome = True
    49.                                     treeNetLib.IndexFolderToHtml(strDirPath, sw, False)
    50.                                 Case Else
    51.                                     treeNetLib.IndexFolderToTxt(strDirPath, sw, False)
    52.                             End Select
    53.                         Next
    54.                     End If
    55.  
    56.                     Dim lastDir As New cDir(myReader.GetConfig.FolderList.Item(myReader.GetConfig.FolderList.Count - 1))
    57.                     Select Case myReader.GetConfig.IndexFileExtension
    58.                         Case Is = ".html"
    59.                             treeNetLib.mBooFirstIndexFile = False
    60.                             treeNetLib.IndexFolderToHtml(lastDir.GetDirPath, sw, True)
    61.                         Case Else
    62.                             treeNetLib.IndexFolderToTxt(lastDir.GetDirPath, sw, True)
    63.                     End Select
    64.                     sw.Close()
    65.                     If myReader.GetConfig.ZipMergedFile Then
    66.                         myReader.ZipAdminFile(where)
    67.                     End If
    68.  
    69.                 End If
    70.  
    71.         End Select
    72.  
    73.     End Sub

    As you now see, both methods are indentical, but the only difference in Windows.Forms method is that it reports Progress to a ProgressBar.

    I can't seperate the method from the Form because it won't then report the progress to the ProgressBar in the Windows.Form.

    Well, this is what the project is all about: http://microsoftuse.temp.powweb.com/...loads/treegui/

    I hope you understand my problem. Is there any workground for this?

    Thanks in advance.
    McoreD

  2. #2
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Duplicate methods, would like to know how to prevent

    YOu could put the method in a class, with a structure similar to this:

    VB Code:
    1. Public Class myIndexer
    2.  
    3.    Private Percent_complete As Integer
    4.    Public Property Percent As Integer 'Or whatever type you need it to be
    5.      Get
    6.         return Percent_complete
    7.      End Get
    8.      Set (Value)
    9.         Percent_complete = value
    10.      End Set
    11.    End Property
    12.    Public Sub IndexNow(ByVal myReader as cReader)
    13.       'Implementation the same as yours
    14.       'Except that instead of the report progress method, something is done
    15.       'to the percent_complete variable
    16.    End Sub
    17. End Class

    And then compile the class to a DLL and put it in the Global Assembly Cache, and call the percent property at some interval from within your forms application. This would enable you to in the future optimize or work on just one version of the program which could be accessed from both versions.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Re: Duplicate methods, would like to know how to prevent

    That's an excellent idea, thanks conipto.

    Let me focus on one For Each loop:

    VB Code:
    1. For i As Integer = 0 To myReader.GetConfig.FolderList.Count - 1
    2.                     Dim strDirPath As String = myReader.GetConfig.FolderList.Item(i)
    3.                     where = strDirPath + "\" + myReader.GetConfig.GetIndexFileName
    4.                     Try
    5.                         Dim sw As New StreamWriter(where, False)
    6.                         Select Case myReader.GetConfig.IndexFileExtension
    7.                             Case Is = ".html"
    8.                                 'MessageBox.Show(myReader.GetConfig.mCssFilePath)
    9.                                 treeNetLib.mBooFirstIndexFile = True
    10.                                 treeNetLib.IndexFolderToHtml(strDirPath, sw, True)
    11.                             Case Else
    12.                                 treeNetLib.IndexFolderToTxt(strDirPath, sw, True)
    13.                         End Select
    14.                         sw.Close()
    15.                         If myReader.GetConfig.ZipFilesInEachDir Then
    16.                             myReader.ZipAdminFile(where)
    17.                         End If
    18.                         [COLOR=Red]bwIndexer.ReportProgress(0.0, strDirPath)[/COLOR]
    19.                     Catch ex As System.UnauthorizedAccessException
    20.                     End Try
    21.                 Next

    In this Loop, say when FolderList.Count = 6, ProgressBar will increment upto 6. In other words, ReportProgress method is called 6 times. But when the Function is outside of the Form, you can only call the Function once, and get the ReportProgress value once. So to fix that proble, like you said, we have to call the PercentageDone variable from time to time. I suppose this is done via a Timer, say every 100 milliseconds etc? If Timer is a solution then I am in the right track thanks to you.

    Cheers,
    McoreD

  4. #4
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Duplicate methods, would like to know how to prevent

    Or, you could be even cooler, and raise an event each time it is incremented.

    Something like this in your DLL class:
    VB Code:
    1. For i As Integer = 0 To myReader.GetConfig.FolderList.Count - 1
    2.                     Dim strDirPath As String = myReader.GetConfig.FolderList.Item(i)
    3.                     where = strDirPath + "\" + myReader.GetConfig.GetIndexFileName
    4.                     Try
    5.                         Dim sw As New StreamWriter(where, False)
    6.                         Select Case myReader.GetConfig.IndexFileExtension
    7.                             Case Is = ".html"
    8.                                 'MessageBox.Show(myReader.GetConfig.mCssFilePath)
    9.                                 treeNetLib.mBooFirstIndexFile = True
    10.                                 treeNetLib.IndexFolderToHtml(strDirPath, sw, True)
    11.                             Case Else
    12.                                 treeNetLib.IndexFolderToTxt(strDirPath, sw, True)
    13.                         End Select
    14.                         sw.Close()
    15.                         If myReader.GetConfig.ZipFilesInEachDir Then
    16.                             myReader.ZipAdminFile(where)
    17.                         End If
    18.                         percent +=1
    19.                         'bwIndexer.ReportProgress(0.0, strDirPath)
    20.                         RaiseEvent (PercentChanged, New EventArgs)
    21.                     Catch ex As System.UnauthorizedAccessException
    22.                     End Try
    23.                 Next
    24.  
    25.      'and outside of your update method but still within the class for the updater
    26.      Public Event PercentChanged(ByVal ea As EventArgs)'Adding the eventargs is really just if you want to add more functionality to the event, so optional.
    27.  
    28.  
    29.      'Then from within your form or service, dim the class variable withevents,
    30.      Dim WithEvents myupdaterClass as new UpdaterClass
    31.  
    32.      'and add a sub to handle the percent changed event
    33.      Private Sub MyPercChangedSub(byval e as EventArgs) Handles myupdaterClass.PercentChanged
    34.          'Update your progress bar based on the percent property
    35.      End Sub

    That's a nice clean way to do it in my opinion, that doesn't waste timer checks when for example progress hasn't been made. I know it's minor, but this IS Code it Better, right Though, I'm sure someone will disagree with it, but that's the beauty of this forum

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Re: Duplicate methods, would like to know how to prevent

    This is very cool conipto. I was just about to post a reply thanking you and to say how Timer worked well. However for operations that took less than 100 milliseconds, ProgressBar did not uptdate accurately (left at 0). So RaiseEvents will be a cool way to do it.

    Thanks again,
    McoreD

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Re: Duplicate methods, would like to know how to prevent

    (Not related to the Thread but...) Looks like there is a limitation in VB according to my design.

    Derived classes cannot raise base class events

    http://msdn2.microsoft.com/en-us/library/0ecakwbz.aspx

    TreeLib and TreeNetLib are two indexing Engines which inherits TreeEngine.

    An event can be raised only from the declaration space in which it is declared. Therefore, a class cannot raise events from any other class, even one from which it is derived.

    Error ID: BC30029
    To correct this error

    Move the Event statement or the RaiseEvent statement so they are in the same class.

    I might need to do that and change some other code accordingly.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Re: Duplicate methods, would like to know how to prevent

    Unfortunately this Multithreading error hit in the middle:

    Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.

    Though I cannot implement it to this project, I learnt a lot from you about EventArgs and am able implement it to any other single threaded project. So thank you again conipto, for your excellent ideas.

    Cheers,
    McoreD

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width