473,320 Members | 1,722 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,320 software developers and data experts.

Inherited Form with error Object Reference not set...

Hello All,

I have encountered a problem.
I am using visual inheritance and my base form adds an event handler on Form
Load using the AddHandler Keyword.

The problem is that if the Event Handler code is there, when I create the
inherited form I get the error "Object Reference not set to an instance of
an object". If it is not I get no error.
I have tried leaving this code in the forms Sub New, but it produces the
same error.
I need the handler to be added during the form load and removed on the form
unload. I cannot use the withevents keyword because the class is declared on
the global level, and it is actually a property of this globally declared
class. Some code to help:
-//-

On a global Module:
Friend g_objTables As TableOps

On the application's Sub Main:
g_objTables = New TableOps(AppSettings.ConnectionString)

On the Base Class:
Private Sub frmBase1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

AddHandler m_btObj.DataChanged, AddressOf CostCentersDataChanged
AddHandler m_btObj.Range.RangeChanged, AddressOf RangeChanged

End Sub

Private Sub CostCentersDataChanged() 'handling DataChanged Event

'various steps taken to handle the event

End Sub

Private Sub RangeChanged()

'various steps taken to handle the event

End Sub

Private Sub frmBase1_Closed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Closed

RemoveHandler m_btObj.DataChanged, AddressOf CostCentersDataChanged
RemoveHandler m_btObj.Range.RangeChanged, AddressOf RangeChanged

End Sub
On the Derived Class:
Private Sub frmCostCenters_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

m_btObj = g_objTables.CostCenters

End Sub

-//-

If I comment the two code lines in frmBase1_Load I get no error. If I
don't...
Also, if I try calling something else, like Msgbox("Test"), it works fine.

Does anyone know why that's happening? Can't I add event handlers in the
Form Load?

Thanks a lot in advance for all the replies.

Giovanni Bassi

Nov 20 '05 #1
2 5282
i usually add handlers in sub new() after mybase.new() and
initializecomponent()...i haven't had problems. btw, you could try this in
your form class:

private withevents m_btObj as TableOps.CostCenters = g_objTables.CostCenters
' or set it to g_objTables in your mybase.load delegate.

that should work just fine...your code w/b:

delegateFunctionBlahBlahBlah(...) handles m_btObj.Blah()

that way you can leave out the explicit handers (add/remove) and let the
framework handle the delegate resources.

hth,

steve

btw...you'd be better off removing the handlers on the closing event rather
than the closed event. the reason for the errors, i think, is b/c the base
class isn't instanciated yet...let it load b4 making use of it's interfaces.
"Giovanni Bassi" <gb****@coair.com> wrote in message
news:uC**************@TK2MSFTNGP10.phx.gbl...
Hello All,

I have encountered a problem.
I am using visual inheritance and my base form adds an event handler on Form Load using the AddHandler Keyword.

The problem is that if the Event Handler code is there, when I create the
inherited form I get the error "Object Reference not set to an instance of
an object". If it is not I get no error.
I have tried leaving this code in the forms Sub New, but it produces the
same error.
I need the handler to be added during the form load and removed on the form unload. I cannot use the withevents keyword because the class is declared on the global level, and it is actually a property of this globally declared
class. Some code to help:
-//-

On a global Module:
Friend g_objTables As TableOps

On the application's Sub Main:
g_objTables = New TableOps(AppSettings.ConnectionString)

On the Base Class:
Private Sub frmBase1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

AddHandler m_btObj.DataChanged, AddressOf CostCentersDataChanged
AddHandler m_btObj.Range.RangeChanged, AddressOf RangeChanged

End Sub

Private Sub CostCentersDataChanged() 'handling DataChanged Event

'various steps taken to handle the event

End Sub

Private Sub RangeChanged()

'various steps taken to handle the event

End Sub

Private Sub frmBase1_Closed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Closed

RemoveHandler m_btObj.DataChanged, AddressOf CostCentersDataChanged RemoveHandler m_btObj.Range.RangeChanged, AddressOf RangeChanged

End Sub
On the Derived Class:
Private Sub frmCostCenters_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

m_btObj = g_objTables.CostCenters

End Sub

-//-

If I comment the two code lines in frmBase1_Load I get no error. If I
don't...
Also, if I try calling something else, like Msgbox("Test"), it works fine.

Does anyone know why that's happening? Can't I add event handlers in the
Form Load?

Thanks a lot in advance for all the replies.

Giovanni Bassi

Nov 20 '05 #2
Hey Steve,

Thanks for the explanation. I tried your way but it wouldn't work either.
I added a

If Me.DesignMode = False Then
AddHandler m_btObj.DataChanged, AddressOf CostCentersDataChanged
AddHandler m_btObj.Range.RangeChanged, AddressOf RangeChanged
End If

to the form's load event. I can see the form now and everything works fine.
I believe the problem was in handling the m_btObj object. It is set to

m_btObj = g_objTables.CostCenters

on the derived form only. And CostCenters is a property of g_objTables that
returns a reference to an object. That object is only created at that point,
when the reference is requested, and that is probably why the IDE could not
design the whole thing, because it could not find the object. At run time
everything was working fine, so I thought of the DesignMode property, which
came quite handy.

Giovanni Bassi

"steve" <as*@abc.com> wrote in message
news:vn************@corp.supernews.com...
i usually add handlers in sub new() after mybase.new() and
initializecomponent()...i haven't had problems. btw, you could try this in
your form class:

private withevents m_btObj as TableOps.CostCenters = g_objTables.CostCenters ' or set it to g_objTables in your mybase.load delegate.

that should work just fine...your code w/b:

delegateFunctionBlahBlahBlah(...) handles m_btObj.Blah()

that way you can leave out the explicit handers (add/remove) and let the
framework handle the delegate resources.

hth,

steve

btw...you'd be better off removing the handlers on the closing event rather than the closed event. the reason for the errors, i think, is b/c the base
class isn't instanciated yet...let it load b4 making use of it's interfaces.

"Giovanni Bassi" <gb****@coair.com> wrote in message
news:uC**************@TK2MSFTNGP10.phx.gbl...
Hello All,

I have encountered a problem.
I am using visual inheritance and my base form adds an event handler on Form
Load using the AddHandler Keyword.

The problem is that if the Event Handler code is there, when I create the inherited form I get the error "Object Reference not set to an instance of an object". If it is not I get no error.
I have tried leaving this code in the forms Sub New, but it produces the
same error.
I need the handler to be added during the form load and removed on the

form
unload. I cannot use the withevents keyword because the class is declared on
the global level, and it is actually a property of this globally

declared class. Some code to help:
-//-

On a global Module:
Friend g_objTables As TableOps

On the application's Sub Main:
g_objTables = New TableOps(AppSettings.ConnectionString)

On the Base Class:
Private Sub frmBase1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

AddHandler m_btObj.DataChanged, AddressOf CostCentersDataChanged
AddHandler m_btObj.Range.RangeChanged, AddressOf RangeChanged

End Sub

Private Sub CostCentersDataChanged() 'handling DataChanged Event

'various steps taken to handle the event

End Sub

Private Sub RangeChanged()

'various steps taken to handle the event

End Sub

Private Sub frmBase1_Closed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Closed

RemoveHandler m_btObj.DataChanged, AddressOf

CostCentersDataChanged
RemoveHandler m_btObj.Range.RangeChanged, AddressOf RangeChanged

End Sub
On the Derived Class:
Private Sub frmCostCenters_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

m_btObj = g_objTables.CostCenters

End Sub

-//-

If I comment the two code lines in frmBase1_Load I get no error. If I
don't...
Also, if I try calling something else, like Msgbox("Test"), it works fine.
Does anyone know why that's happening? Can't I add event handlers in the
Form Load?

Thanks a lot in advance for all the replies.

Giovanni Bassi


Nov 20 '05 #3

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

Similar topics

3
by: Uty | last post by:
(Originally posted this in VC++ section .. whoops I'm working on a series of projects which have a few things in common, which has been abstracted into an object we'll call ParentForm. I started...
4
by: Mike Cooper | last post by:
There is something about inherited classes I evidently don't know... I wrote the following class: Class Class1 inherits System.Windows.Forms.DataGridTextBoxColumn End Class There is...
1
by: RRiness | last post by:
I'm trying to use a template form I created. When I add an inherited form to my project, I get the error: Object reference not set to an instance of an object. when the inherited form is...
13
by: Lorne Smith | last post by:
Hi, First, sorry for the crosspost, but it seemed appropriate... :) I've come accross what I consider to be a bug, but I don't know if it's already known or not. (VS .Net 2003 Pro - VB.Net) ...
7
by: RandyR | last post by:
I'm building a small desktop database app. I am building a template form that has navigation and editing coded, and want to inherit this for my actual data forms. When I add a form, and change the...
2
by: Peter | last post by:
I have a base form from which many forms inherit. The base form is in one project, and the inherits form is in another project. Both projects are under the same solution space. Both forms...
0
by: cnSoftware | last post by:
I come across this problem during my recent project and even now I am still be puzzled by it. So I paste the procedure here. I wish you can figure out where I am wrong or missing. The sample...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
0
by: Jay Pondy | last post by:
I added a Company field to the aspNet_Users table and then followed along with the MSDN docs on How to Implement a Custom Membership User by creating my inherited class from the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.