I have created a base form in VB.net 2005, in the form load event I toggle the visibility of a help button depending on the value of a property of a global object (A Config Class which read in the users config - do they want help available etc)
If I Inherit a new form from the base form, the IDE gets an warning "Object reference not set to an instance of an object". If I change the code to use a global variable rather than a property of a global object then the IDE doesn't encounter the problem.
Module containing Global Variables
Module
modGlobals Public g_objRegistry As clsRegistry Public g_blnShowHelp As Boolean = TrueEnd
ModuleClass which exposes public property
Public
Class clsRegistry Public ReadOnly Property ShowHelp() As Boolean Get Return False End Get End PropertyEnd
ClassGlobal Object is created in the application startup event
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
g_ObjRegistry = New clsRegistry
End Sub
Form Load Event in the Base Form
Protected Sub frmSummaryBase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadcmdHelp.Visible = g_objRegistry.ShowHelp
'Shows Bug 'cmdHelp.Visible = g_blnShowHelp 'This Works OK End Sub
Cannot Access Properties of Global Object in a Derived Form
Martin Gentry
Added precisely that code you've posted here, added a second form which inherits from the base form, set the second form to startup and it runs fine. However I can replicate the problem you are having...
I'm not an expert in how usercontrols work (I just make 'em), but the issue is that even though the inherited form is operating in design time, it's actually operating in 'run time' - I can't recall how it works, exactly, but the inherited form is running the code on the base form: you can see this if you put a label or something on the base form and set it to, say, the form size in the resize event of the base form:
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ResizeLabel1.Text = Me.Size.ToString
End Sub
Even in design time for your inherited form, the label displays the size of your form: the code in the base form is executing. Thus, in the case of your global object, when the base form code runs - in inheritedform design time - the object has not been set. Someone may have the technical explanation.
NessDan
Thanks for taking the time to look into this; I now understand what is happening.
I think that the IDE being unable to show the form is a bug, though. I am simply using Visual inheritance to impose a basic standard layout across forms. We will need to perform further design work in the IDE in order to add the appropriate functionality to each of the derived forms.
Perhaps someone from Microsoft could comment on if they consider this a bug.
mertkan65
I trhink that's what I'd do. However, going a bit further, I'd encapsulate the base form so it stands alone: i.e. does not rely on any outside objects.
Specifically, the base form would hold a reference to your global object. That reference would be set though a property of the base form. Although the end result is the same, I think it'd encapsulate things a bit better (of course, I'm assuming that the object in question is a bit more complex than what's been posted here
).
e.g.:
private _MyObject as GlobalObjectName
public property BaseObject as GlobalObjectName
Get
Return _MyObject
end get
Set(value as GlobalObjectName)
_MyObject = value
if _MayObject is Nothing then 'Do Something Fun
end if
end set
end property
Thiru_
I wouldn't consider it a bug: it's the way it works: the code in the form has to execute to get everything to show up for the inherited control. What you can do, though, is perform a check to see if the object is nothing prior to performing any actions with the object (which should be done, anyway, to prevent the chance of any null reference exceptions).
gendai
Thanks again for your time
As a result of your first reply I had already changed my code to check the global variables existance, making an arbitary decision on what to do if it doesn't (IE: at Design time)