Circular dependence problem - vb.net

2
Public Class FrmMenu

    Private _modelUsuario As New Usuario.Usuario
    Private _controllerUsuario As New ControllerUsuario.ControllerUsuario
    Private _frmUsuario As New VistaUsuario.FrmUsuario

    Private _msjAlert As String

    Private Sub FrmMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DaGrViUsuarios.DataSource = _controllerUsuario.RouteGetUsuario(_msjAlert)
    End Sub

    Private Sub BtnAgregarUsuario_Click(sender As Object, e As EventArgs) Handles BtnAgregarUsuario.Click
        _frmUsuario.ShowDialog()
    End Sub

    Private Sub DaGrViUsuarios_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DaGrViUsuarios.CellDoubleClick
        If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
            _frmUsuario.TextUsuarioProp = DaGrViUsuarios.CurrentRow.Cells(1).Value.ToString
            _frmUsuario.TextNombresProp = DaGrViUsuarios.CurrentRow.Cells(2).Value.ToString
            _frmUsuario.TextEmailProp = DaGrViUsuarios.CurrentRow.Cells(3).Value.ToString
            _frmUsuario.TextPasswdProp = DaGrViUsuarios.CurrentRow.Cells(4).Value.ToString
            _frmUsuario.CmbPerfilProp = DaGrViUsuarios.CurrentRow.Cells(5).Value.ToString
            _frmUsuario.ShowDialog()
        End If
    End Sub

    Private Sub BtnRefrescar_Click(sender As Object, e As EventArgs) Handles BtnRefrescar.Click
        DaGrViUsuarios.DataSource = _controllerUsuario.RouteGetUsuario(_msjAlert)
    End Sub
End Class

This FrmMenu Class has a reference to FrmUser where I need to reload the DataGridView after inserting a new user

Public Class FrmUsuario

    Private _modelUsuario As New Usuario.Usuario
    Private _controllerUsuario As New ControllerUsuario.ControllerUsuario
    Private _mejAlert As String

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Property TextUsuarioProp()
        Get
            Return TextUsuario.Text
        End Get
        Set(ByVal value)
            TextUsuario.Text = value
        End Set
    End Property

    Public Property TextNombresProp()
        Get
            Return TextNombres.Text
        End Get
        Set(ByVal value)
            TextNombres.Text = value
        End Set
    End Property

    Public Property TextEmailProp()
        Get
            Return TextEmail.Text
        End Get
        Set(ByVal value)
            TextEmail.Text = value
        End Set
    End Property

    Public Property TextPasswdProp()
        Get
            Return TextPasswd.Text
        End Get
        Set(ByVal value)
            TextPasswd.Text = value
        End Set
    End Property

    Public Property CmbPerfilProp()
        Get
            Return CmbPerfil.Text
        End Get
        Set(ByVal value)
            CmbPerfil.Text = value
        End Set
    End Property

    Public Sub CargarCampos()
        _modelUsuario.Usuario = TextUsuario.Text
        _modelUsuario.Nombres = TextNombres.Text
        _modelUsuario.Email = TextEmail.Text
        _modelUsuario.Passwd = TextPasswd.Text
        _modelUsuario.Perfil = CmbPerfil.Text
    End Sub

    Private Sub FrmUsuario_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub BtnEnviarFormUsuario_Click(sender As Object, e As EventArgs) Handles BtnEnviarFormUsuario.Click
        CargarCampos()
        If _controllerUsuario.Validacion(_modelUsuario, _mejAlert) Then
            _modelUsuario = _controllerUsuario.RouteAddUsuario(TextUsuario.Text, TextNombres.Text, TextEmail.Text, TextPasswd.Text, CmbPerfil.Text)
            MessageBox.Show("Usuario Guardado Exitosamente!.", "Success!")
            'Aqui necesito recargar el DatagridView que esta en el otro form 
            Hide()
        Else
            MessageBox.Show(_mejAlert, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        End If
    End Sub
End Class

In this form I need to load the DatagridView but at the time of adding as a reference the other form tells me an error that this would cause a circular dependency

    
asked by Christian Gimenez 18.05.2016 в 23:49
source

1 answer

1

Personally I avoid the use of .dll precisely because of that, I feel more comfortable without dependencies, however of course it is not always possible.

Anyway, you can solve the problem by passing the datagrid by reference in the constructor and save it outside to access the datagrid from the rest of the functions.

Notice that I added the variable '_ReferenciaDataGrid'

Public Class FrmUser

Private _modelUsuario As New Usuario.Usuario
Private _controllerUsuario As New ControllerUsuario.ControllerUsuario
Private _mejAlert As String

Private _ReferenciaDataGrid As System.Windows.Forms.DataGridView
Public Sub New(DataGrid As System.Windows.Forms.DataGridView)

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.



    _ReferenciaDataGrid = DataGrid

End Sub

End Class

    
answered by 19.05.2016 в 09:31