Accessing Procedure on another form

  • Thread starter Thread starter David
  • Start date Start date
D

David

Hi,
I am having trouble with a form that I have loaded trying to access a
procedure on the main form.

The trouble seems to be that a Global Array that is declare in a Module is
producing a Nullreference error when I try to call the procedure on the main
form.

ie.
Module 1 contains
Public TXBuff(100) As Byte

Public mainform As Form1

The second form contains code

TXBuff(0) = 2

TXBuff(1) = 13

TXBuff(2) = 6

mainform.Send(3)

And the main form contains code

Public Sub Send(ByVal nbytes As Integer)

RXBytes = 0

DisplayTXBytes(nbytes)

SerialConnection.Write(TXBuff, 0, nbytes)

ReplyTimer.Enabled = True

End Sub

The exact error is ...

An unhandled exception of type 'System.NullReferenceException' occurred in
Config Utility Source.exe

Additional information: Object reference not set to an instance of an
object.



Thanks in advance for any help
 
David said:
Hi,
I am having trouble with a form that I have loaded trying to access a
procedure on the main form.

The trouble seems to be that a Global Array that is declare in a Module is
producing a Nullreference error when I try to call the procedure on the main
form.

ie.
Module 1 contains
Public TXBuff(100) As Byte

This *declares* TXBuff to be a variable of type 'array of 100 Byte',
but doesn't actually *define* such an array. Remember, in VB.NET Arrays
are now reference types, not value types (ie they act more like Objects
than, say, Integers). Therefore you must actually create an array of
100 Byte for this variable to reference, otherwise when you refer to
TXBuff(0) you are using a null reference.

The simple fix is to change

Public TXBuff(100) As Byte

to

Public TXBuff(100) As Byte = New Byte(100) {}

This will mean that at initialisation time, a new array of 100 Bytes
will be created, and TXBuff set to point to it.

The changes to arrays are one of the key language change areas
referenced in the MSDN/VStudio help, I think.
 
Larry,

Thanks for the response.

When I place this in the Module, the following error occurs.

"Explicit Initialization is not permitted for arrays declared with explicit
bounds"
 
David said:
Larry,

Thanks for the response.

When I place this in the Module, the following error occurs.

"Explicit Initialization is not permitted for arrays declared with explicit
bounds"

Sorry my mistake, you want

Public TXBuff() As Byte = New Byte(100) {}
 
Larry,

I made the changes and now do not get the explicit initialization error but
still get the null reference error.

If I call the Send routine from the main form instaed of from the second
form, it works fine. The nullreference error only occurs when I try to call
the send routine from another form that I have opened.
 
Larry,

I think the error may actually be in the way I am trying to call the main
form.
If I stop in debug mode and cursor over the call, the dialog shows "mainform
= nothing"
 
You have declared

Public mainform As Form1

but remembering that Forms are *objects*, it sounds like you haven't
explicitly set mainform to the instance of your Form1. If there is only
one Form1 in your app, you can get away with just putting

mainform = Me

in the Form_Load of Form1. Then when other parts of the code use the
mainform variable it will refer to the Form1 that is loaded.
 
Larry,

Thanks for the help with what must be very fundamental issues for you.

setting mainform = me has fixed the problem.
 
Back
Top