Overloads - DRY

  • Thread starter Thread starter valamas
  • Start date Start date
V

valamas

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
 
valamas said:
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

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
 
Back
Top