Circle Reference

  • Thread starter Alhambra Eidos Desarrollo
  • Start date
A

Alhambra Eidos Desarrollo

Hi all,

I have two projects, and circle reference with them...

Cliente.Util.vbproj

For Each formularioAbierto As Form In
System.Windows.Forms.Application.OpenForms

If
formularioAbierto.GetType().Equals(GetType(GRUPOBACKUP.Administrador.frmMain)) Then

Dim frmMain As GRUPOBACKUP.Administrador.frmMain
frmMain = CType(formularioAbierto, GRUPOBACKUP.Administrador.frmMain)
frmMain.Conectado = False

End If
Next

Administrador.vbproj

Contains GRUPOBACKUP.Administrador.frmMain class
and use classes of Cliente.util.vbproj

Can I use Reflection for avoid circle reference for this code??

For Each formularioAbierto As Form In
System.Windows.Forms.Application.OpenForms

If
formularioAbierto.GetType().Equals(GetType(GRUPOBACKUP.Administrador.frmMain)) Then

Dim frmMain As GRUPOBACKUP.Administrador.frmMain
frmMain = CType(formularioAbierto, GRUPOBACKUP.Administrador.frmMain)
frmMain.Conectado = False

End If
Next


Thanks in advanced.
 
A

Armin Zingler

Alhambra said:
If
formularioAbierto.GetType().Equals(GetType(GRUPOBACKUP.Administrador.frmMain))
Then

Shorter:
If typeof formularioAbierto is GRUPOBACKUP.Administrador.frmMain then

However, it is also true if formularioAbierto is a derived class.

Can I use Reflection for avoid circle reference for this code??

Circular references are very dangerous! They can work temporarily, but, once
the chain is broken, you won't be able to compile any of the projects. The
ol' chicken and egg problem. You need A in order to build B, but you need B
in order to build A, so, what to build first?

You must always build layers, each build upon lower layers. If you wanted to
make a reference to a higher layer, you should be alerted. Then you have to
find a more abstract approach, i.e. supplying a base class or an Interface
in the lower layer that can be used in higher layers. Or the problem is an
indication that both projects must be melted together to one.

I can't say how to do this in your specific case because it's a design
question. Design is often a matter of taste.


Armin
 
P

Phill W.

Alhambra said:
I have two projects, and circle reference with them...

Oh dear.
If
formularioAbierto.GetType().Equals(GetType(GRUPOBACKUP.Administrador.frmMain)) Then
Dim frmMain As GRUPOBACKUP.Administrador.frmMain
frmMain = CType(formularioAbierto, GRUPOBACKUP.Administrador.frmMain)
frmMain.Conectado = False

If this is /all/ you need to do (set a property on the form), then
consider using an Interface:

Public Interface IConectado
Public Property Conectado() as Boolean
End Interface

Compile this into a Dll and reference it from /both/ projects.

In both projects, have the form (frmMain) implement this Interface, as in

Public Class frmMain
Inherits Form

Public Property Conectado() as Boolean _
Implements IConectado.ConectAdo
. . .
End Property

End Class

Then, your code becomes:

If formularioAbierto Is IConectado Then
Dim frm as IConectado _
= DirectCast( formularioAbierto, IConectado )
frm.Conectado = False
End If

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top