property access from the form

V

Vivek Sharma

Hi,

I have a form in a windows application. In that form I have declared the
public property InvoiceNo. I am trying to access this property from the
other form. In the other form I declared the first form as

public frmInvoice as InvoiceForm

after the declaration I have on load event in which I am trying to access
the property


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

dim inv as string

inv = frmInvoice.InvoiceNo
End Sub



I get the error of the Object reference not set to an instance of an object.
Now I can fix it by declaring

public frmInvoice as new InvoiceForm

but then I get frmInvoice.InvoiceNo as NOTHING

Please help
 
C

Cerebrus

Hi,

Try to conceptualise what you are trying to do, first.

A form is just an instance of a class, so we shouldn't treat it as if
it were something else.

Let us look at what you were trying to do, in order to understand the
problem :
(Assume that your ReportViewer Form is Class 2 and frmInvoice is Class
1)

1. Trying to access Class 1 from Class 2.
Problem : Class 1 has not been instantiated. No object has been
created.
Error : Null Reference exception.
Solution : Instantiate Class1. You did this by the statement "public
frmInvoice as new InvoiceForm()"

2. Trying to access the property "InvoiceNo" of Class 1 from Class 2.
Problem : Class 1 has been instantiated, but the property contains
Nothing. In short, you
are trying to access a property that has not been set.
Solution : Set a default value for the property in Class 1 code.

This solves the problem, but I don't think it is what you are trying to
do. Instead, I
believe you already have Class 1 (the InvoiceForm) running, and you
want to access it's
InvoiceNo property from Class 2 (the ReportViewer form).

This means that we must not initialize InvoiceForm again. We must
somehow get a reference to the already running InvoiceForm.

So, the question boils down to : "How to transfer the value of property
InvoiceNo in frmInvoice to ReportViewer". (Your original question was
"How to access the property InvoiceNo in frmInvoice from ReportViewer).
Can you see the difference ? You were thinking in the opposite
direction.

This can be solved simply by : (Assuming ReportViewer is loaded from
frmInvoice)
---------------------------------------------------
In frmInvoice:
---------------
Public Property InvoiceNo as Integer
:
:
End Property

'This procedure loads ReportViewer...
Private Sub LoadReport()
dim frm2 as New ReportViewer ()
frm2.myInvoiceNo = Me.InvoiceNo
frm2.show()
End Sub


In ReportViewer:
------------------
Friend myInvoiceNo as Integer
---------------------------------------------------
Hope this helps,

Regards,

Cerebrus.
 
C

Cor Ligthert [MVP]

Vivek,

What you have done with

"public frmInvoice as InvoiceForm"

Is creating a public placeholder for the adres of a to create object
frmInvoice

To instance it (to create a New object) you have to use the New word.
That can inside that declaration

\\\
public frimInfoice as New InvoiceForm
///

or otherwise as first statement in your load event.

\\\
frmInvoice = New InvoiceForm
///

Reading the message from Cerebrus again, I see that he writes in a way the
same, so see this as addition with some samples.

I hope this helps,

Cor


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

Similar Threads

Debugging Error 1
Events 3
Passing values between webpages 1
CLS-complaint 5
NotifyIcon Confusion 1
Data binding problem 5
Preventing MCI Client Form From Drawing Borders 1
How do I fix this class? 10

Top