Pass 'Me' by reference to constructor a class.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rms12345
    New Member
    • Feb 2012
    • 1

    #1

    Pass 'Me' by reference to constructor a class.

    Hi,

    Implementing a simple MVC in vb. The controller instance creates and holds instances of the model and view:
    Code:
    Public Class Controller
        Public Property myModel As Model
        Public Property myView As View
    
        Public Sub New()
            Me.myModel = New Model(Me)
            Me.myView = New View(Me)
        End Sub
    The View and Model classes' constructors look like as follows:
    Code:
    Public Class View
        Dim controller As Controller
    
        Public Sub New(ByRef ctrl As Controller)
            Me.controller = ctrl
        End Sub
    Within View and Model I have made calls to functions within the controller, eg:
    Code:
    Private Sub AddRangeButton_Click(...)
        Me.controller.a_function()
    The function performs actions on controller.myVi ew and controller.myMo del, however, I get an error saying myView is no longer instantiated:

    "Object reference not set to an instance of an object."

    So it looks like when I passed controller to the View ByRef, it actually just passed it as if ByVal, and the controller held by View is different to the controller which created and holds View.

    Is that what is happening? Any ideas for a solution?
  • !NoItAll
    Contributor
    • May 2006
    • 297

    #2
    Try

    Dim Controller as New Controller

    in your View and Model classes

    Comment

    Working...