How do I call a method of another from

  • Thread starter Thread starter Partha Protim Roy
  • Start date Start date
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
 
Your point is noted. I think you don't like to be bettered, and hence will
always look for an argument to make yourself so.

Good luck in your endeavours, I think that you probably don't get much sleep
at night worrying about how you could improve yourself.


Stay Lucky - OHM

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
One Handed Man ( OHM - Terry Burns ) said:
Your point is noted. I think you don't like to be bettered, and hence will
always look for an argument to make yourself so.

A statement like ' I like my code better ' is of no value unless there are
good reasons why that method is a better choice.

These groups go around the globe, and are archived for several years.
Its one thing to say, 'this is how you do that', but it is completely
different to say 'this is why you do that this way'.

Everyone reading the thread will be better informed if the reasoning
is included (where appropreate) than when just a snippette of code
is posted. That is why I went through the trouble of comparing the
two methods. They both work, so why choose one over the other?

With the added comments, the reader is better informed to make up
their own mind about which to use in their own situation....

There is always the chance that someone else may jump in with
yet a better method. When then happens, in this case, then I might
be in your shoes and find my method was maybe not the best.
But the casual reader/lurker would then be educated even further!

That is what makes these groups such a good tool for learning!

LFS
 
Good Luck - OHM

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
Larry,
With the added comments, the reader is better informed to make up
their own mind about which to use in their own situation....
Depends if it is real a global question, when somebody asks how can I add 5
to 5, I think than that telling why you did use an integer instead of a
double is a little bit overdone.

In my opinion needs a snippet no comments until the OP or somebody else
starts asking questions about it.

However just my opinion.

Cor
 
I agree.

Using ones experience to help others in a positive way engenders a sense of
community with mentoring, However, being aloof and using the shortfalls of
others to widen the (perceived) gap between one's capability and the
capability of others does little for the community and breeds resentment.

It is not always necessary to debate things to the Nth degree just to gain a
final advantage. Whilst I agree all things can be rationalised down to a
finite level to obtain a 'best practice' or 'best solution', life is seldom
kind enough to give us the time to do so, and if we did, we may never
achieve the end game because we spent all our time at the macro level
debating which is best.

OHM
 
Cor Ligthert said:
Depends if it is real a global question, when somebody asks how can I add 5
to 5, I think than that telling why you did use an integer instead of a
double is a little bit overdone.

I agree, and I even said so:
Everyone reading the thread will be better informed if the reasoning
is included (where appropreate) than when just a snippette of code
is posted.

That '(where appropreate)' addresses your concern. In the case you
mention, trying to say why an integer was used, isn't appropreate.

You know how many times a question like this has been asked:

In this thread alone, there were 3 different methods suggested, so
adding a bit of information on deciding which to choose is going
to help those who also have need to pass variables to or from a
second form.

LFS
 
One Handed Man ( OHM - Terry Burns ) said:
However, being aloof and using the shortfalls of
others to widen the (perceived) gap between one's capability and the
capability of others does little for the community and breeds resentment.

I have been participating in newsgroups for several years. They are an
excellent medium to hold informative discussions. One thing that is pretty
common is that nobody likes hearing someone say 'Your method is wrong'
when the darn thing works and gets the job done.

But I didn't say that, the major reason I continued at all was because of
Cor's reply:
By instance for a showdialog the method from OHM is in my opinion the
best solutions for the question.

If it turns out there are better solutions, then it would be in everyone's best interest
to get Cor to change his opinion to the better solution so that the next time that
question gets asked, he can offer the better solution. As you know Cor posts a
lot of messages, and helps many people, so getting him to see 'why' one solution is
better than another will 'maybe' persuade him to change his opinion of the best
solution for the task.

It is not always necessary to debate things to the Nth degree just to gain a
final advantage.

See above, this particular question is asked fairly often, (and may continue
to be asked often) so getting people who post replies familiar with a
'best practises' solution is going to benefit the most people.
Whilst I agree all things can be rationalised down to a
finite level to obtain a 'best practice' or 'best solution', life is seldom
kind enough to give us the time to do so, and if we did, we may never
achieve the end game because we spent all our time at the macro level
debating which is best.

I did not intend to be aloof, or to put down anyone. Look at it as you
would a bit of optimization. You don't optimize every line of code and
every routine in your program. You profile where it would do the most
good, and concentrate on that. As I said, Cor answers a lot of questions,
and and many listen to what he says. I also know that he may see this same
question again and again. Therefore, if I explain why one solution works
better than others, and if he is open enough to change his opinion to the
better solution, then many others who ask this same question have a better
chance at hearing the 'best practises' solution, and not the one you agreed
is not the best one for the job.

I am not singling out Cor as the only one who answers questions, he does
post a lot however. I can only wonder what your response will be the next
time you see a similar question!

<g>
LFS
 
It's just in your nature Larry, admit it, you just have to have the last
word don't you ?

;-)

Your points are noted. The important thing is however, is how you put those
points across. Try being a little less challenging, coaxing a person to see
where they could improve their code by 'pleasantly' asking the right
questions is far more likely to bring about a state of cognition in the
respondent, than making lightly veiled derisory remarks which usually cause
a push back.

I can see that you are equipped to make a cogent argument for yourself in
the subject matter, however, in a community 'Even an on-line community' such
as this. Relationships between contributors are everything.

The written word, can sometimes convey emotions which were never intended
and stimulate resentment and bad feeling, so sometimes one has to be overly
polite when talking about potentially inflaming things.

I hope you can see the rational I am trying to put across.


--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
One Handed Man ( OHM - Terry Burns ) said:
It's just in your nature Larry, admit it, you just have to have the last
word don't you ?

There's a catch 22 question if there ever was one....
Your points are noted. The important thing is however, is how you put those
points across. Try being a little less challenging,

Noted....

I hope you can see the rational I am trying to put across.

I do, and hope later encounters will show some improvement on my end....

Thanks!
LFS
 
Hi Larry

What did you construct in the message above I only wrote the first part.

I don't know the rest which seems from me, I do not like that.

Cor
 
Your welcome. No hard feelings here.

END

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
Larry,
But I didn't say that, the major reason I continued at all was because of
Cor's reply:
Do you mean the last messages I have send in what I said that I saw it wrong
in my first message and that the method from Terry was probably not the
best. Do you now tell that the method from Terry is the best?

However the OP has only told this.
----------------------------------------------------------------------------
I have a Customer form say A to enter/update customer details. In the Form
A I have a button which opens another form say B.

In the Form B, I am providing user with a option to search Customer from the
database depending on various search criteria.
------------------------------------------------------------------------------

When this is about two MDI forms the change is that all your solutions would
probably have been completly wrong. (I did not investigage this whole
thread). Terry did it better, so he can maybe tell that?

Cor
 
Cor Ligthert said:
What did you construct in the message above I only wrote the first part.

I don't know the rest which seems from me, I do not like that.

I do not mean to insult you. Read in context, my message explains
that you already know the question "How to pass data from form to form"
gets asked a lot.

That is what was meant by:

---------------
You know how many times a question like this has been asked:
--------------

I said; you have seen this question many times. Nothing more.

After that I said:

"adding a bit of information on deciding which to choose is going
to help those who also have need to pass variables to or from a
second form"

I also do not like to be quoted 'out of context', but this was not
a bad quote 'out of context'. It was an affirmation (by agreeing) with
what you said.

HTH
LFS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top