L
Larry Serflaten
One Handed Man ( OHM - Terry Burns ) said:OK, your right I conceed that your method will work. I still think that mine
is cleaner and easier to read.
Dim searchForm As New B
Dim DResult As DialogResult
DResult = searchForm.ShowDialog()
Select Case DResult
Case DialogResult.OK
Me.returnedText.Text = searchForm.UserName
Case DialogResult.Cancel
Me.returnedText.Text = "Cancelled"
End Select
searchForm.Close()
Compare those 10 lines to these 7:
Dim User As New Form2
Dim data As String = User.GetData()
If data Is Nothing Then
MsgBox("User canceled")
Else
MsgBox(data)
End If
Yours is easier to read?
//Called Form
Private m_UserName As String
Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles okButton.Click
Me.DialogResult = DialogResult.OK
Me.Hide()
End Sub
What is Hide adding to your code here? (comment it out?)
Now compare your 17 lines of Form2 code to these 12:
Private mUser As String
Public Function GetData() As String
Me.ShowDialog()
Return mUser
End Function
Private Sub OK_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnOK.Click
mUser = TextBox1.Text
Me.Close()
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
Are you still thinking yours is cleaner, and easier to read?
The fact that you require your calling forms to know the proper
property to use after the call is a minor strike against that method
when (as you see) it is not required.
Do as you see fit, they both work, but as you may know, the more
code you add to aprogram, the more there is that can go wrong.
But here is the kicker!
Suppose you wanted to adapt that dialog to a different purpose?
Suppose now, you want to use it to return the name of a country,
or as in the original case, a selection from an entirely different list.
Do you add a new Property called UserCountry? And in the
case of the list, how does the form know which list to load?
In my case, I would add a new Function GetItem and that
new routine could set up the form for the added functionality,
and return the new data.
So, IMHO, my method is a little bit more forward looking,
in that it can easily be made to accomodate added functionality
without requireing changes to the code that already put it to
use.
In your case, you might give it a property to tell it which list
to load, but if you did that, you'd have to go back in your
code to update all those places it was used, to add a line
to set that property to its required value. That 'resistance
to change' also makes the call and query method a bit less
useful....
IMHO you have to keep an eye on allowing your objects
to be modified without causing a major impact on previous
code. If you can do that, without undue jumping through
hoops, then that would be the more favored approach in
my book.
LFS