Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / January 2005

Tip: Looking for answers? Try searching our database.

Overloads - DRY

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
valamas - 30 Jan 2005 05:38 GMT
Hi All

How can I recode the following so that I do not repeat my
code?

Public Overloads Sub SetNodeImageIndex(ByVal oNode As
System.Windows.Forms.TreeNode, ByVal ImageIndex As String)

   oNode.ImageIndex = ImageIndex
   oNode.SelectedImageIndex = ImageIndex

End Sub

Protected Overloads Sub SetNodeImageIndex(ByVal e As
System.Windows.Forms.TreeViewCancelEventArgs, ByVal
ImageIndex As Long)

   e.Node.ImageIndex = ImageIndex
   e.Node.SelectedImageIndex = ImageIndex

End Sub

regards, valamas
Cor Ligthert - 30 Jan 2005 08:42 GMT
Valamas

You cannot when you use both

Cor
UAError - 31 Jan 2005 15:43 GMT
>Hi All
>
[quoted text clipped - 19 lines]
>
>regards, valamas

I must be missing something because the obvious DRY (Do not
Repeat Yourself) solution is:

Public Overloads Sub SetNodeImageIndex( _
   ByVal oNode As System.Windows.Forms.TreeNode, _
   ByVal ImageIndex As String _
)
   ' Delegate actual work to one common method
   SetNodeImageIndexCommon(oNode, CInt(ImageIndex))
End Sub

Protected Overloads Sub SetNodeImageIndex( _
 ByVal e As System.Windows.Forms.TreeViewCancelEventArgs, _
 ByVal ImageIndex As Long _
)
  ' Delegate actual work to one common method
  SetNodeImageIndexCommon(e.Node, CInt(ImageIndex))
End Sub

Private Sub SetNodeImageIndexCommon( _
   ByVal oNode As System.Windows.Forms.TreeNode, _
   ByVal ImageIndex As Integer _
)
   ' Do actual work in only one place
   oNode.ImageIndex = ImageIndex
   oNode.SelectedImageIndex = ImageIndex
End Sub

Or even

Public Overloads Sub SetNodeImageIndex( _
   ByVal oNode As System.Windows.Forms.TreeNode, _
   ByVal ImageIndex As String _
)
   ' Do actual work in only one place
   oNode.ImageIndex = CInt(ImageIndex)
   oNode.SelectedImageIndex = CInt(ImageIndex)
End Sub

Protected Overloads Sub SetNodeImageIndex( _
 ByVal e As System.Windows.Forms.TreeViewCancelEventArgs, _
 ByVal ImageIndex As Long _
)
   ' Delegate actual work to one common method
   SetNodeImageIndex(e.Node, CStr(ImageIndex))
End Sub
'Any fool can write code that a computer can understand.
Good programmers write code that humans can understand.'
Martin Fowler,
'Refactoring: improving the design of existing code', p.15

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.