473,413 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Accessing Procedure on another form

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
Nov 21 '05 #1
7 1995

David wrote:
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 Lard
Replies to group please

Nov 21 '05 #2
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"
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...

David wrote:
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 Lard
Replies to group please

Nov 21 '05 #3

David wrote:
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 Lard" <la*******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...

David wrote:
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 Lard
Replies to group please


Nov 21 '05 #4
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 Lard" <la*******@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

David wrote:
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 Lard" <la*******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...

David wrote:
> 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 Lard
Replies to group please

Nov 21 '05 #5
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"

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

David wrote:
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 Lard" <la*******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...

David wrote:
> 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 Lard
Replies to group please

Nov 21 '05 #6
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.

Nov 21 '05 #7
Larry,

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

setting mainform = me has fixed the problem.


"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.

Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: stjulian | last post by:
I have a stored procedure that is supposed to 1. Increment a counter in Table A via a transaction 2. Use this value as the primary key to add in an address to customers Table B (Referenced as a...
1
by: - | last post by:
this is one way of creating a stored procedure in mysql: CREATE PROCEDURE some_procedure (IN value1, OUT value2) BEGIN SELECT ... INTO value2 FROM .... END// and if called from another...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
8
by: Christopher Weaver | last post by:
I'm having trouble accessing the value of an output parameter of a stored procedure. The SP looks like this: SET TERM ^ ; CREATE PROCEDURE SP_NEW_TASK RETURNS ( "uidTask" INTEGER) AS begin
8
by: Mike Caputo | last post by:
In VB.NET, need to be able to access certain properties on my main form from other forms. These are properties that may be changed by the user, so I have to be able to get to them throughout the...
5
by: RSH | last post by:
I havent been able to set a property from another class with out getting some sort of error. Can someone please tell me what I'm doing wrong here? Public Class Form1
6
by: Bob Alston | last post by:
I am looking for Access reporting add-in that would be easy to use by end users. My key focus is on selection criteria. I am very happy with the Access report writer capabilities. As far as...
3
by: Kurtulus | last post by:
Hi; I have problem for very long time. I have one mdiparent form and several mdichild form. I have tried to change one control on the mdi parent form form anther one. However I have encountered an...
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.