473,372 Members | 814 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,372 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 5285
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.