Referencing "Forms" objects from a module

  • Thread starter Thread starter Paul Helmuth
  • Start date Start date
P

Paul Helmuth

All,

(here's an easy one)...

This is probably a stupid question - please bare with me as I am new to
dotNet.

How does one reference objects on a form from a module? In 6.0 you could
simply provide the form name as a prefix to the object within the form and
that would do it (kind of like providing a fully qualified path name to a
file).

What is the mechanism for such a reference from a VB.net module?

Thanks in advance for any insight

-Paul
 
your can easily find answer, in the following code

------------------------------------------------------------------------

Module Module1

Function A_Function()
MessageBox.Show(" i m in module1")
End Function

End Module

------------------------------------------------------------------------

Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Module1.A_Function()

End Sub
End Class
------------------------------------------------------------------------
 
Paul Helmuth said:
All,

(here's an easy one)...

This is probably a stupid question - please bare with me as I am new
to dotNet.

How does one reference objects on a form from a module? In 6.0 you
could simply provide the form name as a prefix to the object within
the form and that would do it (kind of like providing a fully
qualified path name to a file).

What is the mechanism for such a reference from a VB.net module?

Thanks in advance for any insight


If possible, don't use a Module. See previous discussions in this group.

Why don't you put the code in the Form?


Armin
 
Almost.

The code snipit you provided shows the reverse.

You are referncing a module function from a form. I want to reference a form
object from a module. I have tried the 6.0 syntax which I described in my
original post - it's pretty much the same as the "form-to-module" reference
that you have illustrated.

How does one reference a form object from within a module?
 
Paul you can do it by making the instance of the form in module like
shown below:


Module Module1

Function A_Function()

Dim frmInstance As Form1
frmInstance = New Form1

frmInstance.callMeFunction81()

End Function

End Module

* callMeFunction81() will be called which is in the form, Or you can do
what ever you want to do with it, as you got the instance of that form.
 
When you say "objects ON a form", do you mean controls? If not, please
ignore this post. If so, though...

I would do this as a property of the form, exposing the object. This gives
your form control over itself, provides true encapsulation, and allows you
the ability to reject requests for the object, etc., etc. Directly
referencing the control is, IMHO, not the best programming practice for
these reasons.

In the form:

Public ReadOnly Property NameTextBox() As TextBox
Get
NameTextBox = TextBox1
End Get
End Property


In the module:

Dim mForm1 As Form1

Sub Test()
MsgBox(mForm1.NameTextBox.Text())
End Sub

Where NameTextBox is a descriptive name of your text box object, and mForm1
is an object that references your form.

One last thing... I have no idea what your application is, and I don't mean
to preach at all, but you may want to take the encapsulation concept one
step further and expose only values from your control, such as a property
that exposes the text value of a text box.
 
When you say "objects ON a form", do you mean controls? If not, please
ignore this post. If so, though...

I would do this as a property of the form, exposing the object. This gives
your form control over itself, provides true encapsulation, and allows you
the ability to reject requests for the object, etc., etc. Directly
referencing the control is, IMHO, not the best programming practice for
these reasons.

In the form:

Public ReadOnly Property NameTextBox() As TextBox
Get
NameTextBox = TextBox1
End Get
End Property


In the module:

Dim mForm1 As Form1

Sub Test()
MsgBox(mForm1.NameTextBox.Text())
End Sub

Where NameTextBox is a descriptive name of your text box object, and mForm1
is an object that references your form.

One last thing... I have no idea what your application is, and I don't mean
to preach at all, but you may want to take the encapsulation concept one
step further and expose only values from your control, such as a property
that exposes the text value of a text box.
 
If possible, don't use a Module. See previous discussions in this group.
That's just your personal opinion.
Absolutely not, in my opinion exact the right answer from Armin

Cor
 
Armin Zingler said:
If possible, don't use a Module. See previous discussions in this
group.

To clarify this... :-)

It could be understood as: "Don't use a Module. That's the
conlusions of previous discussions.".

What I meant was:
"Don't use a Module. The usage of modules has already been discussed, so
look up the previous threads."

Thus, the last sentence was not to support my own opinion and give a (maybe
wrong) impression of what's the conclusion of previous threads, but it was
only to give a general hint to discussions about this subject (that also
mention advantages of modules).

(IMO, everything in a module is part of "*the* application". "*The*
application" is an object (singleton in this case) => "Class App" with
shared methods (my personal way of thinking only))


Armin
 
Huh ???

Well i guess it is your opinion , and it seems also to be Cor`s opinion so
Herrfried was right when he said that is just your opinion :-)

probably he said this, because if you read the comment someone might think
that it is bad coding practice to use a module at all

regards

Michel Posseth [MCP]
 
Well i guess it is your opinion , and it seems also to be Cor`s opinion
so Herrfried was right when he said that is just your opinion :-)


Ik zal het voor jouw in het nederlands doen.

Als het Armin en mijn opinion is, dan is het niet *alleen maar* de
persoonlijke opinie van Armin, maar een opinie van meer personen.

Translated.

I'll do it for you in Dutch.

If it is the opinion of Armin and mine opinion, than it is not *just* the
personal opinion from Amrin, however an opinion from more persons.

For the rest is real developping a going on traject where what was yesterday
right is tomorrow wrong.

The pleasure of Visual Basic Net is that it is not so commanding as other
languages. Because I have read many messages from you, is my believe that we
share that opinion.

Cor
 
m.posseth said:
Huh ???

Well i guess it is your opinion , and it seems also to be Cor`s
opinion so Herrfried was right when he said that is just your
opinion :-)

probably he said this, because if you read the comment someone might
think that it is bad coding practice to use a module at all

But that's what I wrote in my previous post!?? I saw that it could have been
misunderstood - as Herfried probably did - thus the clarification.


Armin
 
Armin , Cor

i just wanted to make clear that a opinion is not a fact , even if it is
shared by multiple persons

from my personal point of view this was not clear in the thread , it looked
to me as if it is bad coding practice to use a module
But that's what I wrote in my previous post!??

I missed the previous post that was indeed exactly the facto that i missed
before .
It arived on my computer after i wrote the response i read the newsgroup
through nova.planet.nl and through news.microsoft.com on different
computers , the planet server seems to be late with its delivery sometimes
..

I am with you if you say that , you should try to avoid the usage of modules
( although this is also a personal opinion ;-)

regards

Michel Posseth [MCP]
 
m.posseth said:
Armin , Cor

i just wanted to make clear that a opinion is not a fact , even if
it is shared by multiple persons

from my personal point of view this was not clear in the thread , it
looked to me as if it is bad coding practice to use a module


I missed the previous post that was indeed exactly the facto that
i missed before .
It arived on my computer after i wrote the response i read the
newsgroup through nova.planet.nl and through news.microsoft.com
on different computers , the planet server seems to be late with
its delivery sometimes .

Ok, I see. :-)
I am with you if you say that , you should try to avoid the usage of
modules ( although this is also a personal opinion ;-)


But isn't this your personal opinion only? ;-))) Don't answer, I do
understand. :)


Armin
 
Michel,

You probably have seen that I can as well use the nova newsserver.

However, I use direct the microsoft newsserver, it is quicker and has in my
opinion exactly the right retention time.

news://news.microsoft.com/microsoft.public.dotnet.languages.vb

Why don't you try it yourself.

I can not remember me that I forever wrote something as a fact here in these
newsgroups. I have to often seen that what is a fact today was rubish
tomorrow. Maybe is that the reason that I react forever on people who write
things from which they tell it are facts.

Even 1+1 is not always 2 when option strict is off.

Cor
 

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