none
Form Inheritance error with Global variables in Form_Load event RRS feed

  • Question

  • Hi All

    I am trying to resolve an "Object reference not set to an instance of an object. " during the design time.

    Basically I have a base form with a button only (Form1.vb). And I have another Form2 inheriting from Form1. In the Form_Load event of Form1, I need access to a global variable from another DLL.

    If I compile and run, it works, but problem is when I try to load the form2 during design time, I get an error and the design UI doesn't load properly and show an error with the above mentioned message. Anyone with any ideas on how to resolve this kind of problem.

    Tks in advance.

    Below is a link to the source code I created. But this source code cannot be attached into this forum. Attachments

    • Edited by bit82lorry Friday, May 3, 2019 10:16 AM Add link to source code
    Friday, May 3, 2019 10:15 AM

Answers

  • If you think the problem lies with the global variable use a singleton and for the property make sure it's in a valid state when needed.

    Example Singleton

    Public Class MyProperties
        Private Shared _Instance As MyProperties
        Private _Item As String
        'Singleton Instance Provider
        Public Shared Function GetInstance() As MyProperties
    
            If _Instance Is Nothing Then
                _Instance = New MyProperties
            End If
    
            Return _Instance
    
        End Function
        Public Property Item() As String
            Get
                '
                ' Before returning the value make sure it's 
                ' valid for the operation it's intended for
                '
                Return _Item
            End Get
            Set
                _Item = Value
            End Set
        End Property
    
        Protected Sub New()
        End Sub
    End Class
    

    Set a property

    MyProperties.GetInstance().Item = "Karen"

    Get a property

    Console.WriteLine(MyProperties.GetInstance().Item)


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by bit82lorry Monday, May 6, 2019 5:19 PM
    • Unmarked as answer by bit82lorry Monday, May 6, 2019 5:19 PM
    • Marked as answer by bit82lorry Monday, May 6, 2019 5:20 PM
    Friday, May 3, 2019 4:28 PM
    Moderator
  • Using instructions like ‘If Not DesignMode Then …’ you can exclude some portions that can be used at run-time only and are not needed at design time.

    • Marked as answer by bit82lorry Monday, May 6, 2019 5:19 PM
    Friday, May 3, 2019 5:05 PM

All replies

  • Hello,

    If you are using the following way to inherit heed the warnings. Make sure there is not a control on both forms with the same name also. Another thing to try is move any code out of form load if any into the Shown event.

    If all fails then try (and I know this is going to sound counter productive) removing all controls on both forms then add one back in at a time and test to see if you see get this error which is one way to see if this points out the problem. You could even do this in a new project do not to mess up the current project.

    In regards to the global variable, discard this code for now so that is ruled out if you have coded for it presently. That should be another question anyways unless discarding any existing code removed the error.

    Bottom line is this puts you into detective mode as a programmer to rule out all possible issues thinking you did a simple line to inherit as below.

    Public Class Form2  
        Inherits Namespace1.Form1

     

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, May 3, 2019 11:02 AM
    Moderator
  • Hi

    Thanks for your reply and your warnings.

    Put aside the warning first, back to the problem. I think the problem lies with the global variable. And the attachment in the link provided is already in the simplified form that will reproduce the error. Just two forms without any controls in it (i.e. Form1 and Form2, with Form2 inheriting from Form1 inherits TstFormInherit.Form1).

    So I do not see in anyway how I can remove any control when there is none in the forms to begin with.

    And if I remove the access to the global variable in Form1_Load event, the designer will show Form2 without any error. But if I try to access the global variable in the Form1_Load event then the error will show when I try to load Form2 in design time. Also please note that the global variable is instantiated in another form first before Form1 and Form2 is able to be loaded during runtime.

    Please advise.


    • Edited by bit82lorry Friday, May 3, 2019 3:30 PM additional info on instanitating the global variable
    Friday, May 3, 2019 3:28 PM
  • If you think the problem lies with the global variable use a singleton and for the property make sure it's in a valid state when needed.

    Example Singleton

    Public Class MyProperties
        Private Shared _Instance As MyProperties
        Private _Item As String
        'Singleton Instance Provider
        Public Shared Function GetInstance() As MyProperties
    
            If _Instance Is Nothing Then
                _Instance = New MyProperties
            End If
    
            Return _Instance
    
        End Function
        Public Property Item() As String
            Get
                '
                ' Before returning the value make sure it's 
                ' valid for the operation it's intended for
                '
                Return _Item
            End Get
            Set
                _Item = Value
            End Set
        End Property
    
        Protected Sub New()
        End Sub
    End Class
    

    Set a property

    MyProperties.GetInstance().Item = "Karen"

    Get a property

    Console.WriteLine(MyProperties.GetInstance().Item)


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by bit82lorry Monday, May 6, 2019 5:19 PM
    • Unmarked as answer by bit82lorry Monday, May 6, 2019 5:19 PM
    • Marked as answer by bit82lorry Monday, May 6, 2019 5:20 PM
    Friday, May 3, 2019 4:28 PM
    Moderator
  • Using instructions like ‘If Not DesignMode Then …’ you can exclude some portions that can be used at run-time only and are not needed at design time.

    • Marked as answer by bit82lorry Monday, May 6, 2019 5:19 PM
    Friday, May 3, 2019 5:05 PM
  • Hi 

    Thanks for the advice. 

    I just change the global declaration from 

    Public gReqInfo As RequestInfo 

    to this

    Public gReqInfo As New RequestInfo

    And everything just works.

    • Marked as answer by bit82lorry Monday, May 6, 2019 5:19 PM
    • Unmarked as answer by bit82lorry Monday, May 6, 2019 5:19 PM
    • Edited by bit82lorry Monday, May 6, 2019 5:24 PM
    Monday, May 6, 2019 5:18 PM
  • This works as well. But will be a bit tedious though. Thanks.
    • Marked as answer by bit82lorry Monday, May 6, 2019 5:19 PM
    • Unmarked as answer by bit82lorry Monday, May 6, 2019 5:19 PM
    Monday, May 6, 2019 5:19 PM