Unable to reference form field...

B

Brad Pears

I have the following code that references a "textbox" on a form. I want to
pass the value of this textbox to a stored procedure as a parameter. This
code is located on a different form obviously. I thought that I would be
able to reference the textbox simply by placing the name of the form it is
located on (class name) in front as follows...

param1 = frmSteelPlates.txtJobNo.Text

I am getting the following error when I debug the line to see what the value
of the text box actually is...

"Reference to a non-shared member requires an object reference"

What am I missing here?

Thanks, Brad
 
K

kimiraikkonen

I have the following code that references a "textbox" on a form. I want to
pass the value of this textbox to a stored procedure as a parameter. This
code is located on a different form obviously. I thought that I would be
able to reference the textbox simply by placing the name of the form it is
located on (class name) in front as follows...

param1 = frmSteelPlates.txtJobNo.Text

I am getting the following error when I debug the line to see what the value
of the text box actually is...

"Reference to a non-shared member requires an object reference"

What am I missing here?

Thanks, Brad

Without seeing your full code, i found that suggestion which may work
for you:
http://www.velocityreviews.com/forums/t95455-reference-to-nonshared-member.html

Thanks,

Onur Güzel
 
B

Brad Pears

Unfortunately that solution appears to be specific to ASP... I will jsut use
a public variable for now but I'd like to get to the bottom of that issue...

Thanks for the response...

Brad

I have the following code that references a "textbox" on a form. I want to
pass the value of this textbox to a stored procedure as a parameter. This
code is located on a different form obviously. I thought that I would be
able to reference the textbox simply by placing the name of the form it is
located on (class name) in front as follows...

param1 = frmSteelPlates.txtJobNo.Text

I am getting the following error when I debug the line to see what the
value
of the text box actually is...

"Reference to a non-shared member requires an object reference"

What am I missing here?

Thanks, Brad

Without seeing your full code, i found that suggestion which may work
for you:
http://www.velocityreviews.com/forums/t95455-reference-to-nonshared-member.html

Thanks,

Onur Güzel
 
J

Jack Jackson

I have the following code that references a "textbox" on a form. I want to
pass the value of this textbox to a stored procedure as a parameter. This
code is located on a different form obviously. I thought that I would be
able to reference the textbox simply by placing the name of the form it is
located on (class name) in front as follows...

param1 = frmSteelPlates.txtJobNo.Text

I am getting the following error when I debug the line to see what the value
of the text box actually is...

"Reference to a non-shared member requires an object reference"

What am I missing here?

Thanks, Brad

If frmSteelPlates is a reference to the other form, and if txtJobNo is
marked Public, then it would work.

My guess is that frmSteelPlates is a class name, not a reference to an
instance of the class.

I don't think it is good practice to make controls be Public and
reference them from somewhere else. It ties the code too closely to
how you chose to implement the form.

I would add to the form that contains the textbox a Public Function
that returns the value, something like:

Public Function GetJobNo() As String
Return txtJobNo.Text
End Function

Then arrange for the form that calls the stored procedure to have a
reference to the form that has the job number.
 
B

Brad Pears

Ok, that sounds good. I will implement it like that instead. In teh meantime
I decided to use a public variable (pJobNo) which works fine, but I prefer
not to use them if there is a better method that is not too difficult of an
implementation.

Thanks, Brad
 
B

Brad Pears

One other quick question...

When you said

" Then arrange for the form that calls the stored procedure to have a
reference to the form that has the job number."

I'm just not sure what you mean there...

Basically I want to call your function "GetJobNo" in a completely different
form (the one that calls the stored procedure) than the one that fucntion
"GetJobNo" was declared public in. I can't do that unless I pass the actual
entire form by reference to the new form - is that what you mean?

Could you give me a quick example of what you are referring to there?

Thanks, Brad
 
J

Jack Jackson

Yes. You need a reference to the form that contains the textbox
(saying "pass the actual entire form by reference" doesn't really mean
anything, a reference is a pointer to the instantiation of the class).

You can get the reference in a couple of ways. If the form with the
textbox starts the other form, then it is easy. If the second form is
a modal dialog box then you will have code like:

Using frm2 As New Form2
frm2.ShowDialog()
End Using

To pass a reference to the calling form you can either pass it as a
parameter to the form's New or call a method:

Using frm2 As New Form2(Me)
or
Using frm2 As New Form2
frm2.SetCallingForm(Me)
frm2.ShowDialog()
End Using

You could also look through the application's list of forms to find
the form you want (System.Windows.Forms.Application.OpenForms).
 
C

Cor Ligthert[MVP]

Brad,

A class is only a template, this is a little bit confusing for former VB6
users, as in that language where AFAIK classes always shared classes, and
therefore in fact modules.

The form class is now a real class. And therefore you have to instance it
from your current class to an obect.

Therefore as you show us how you have instanced frmSteelPlates, then some of
us can tell you probably more.

(There are and endles quantity of methods to create an form object)

Cor
 
J

Joergen Bech

Brad,
Cor,

A class is only a template, this is a little bit confusing for former VB6
users, as in that language where AFAIK classes always shared classes, and
therefore in fact modules.

In VB6, a class must be stored in a single file and a single file
cannot contain more than one class. Still, except for the lack
of inheritance, classes in VB6 work pretty much like those in
VB.Net, albeit a little crippled by comparison.

A default instance of a form in VB6 can be referenced without
instantiation, but it is also possible to create multiple, non-default
instances of a form.
The form class is now a real class. And therefore you have to instance it
from your current class to an obect.

This was true in VS2002 and VS2003, but VS2005 (re)introduced
the default instance model known from VB6, so in VB.Net 2005+ it
is now possible to write

Sub main()
Form1.ShowDialog()
End Sub

i.e. display the default instance of a form without declaring
it first. This is (now) handled behind the scenes.

If all you ever need is to display one instance of the same
form class at a time, the default instance can be used.

My personal opinion is that Microsoft never should have
added the default instance stuff but kept forcing developers
to keep things explicit, i.e. out in plain view. Less opportunity
for errors and confusion.
Therefore as you show us how you have instanced frmSteelPlates, then some of
us can tell you probably more.

I'll second that.

Regards,

Joergen Bech
 

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

Top