"valamas" <an*******@disc ussions.microso ft.com> wrote:
Hi All
How can I recode the following so that I do not repeat my
code?
Public Overloads Sub SetNodeImageInd ex(ByVal oNode As
System.Windows .Forms.TreeNode , ByVal ImageIndex As String)
oNode.ImageInde x = ImageIndex
oNode.SelectedI mageIndex = ImageIndex
End Sub
Protected Overloads Sub SetNodeImageInd ex(ByVal e As
System.Windows .Forms.TreeView CancelEventArgs , ByVal
ImageIndex As Long)
e.Node.ImageInd ex = ImageIndex
e.Node.Selected ImageIndex = ImageIndex
End Sub
regards, valamas
I must be missing something because the obvious DRY (Do not
Repeat Yourself) solution is:
Public Overloads Sub SetNodeImageInd ex( _
ByVal oNode As System.Windows. Forms.TreeNode, _
ByVal ImageIndex As String _
)
' Delegate actual work to one common method
SetNodeImageInd exCommon(oNode, CInt(ImageIndex ))
End Sub
Protected Overloads Sub SetNodeImageInd ex( _
ByVal e As System.Windows. Forms.TreeViewC ancelEventArgs, _
ByVal ImageIndex As Long _
)
' Delegate actual work to one common method
SetNodeImageInd exCommon(e.Node , CInt(ImageIndex ))
End Sub
Private Sub SetNodeImageInd exCommon( _
ByVal oNode As System.Windows. Forms.TreeNode, _
ByVal ImageIndex As Integer _
)
' Do actual work in only one place
oNode.ImageInde x = ImageIndex
oNode.SelectedI mageIndex = ImageIndex
End Sub
Or even
Public Overloads Sub SetNodeImageInd ex( _
ByVal oNode As System.Windows. Forms.TreeNode, _
ByVal ImageIndex As String _
)
' Do actual work in only one place
oNode.ImageInde x = CInt(ImageIndex )
oNode.SelectedI mageIndex = CInt(ImageIndex )
End Sub
Protected Overloads Sub SetNodeImageInd ex( _
ByVal e As System.Windows. Forms.TreeViewC ancelEventArgs, _
ByVal ImageIndex As Long _
)
' Delegate actual work to one common method
SetNodeImageInd ex(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