Circular references ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave S

    Circular references ?

    Anyone know how to get round the problem of circular references in VB.NET
    (sorry I'm a .NET newbie).
    I create one project which has 2 classes in it, MyHandler and MyObject.
    MyHandler just needs to call another class in a different project and pass a
    parameter of type MyObject to it.
    So the class in the different project needs to import the first project
    namespace so it know what MyObject is.
    The MyHandler class needs to import the second project namespace so it can
    call the class in the second project.

    This doesn't seem to be allowed in .NET even though Java could easily do
    this.

    See code below :
    (1st project : namespace = MyClass.Test1)

    Public Class MyHandler
    Public Function DoSomething(ByV al obj As MyObject) As String
    Dim a As New [MyClass].Test2.MyAction
    [color=red]Return a.DoMethod(obj)[/color]
    End Function
    End Class

    Public Class MyObject
    Private a, b As String
    Public Property attrib1() As String
    Get
    Return a
    End Get
    Set(ByVal Value As String)
    a = Value
    End Set
    End Property
    Public Property attrib2() As String
    Get
    Return b
    End Get
    Set(ByVal Value As String)
    b = Value
    End Set
    End Property
    End Class


    (2nd project : namespace = MyClass.Test2)

    Imports [MyClass].Test1

    Public Class MyAction
    Public Function DoMethod(ByVal obj As MyObject) As String
    Return obj.attrib1
    End Function
    End Class



    The line highlighted in red above fails because according to .NET it doesn't
    know what type the value called 'obj' is when it comes to passing it to the
    method in the 2nd project.
    The error I get is :

    H:\Work\testbed \MyClass.Test1\ MyHandler.vb(4) : Reference required to
    assembly 'MyClass.Test1' containing the type 'MyClass.Test1. MyObject'. Add
    one to your project.




    This seems a really basic piece of functionality and I must be declaring
    something incorrectly here

    Any ideas ??


  • Erik Frey

    #2
    Re: Circular references ?

    "Dave S" <dave_xxx70@hot mail.com> wrote in message
    news:ac629e82aa b3c366fe2c0c20e 78b5eee@news.te ranews.com...[color=blue]
    > Anyone know how to get round the problem of circular references in VB.NET
    > (sorry I'm a .NET newbie).
    > I create one project which has 2 classes in it, MyHandler and MyObject.
    > MyHandler just needs to call another class in a different project and pass[/color]
    a[color=blue]
    > parameter of type MyObject to it.
    > So the class in the different project needs to import the first project
    > namespace so it know what MyObject is.
    > The MyHandler class needs to import the second project namespace so it can
    > call the class in the second project.[/color]

    Typically this is adressed by creating a third project that contains your
    business types in it.

    Then have your other two projects refer to this new project, to have access
    to the common type (so in your case, put MyObject in the third project).

    Also, keep in mind that even if you have three projects in a solution, you
    still have to add the project references manually (right click on
    References - > Add References -> Projects tab)

    Erik


    Comment

    • Erik Frey

      #3
      Re: Circular references ?

      "Dave S" <dave_xxx70@hot mail.com> wrote in message
      news:ac629e82aa b3c366fe2c0c20e 78b5eee@news.te ranews.com...[color=blue]
      > Anyone know how to get round the problem of circular references in VB.NET
      > (sorry I'm a .NET newbie).
      > I create one project which has 2 classes in it, MyHandler and MyObject.
      > MyHandler just needs to call another class in a different project and pass[/color]
      a[color=blue]
      > parameter of type MyObject to it.
      > So the class in the different project needs to import the first project
      > namespace so it know what MyObject is.
      > The MyHandler class needs to import the second project namespace so it can
      > call the class in the second project.[/color]

      Typically this is adressed by creating a third project that contains your
      business types in it.

      Then have your other two projects refer to this new project, to have access
      to the common type (so in your case, put MyObject in the third project).

      Also, keep in mind that even if you have three projects in a solution, you
      still have to add the project references manually (right click on
      References - > Add References -> Projects tab)

      Erik


      Comment

      • Dave S

        #4
        Re: Circular references ?

        Erik,

        Thanks for this, I just wanted to confirm that it's not possible to do it
        with just 2 projects.
        I tried moving my object into another project (so it's sort of an interface
        project) and then each of the other projects can reference that project
        without causing the circular reference problem.
        I just thought it was a bit messy when in Java you could easily do this
        without a middleman package containing the object you want to pass between
        the classes.
        At least I know that it wasn't something I'd missed out when declaring the 2
        projects initially.
        Will have to remember this one in future (I've been struggling with this all
        afternoon)

        Dave




        "Erik Frey" <eriksjunk@hotm ail.com> wrote in message
        news:uCVltrOHEH A.3556@TK2MSFTN GP10.phx.gbl...[color=blue]
        > "Dave S" <dave_xxx70@hot mail.com> wrote in message
        > news:ac629e82aa b3c366fe2c0c20e 78b5eee@news.te ranews.com...[color=green]
        > > Anyone know how to get round the problem of circular references in[/color][/color]
        VB.NET[color=blue][color=green]
        > > (sorry I'm a .NET newbie).
        > > I create one project which has 2 classes in it, MyHandler and MyObject.
        > > MyHandler just needs to call another class in a different project and[/color][/color]
        pass[color=blue]
        > a[color=green]
        > > parameter of type MyObject to it.
        > > So the class in the different project needs to import the first project
        > > namespace so it know what MyObject is.
        > > The MyHandler class needs to import the second project namespace so it[/color][/color]
        can[color=blue][color=green]
        > > call the class in the second project.[/color]
        >
        > Typically this is adressed by creating a third project that contains your
        > business types in it.
        >
        > Then have your other two projects refer to this new project, to have[/color]
        access[color=blue]
        > to the common type (so in your case, put MyObject in the third project).
        >
        > Also, keep in mind that even if you have three projects in a solution, you
        > still have to add the project references manually (right click on
        > References - > Add References -> Projects tab)
        >
        > Erik
        >
        >[/color]


        Comment

        • Dave S

          #5
          Re: Circular references ?

          Erik,

          Thanks for this, I just wanted to confirm that it's not possible to do it
          with just 2 projects.
          I tried moving my object into another project (so it's sort of an interface
          project) and then each of the other projects can reference that project
          without causing the circular reference problem.
          I just thought it was a bit messy when in Java you could easily do this
          without a middleman package containing the object you want to pass between
          the classes.
          At least I know that it wasn't something I'd missed out when declaring the 2
          projects initially.
          Will have to remember this one in future (I've been struggling with this all
          afternoon)

          Dave




          "Erik Frey" <eriksjunk@hotm ail.com> wrote in message
          news:uCVltrOHEH A.3556@TK2MSFTN GP10.phx.gbl...[color=blue]
          > "Dave S" <dave_xxx70@hot mail.com> wrote in message
          > news:ac629e82aa b3c366fe2c0c20e 78b5eee@news.te ranews.com...[color=green]
          > > Anyone know how to get round the problem of circular references in[/color][/color]
          VB.NET[color=blue][color=green]
          > > (sorry I'm a .NET newbie).
          > > I create one project which has 2 classes in it, MyHandler and MyObject.
          > > MyHandler just needs to call another class in a different project and[/color][/color]
          pass[color=blue]
          > a[color=green]
          > > parameter of type MyObject to it.
          > > So the class in the different project needs to import the first project
          > > namespace so it know what MyObject is.
          > > The MyHandler class needs to import the second project namespace so it[/color][/color]
          can[color=blue][color=green]
          > > call the class in the second project.[/color]
          >
          > Typically this is adressed by creating a third project that contains your
          > business types in it.
          >
          > Then have your other two projects refer to this new project, to have[/color]
          access[color=blue]
          > to the common type (so in your case, put MyObject in the third project).
          >
          > Also, keep in mind that even if you have three projects in a solution, you
          > still have to add the project references manually (right click on
          > References - > Add References -> Projects tab)
          >
          > Erik
          >
          >[/color]


          Comment

          • Erik Frey

            #6
            Re: Circular references ?

            "Dave S" <dave_xxx70@hot mail.com> wrote in message
            news:5cccffe5c7 df71d9fdfd061c1 e6213f2@news.te ranews.com...[color=blue]
            > Erik,
            >
            > Thanks for this, I just wanted to confirm that it's not possible to do it
            > with just 2 projects.[/color]

            If you don't want a third common class, your other option is to move the
            MyObject class declaration into the second project that you're trying to
            send it to. Then, just reference from your first project (that still
            contains MyHandler).

            But no - two projects cannot reference eachother.

            Erik


            Comment

            • Erik Frey

              #7
              Re: Circular references ?

              "Dave S" <dave_xxx70@hot mail.com> wrote in message
              news:5cccffe5c7 df71d9fdfd061c1 e6213f2@news.te ranews.com...[color=blue]
              > Erik,
              >
              > Thanks for this, I just wanted to confirm that it's not possible to do it
              > with just 2 projects.[/color]

              If you don't want a third common class, your other option is to move the
              MyObject class declaration into the second project that you're trying to
              send it to. Then, just reference from your first project (that still
              contains MyHandler).

              But no - two projects cannot reference eachother.

              Erik


              Comment

              • Jason Sobell

                #8
                Re: Circular references ?

                You can't directly call from B to A, but you can pass back events with the
                package as a parameter, and this is the standard approach to your original
                problem.
                MyHandler simply raises the event without requiring intimate knowledge
                (i.e. dependency) on project B.

                This is not a VB.Net specific issue. It applies to all OO languages. Java
                uses interfaces or events to avoid the issue, and you can do the same in
                VB.

                Cheers,
                Jason

                On Wed, 07 Apr 2004 22:09:35 GMT, Dave S wrote:[color=blue]
                > Erik,
                >
                > Thanks for this, I just wanted to confirm that it's not possible to do it
                > with just 2 projects.
                > I tried moving my object into another project (so it's sort of an interface
                > project) and then each of the other projects can reference that project
                > without causing the circular reference problem.
                > I just thought it was a bit messy when in Java you could easily do this
                > without a middleman package containing the object you want to pass between
                > the classes.
                > At least I know that it wasn't something I'd missed out when declaring the 2
                > projects initially.
                > Will have to remember this one in future (I've been struggling with this all
                > afternoon)
                >
                > Dave
                >
                >
                >
                >
                > "Erik Frey" <eriksjunk@hotm ail.com> wrote in message
                > news:uCVltrOHEH A.3556@TK2MSFTN GP10.phx.gbl...[color=green]
                >> "Dave S" <dave_xxx70@hot mail.com> wrote in message
                >> news:ac629e82aa b3c366fe2c0c20e 78b5eee@news.te ranews.com...[color=darkred]
                >>> Anyone know how to get round the problem of circular references in[/color][/color]
                > VB.NET[color=green][color=darkred]
                >>> (sorry I'm a .NET newbie).
                >>> I create one project which has 2 classes in it, MyHandler and MyObject.
                >>> MyHandler just needs to call another class in a different project and[/color][/color]
                > pass[color=green]
                >> a[color=darkred]
                >>> parameter of type MyObject to it.
                >>> So the class in the different project needs to import the first project
                >>> namespace so it know what MyObject is.
                >>> The MyHandler class needs to import the second project namespace so it[/color][/color]
                > can[color=green][color=darkred]
                >>> call the class in the second project.[/color]
                >>
                >> Typically this is adressed by creating a third project that contains your
                >> business types in it.
                >>
                >> Then have your other two projects refer to this new project, to have[/color]
                > access[color=green]
                >> to the common type (so in your case, put MyObject in the third project).
                >>
                >> Also, keep in mind that even if you have three projects in a solution, you
                >> still have to add the project references manually (right click on
                >> References - > Add References -> Projects tab)
                >>
                >> Erik
                >>
                >>[/color][/color]

                Comment

                • Jason Sobell

                  #9
                  Re: Circular references ?

                  You can't directly call from B to A, but you can pass back events with the
                  package as a parameter, and this is the standard approach to your original
                  problem.
                  MyHandler simply raises the event without requiring intimate knowledge
                  (i.e. dependency) on project B.

                  This is not a VB.Net specific issue. It applies to all OO languages. Java
                  uses interfaces or events to avoid the issue, and you can do the same in
                  VB.

                  Cheers,
                  Jason

                  On Wed, 07 Apr 2004 22:09:35 GMT, Dave S wrote:[color=blue]
                  > Erik,
                  >
                  > Thanks for this, I just wanted to confirm that it's not possible to do it
                  > with just 2 projects.
                  > I tried moving my object into another project (so it's sort of an interface
                  > project) and then each of the other projects can reference that project
                  > without causing the circular reference problem.
                  > I just thought it was a bit messy when in Java you could easily do this
                  > without a middleman package containing the object you want to pass between
                  > the classes.
                  > At least I know that it wasn't something I'd missed out when declaring the 2
                  > projects initially.
                  > Will have to remember this one in future (I've been struggling with this all
                  > afternoon)
                  >
                  > Dave
                  >
                  >
                  >
                  >
                  > "Erik Frey" <eriksjunk@hotm ail.com> wrote in message
                  > news:uCVltrOHEH A.3556@TK2MSFTN GP10.phx.gbl...[color=green]
                  >> "Dave S" <dave_xxx70@hot mail.com> wrote in message
                  >> news:ac629e82aa b3c366fe2c0c20e 78b5eee@news.te ranews.com...[color=darkred]
                  >>> Anyone know how to get round the problem of circular references in[/color][/color]
                  > VB.NET[color=green][color=darkred]
                  >>> (sorry I'm a .NET newbie).
                  >>> I create one project which has 2 classes in it, MyHandler and MyObject.
                  >>> MyHandler just needs to call another class in a different project and[/color][/color]
                  > pass[color=green]
                  >> a[color=darkred]
                  >>> parameter of type MyObject to it.
                  >>> So the class in the different project needs to import the first project
                  >>> namespace so it know what MyObject is.
                  >>> The MyHandler class needs to import the second project namespace so it[/color][/color]
                  > can[color=green][color=darkred]
                  >>> call the class in the second project.[/color]
                  >>
                  >> Typically this is adressed by creating a third project that contains your
                  >> business types in it.
                  >>
                  >> Then have your other two projects refer to this new project, to have[/color]
                  > access[color=green]
                  >> to the common type (so in your case, put MyObject in the third project).
                  >>
                  >> Also, keep in mind that even if you have three projects in a solution, you
                  >> still have to add the project references manually (right click on
                  >> References - > Add References -> Projects tab)
                  >>
                  >> Erik
                  >>
                  >>[/color][/color]

                  Comment

                  • Dave S

                    #10
                    Re: Circular references ?

                    Jason,

                    Not sure I understand that one but I can try and look into raising events.
                    Any code examples of this anywhere ?

                    Thanks
                    Dave



                    "Jason Sobell" <iGadget_@hotma il.com> wrote in message
                    news:70ds7tcggr n2.1cfdlkstq2s0 j.dlg@40tude.ne t...[color=blue]
                    > You can't directly call from B to A, but you can pass back events with the
                    > package as a parameter, and this is the standard approach to your original
                    > problem.
                    > MyHandler simply raises the event without requiring intimate knowledge
                    > (i.e. dependency) on project B.
                    >
                    > This is not a VB.Net specific issue. It applies to all OO languages. Java
                    > uses interfaces or events to avoid the issue, and you can do the same in
                    > VB.
                    >
                    > Cheers,
                    > Jason
                    >
                    > On Wed, 07 Apr 2004 22:09:35 GMT, Dave S wrote:[color=green]
                    > > Erik,
                    > >
                    > > Thanks for this, I just wanted to confirm that it's not possible to do[/color][/color]
                    it[color=blue][color=green]
                    > > with just 2 projects.
                    > > I tried moving my object into another project (so it's sort of an[/color][/color]
                    interface[color=blue][color=green]
                    > > project) and then each of the other projects can reference that project
                    > > without causing the circular reference problem.
                    > > I just thought it was a bit messy when in Java you could easily do this
                    > > without a middleman package containing the object you want to pass[/color][/color]
                    between[color=blue][color=green]
                    > > the classes.
                    > > At least I know that it wasn't something I'd missed out when declaring[/color][/color]
                    the 2[color=blue][color=green]
                    > > projects initially.
                    > > Will have to remember this one in future (I've been struggling with this[/color][/color]
                    all[color=blue][color=green]
                    > > afternoon)
                    > >
                    > > Dave
                    > >
                    > >
                    > >
                    > >
                    > > "Erik Frey" <eriksjunk@hotm ail.com> wrote in message
                    > > news:uCVltrOHEH A.3556@TK2MSFTN GP10.phx.gbl...[color=darkred]
                    > >> "Dave S" <dave_xxx70@hot mail.com> wrote in message
                    > >> news:ac629e82aa b3c366fe2c0c20e 78b5eee@news.te ranews.com...
                    > >>> Anyone know how to get round the problem of circular references in[/color]
                    > > VB.NET[color=darkred]
                    > >>> (sorry I'm a .NET newbie).
                    > >>> I create one project which has 2 classes in it, MyHandler and[/color][/color][/color]
                    MyObject.[color=blue][color=green][color=darkred]
                    > >>> MyHandler just needs to call another class in a different project and[/color]
                    > > pass[color=darkred]
                    > >> a
                    > >>> parameter of type MyObject to it.
                    > >>> So the class in the different project needs to import the first[/color][/color][/color]
                    project[color=blue][color=green][color=darkred]
                    > >>> namespace so it know what MyObject is.
                    > >>> The MyHandler class needs to import the second project namespace so it[/color]
                    > > can[color=darkred]
                    > >>> call the class in the second project.
                    > >>
                    > >> Typically this is adressed by creating a third project that contains[/color][/color][/color]
                    your[color=blue][color=green][color=darkred]
                    > >> business types in it.
                    > >>
                    > >> Then have your other two projects refer to this new project, to have[/color]
                    > > access[color=darkred]
                    > >> to the common type (so in your case, put MyObject in the third[/color][/color][/color]
                    project).[color=blue][color=green][color=darkred]
                    > >>
                    > >> Also, keep in mind that even if you have three projects in a solution,[/color][/color][/color]
                    you[color=blue][color=green][color=darkred]
                    > >> still have to add the project references manually (right click on
                    > >> References - > Add References -> Projects tab)
                    > >>
                    > >> Erik
                    > >>
                    > >>[/color][/color][/color]


                    Comment

                    • Dave S

                      #11
                      Re: Circular references ?

                      Jason,

                      Not sure I understand that one but I can try and look into raising events.
                      Any code examples of this anywhere ?

                      Thanks
                      Dave



                      "Jason Sobell" <iGadget_@hotma il.com> wrote in message
                      news:70ds7tcggr n2.1cfdlkstq2s0 j.dlg@40tude.ne t...[color=blue]
                      > You can't directly call from B to A, but you can pass back events with the
                      > package as a parameter, and this is the standard approach to your original
                      > problem.
                      > MyHandler simply raises the event without requiring intimate knowledge
                      > (i.e. dependency) on project B.
                      >
                      > This is not a VB.Net specific issue. It applies to all OO languages. Java
                      > uses interfaces or events to avoid the issue, and you can do the same in
                      > VB.
                      >
                      > Cheers,
                      > Jason
                      >
                      > On Wed, 07 Apr 2004 22:09:35 GMT, Dave S wrote:[color=green]
                      > > Erik,
                      > >
                      > > Thanks for this, I just wanted to confirm that it's not possible to do[/color][/color]
                      it[color=blue][color=green]
                      > > with just 2 projects.
                      > > I tried moving my object into another project (so it's sort of an[/color][/color]
                      interface[color=blue][color=green]
                      > > project) and then each of the other projects can reference that project
                      > > without causing the circular reference problem.
                      > > I just thought it was a bit messy when in Java you could easily do this
                      > > without a middleman package containing the object you want to pass[/color][/color]
                      between[color=blue][color=green]
                      > > the classes.
                      > > At least I know that it wasn't something I'd missed out when declaring[/color][/color]
                      the 2[color=blue][color=green]
                      > > projects initially.
                      > > Will have to remember this one in future (I've been struggling with this[/color][/color]
                      all[color=blue][color=green]
                      > > afternoon)
                      > >
                      > > Dave
                      > >
                      > >
                      > >
                      > >
                      > > "Erik Frey" <eriksjunk@hotm ail.com> wrote in message
                      > > news:uCVltrOHEH A.3556@TK2MSFTN GP10.phx.gbl...[color=darkred]
                      > >> "Dave S" <dave_xxx70@hot mail.com> wrote in message
                      > >> news:ac629e82aa b3c366fe2c0c20e 78b5eee@news.te ranews.com...
                      > >>> Anyone know how to get round the problem of circular references in[/color]
                      > > VB.NET[color=darkred]
                      > >>> (sorry I'm a .NET newbie).
                      > >>> I create one project which has 2 classes in it, MyHandler and[/color][/color][/color]
                      MyObject.[color=blue][color=green][color=darkred]
                      > >>> MyHandler just needs to call another class in a different project and[/color]
                      > > pass[color=darkred]
                      > >> a
                      > >>> parameter of type MyObject to it.
                      > >>> So the class in the different project needs to import the first[/color][/color][/color]
                      project[color=blue][color=green][color=darkred]
                      > >>> namespace so it know what MyObject is.
                      > >>> The MyHandler class needs to import the second project namespace so it[/color]
                      > > can[color=darkred]
                      > >>> call the class in the second project.
                      > >>
                      > >> Typically this is adressed by creating a third project that contains[/color][/color][/color]
                      your[color=blue][color=green][color=darkred]
                      > >> business types in it.
                      > >>
                      > >> Then have your other two projects refer to this new project, to have[/color]
                      > > access[color=darkred]
                      > >> to the common type (so in your case, put MyObject in the third[/color][/color][/color]
                      project).[color=blue][color=green][color=darkred]
                      > >>
                      > >> Also, keep in mind that even if you have three projects in a solution,[/color][/color][/color]
                      you[color=blue][color=green][color=darkred]
                      > >> still have to add the project references manually (right click on
                      > >> References - > Add References -> Projects tab)
                      > >>
                      > >> Erik
                      > >>
                      > >>[/color][/color][/color]


                      Comment

                      • Dave S

                        #12
                        Re: Circular references ?

                        Erik,

                        Yes that's another approach.
                        Haven't tried that one out but I'm sure it will work because it removes the
                        need for project 2 being dependent on project 1.

                        Thanks
                        Dave


                        "Erik Frey" <eriksjunk@hotm ail.com> wrote in message
                        news:ubWsb7OHEH A.3832@TK2MSFTN GP10.phx.gbl...[color=blue]
                        > "Dave S" <dave_xxx70@hot mail.com> wrote in message
                        > news:5cccffe5c7 df71d9fdfd061c1 e6213f2@news.te ranews.com...[color=green]
                        > > Erik,
                        > >
                        > > Thanks for this, I just wanted to confirm that it's not possible to do[/color][/color]
                        it[color=blue][color=green]
                        > > with just 2 projects.[/color]
                        >
                        > If you don't want a third common class, your other option is to move the
                        > MyObject class declaration into the second project that you're trying to
                        > send it to. Then, just reference from your first project (that still
                        > contains MyHandler).
                        >
                        > But no - two projects cannot reference eachother.
                        >
                        > Erik
                        >
                        >[/color]


                        Comment

                        • Dave S

                          #13
                          Re: Circular references ?

                          Erik,

                          Yes that's another approach.
                          Haven't tried that one out but I'm sure it will work because it removes the
                          need for project 2 being dependent on project 1.

                          Thanks
                          Dave


                          "Erik Frey" <eriksjunk@hotm ail.com> wrote in message
                          news:ubWsb7OHEH A.3832@TK2MSFTN GP10.phx.gbl...[color=blue]
                          > "Dave S" <dave_xxx70@hot mail.com> wrote in message
                          > news:5cccffe5c7 df71d9fdfd061c1 e6213f2@news.te ranews.com...[color=green]
                          > > Erik,
                          > >
                          > > Thanks for this, I just wanted to confirm that it's not possible to do[/color][/color]
                          it[color=blue][color=green]
                          > > with just 2 projects.[/color]
                          >
                          > If you don't want a third common class, your other option is to move the
                          > MyObject class declaration into the second project that you're trying to
                          > send it to. Then, just reference from your first project (that still
                          > contains MyHandler).
                          >
                          > But no - two projects cannot reference eachother.
                          >
                          > Erik
                          >
                          >[/color]


                          Comment

                          • Jason Sobell

                            #14
                            Re: Circular references ?

                            Hi,
                            It's something along the lines of:

                            Project 1:
                            Public Class MyObject
                            blah
                            End Class

                            Public Class MyHandler
                            Public Event RequestData(dat aObj as MyObject

                            Public Sub Doeventplease()
                            Dim obj As New MyObject(blah)
                            RaiseEvent RequestData(obj )
                            'obj may have been modified by the event handler...
                            End Sub
                            End Class


                            In you main Project2 app you can reference Project1.
                            Then, you can call your Project1 function, and if it needs to obtain more
                            info from Project1 it can do so via the event.

                            E.g.
                            Project2:

                            Private WithEvents P1object As New Project1.MyHand ler

                            Public Sub Main()
                            P1object.DoSome thing
                            'If the P1object requires some data from you, it may now generate an event
                            'that will be caught by the event handler below:

                            End Sub

                            Private Sub RequestDataHand ler(ByValue myob As Project1.MyObje ct) _
                            Handles P1object.Reques tData
                            'Do something with myob, or alter values in it etc.
                            End Sub


                            A typical place I use this is if I'm writing a custom control.
                            The control may require data from the form on which it's placed, and I
                            can't have a direct link from the control to a custom method on my form, so
                            I have events such as "RequestLanguag eEditor" or "RequestValidat ion" that
                            pass enough information to allow the underlying form to perform a
                            calculation, and I might have a 'ByRef Cancel As Boolean' to allow me to
                            easily return a status to the control itself.

                            I'm afraid this is mainly a mindset problem rather than technical, but
                            hopefully this gives an idea. If you give a more concrete problem
                            description then a few people may give alternative architectures.

                            Cheers,
                            Jason

                            p.s. The VB.Net code above is typed from memory, so the syntax may be dodgy
                            :)


                            On Wed, 07 Apr 2004 22:56:05 GMT, Dave S wrote:
                            [color=blue]
                            > Jason,
                            >
                            > Not sure I understand that one but I can try and look into raising events.
                            > Any code examples of this anywhere ?
                            >
                            > Thanks
                            > Dave
                            >
                            >
                            >
                            > "Jason Sobell" <iGadget_@hotma il.com> wrote in message
                            > news:70ds7tcggr n2.1cfdlkstq2s0 j.dlg@40tude.ne t...[color=green]
                            >> You can't directly call from B to A, but you can pass back events with the
                            >> package as a parameter, and this is the standard approach to your original
                            >> problem.
                            >> MyHandler simply raises the event without requiring intimate knowledge
                            >> (i.e. dependency) on project B.
                            >>
                            >> This is not a VB.Net specific issue. It applies to all OO languages. Java
                            >> uses interfaces or events to avoid the issue, and you can do the same in
                            >> VB.
                            >>
                            >> Cheers,
                            >> Jason
                            >>
                            >> On Wed, 07 Apr 2004 22:09:35 GMT, Dave S wrote:[color=darkred]
                            >>> Erik,
                            >>>
                            >>> Thanks for this, I just wanted to confirm that it's not possible to do[/color][/color]
                            > it[color=green][color=darkred]
                            >>> with just 2 projects.
                            >>> I tried moving my object into another project (so it's sort of an[/color][/color]
                            > interface[color=green][color=darkred]
                            >>> project) and then each of the other projects can reference that project
                            >>> without causing the circular reference problem.
                            >>> I just thought it was a bit messy when in Java you could easily do this
                            >>> without a middleman package containing the object you want to pass[/color][/color]
                            > between[color=green][color=darkred]
                            >>> the classes.
                            >>> At least I know that it wasn't something I'd missed out when declaring[/color][/color]
                            > the 2[color=green][color=darkred]
                            >>> projects initially.
                            >>> Will have to remember this one in future (I've been struggling with this[/color][/color]
                            > all[color=green][color=darkred]
                            >>> afternoon)
                            >>>
                            >>> Dave
                            >>>
                            >>>
                            >>>
                            >>>
                            >>> "Erik Frey" <eriksjunk@hotm ail.com> wrote in message
                            >>> news:uCVltrOHEH A.3556@TK2MSFTN GP10.phx.gbl...
                            >>>> "Dave S" <dave_xxx70@hot mail.com> wrote in message
                            >>>> news:ac629e82aa b3c366fe2c0c20e 78b5eee@news.te ranews.com...
                            >>>>> Anyone know how to get round the problem of circular references in
                            >>> VB.NET
                            >>>>> (sorry I'm a .NET newbie).
                            >>>>> I create one project which has 2 classes in it, MyHandler and[/color][/color]
                            > MyObject.[color=green][color=darkred]
                            >>>>> MyHandler just needs to call another class in a different project and
                            >>> pass
                            >>>> a
                            >>>>> parameter of type MyObject to it.
                            >>>>> So the class in the different project needs to import the first[/color][/color]
                            > project[color=green][color=darkred]
                            >>>>> namespace so it know what MyObject is.
                            >>>>> The MyHandler class needs to import the second project namespace so it
                            >>> can
                            >>>>> call the class in the second project.
                            >>>>
                            >>>> Typically this is adressed by creating a third project that contains[/color][/color]
                            > your[color=green][color=darkred]
                            >>>> business types in it.
                            >>>>
                            >>>> Then have your other two projects refer to this new project, to have
                            >>> access
                            >>>> to the common type (so in your case, put MyObject in the third[/color][/color]
                            > project).[color=green][color=darkred]
                            >>>>
                            >>>> Also, keep in mind that even if you have three projects in a solution,[/color][/color]
                            > you[color=green][color=darkred]
                            >>>> still have to add the project references manually (right click on
                            >>>> References - > Add References -> Projects tab)
                            >>>>
                            >>>> Erik
                            >>>>
                            >>>>[/color][/color][/color]

                            Comment

                            • Jason Sobell

                              #15
                              Re: Circular references ?

                              Hi,
                              It's something along the lines of:

                              Project 1:
                              Public Class MyObject
                              blah
                              End Class

                              Public Class MyHandler
                              Public Event RequestData(dat aObj as MyObject

                              Public Sub Doeventplease()
                              Dim obj As New MyObject(blah)
                              RaiseEvent RequestData(obj )
                              'obj may have been modified by the event handler...
                              End Sub
                              End Class


                              In you main Project2 app you can reference Project1.
                              Then, you can call your Project1 function, and if it needs to obtain more
                              info from Project1 it can do so via the event.

                              E.g.
                              Project2:

                              Private WithEvents P1object As New Project1.MyHand ler

                              Public Sub Main()
                              P1object.DoSome thing
                              'If the P1object requires some data from you, it may now generate an event
                              'that will be caught by the event handler below:

                              End Sub

                              Private Sub RequestDataHand ler(ByValue myob As Project1.MyObje ct) _
                              Handles P1object.Reques tData
                              'Do something with myob, or alter values in it etc.
                              End Sub


                              A typical place I use this is if I'm writing a custom control.
                              The control may require data from the form on which it's placed, and I
                              can't have a direct link from the control to a custom method on my form, so
                              I have events such as "RequestLanguag eEditor" or "RequestValidat ion" that
                              pass enough information to allow the underlying form to perform a
                              calculation, and I might have a 'ByRef Cancel As Boolean' to allow me to
                              easily return a status to the control itself.

                              I'm afraid this is mainly a mindset problem rather than technical, but
                              hopefully this gives an idea. If you give a more concrete problem
                              description then a few people may give alternative architectures.

                              Cheers,
                              Jason

                              p.s. The VB.Net code above is typed from memory, so the syntax may be dodgy
                              :)


                              On Wed, 07 Apr 2004 22:56:05 GMT, Dave S wrote:
                              [color=blue]
                              > Jason,
                              >
                              > Not sure I understand that one but I can try and look into raising events.
                              > Any code examples of this anywhere ?
                              >
                              > Thanks
                              > Dave
                              >
                              >
                              >
                              > "Jason Sobell" <iGadget_@hotma il.com> wrote in message
                              > news:70ds7tcggr n2.1cfdlkstq2s0 j.dlg@40tude.ne t...[color=green]
                              >> You can't directly call from B to A, but you can pass back events with the
                              >> package as a parameter, and this is the standard approach to your original
                              >> problem.
                              >> MyHandler simply raises the event without requiring intimate knowledge
                              >> (i.e. dependency) on project B.
                              >>
                              >> This is not a VB.Net specific issue. It applies to all OO languages. Java
                              >> uses interfaces or events to avoid the issue, and you can do the same in
                              >> VB.
                              >>
                              >> Cheers,
                              >> Jason
                              >>
                              >> On Wed, 07 Apr 2004 22:09:35 GMT, Dave S wrote:[color=darkred]
                              >>> Erik,
                              >>>
                              >>> Thanks for this, I just wanted to confirm that it's not possible to do[/color][/color]
                              > it[color=green][color=darkred]
                              >>> with just 2 projects.
                              >>> I tried moving my object into another project (so it's sort of an[/color][/color]
                              > interface[color=green][color=darkred]
                              >>> project) and then each of the other projects can reference that project
                              >>> without causing the circular reference problem.
                              >>> I just thought it was a bit messy when in Java you could easily do this
                              >>> without a middleman package containing the object you want to pass[/color][/color]
                              > between[color=green][color=darkred]
                              >>> the classes.
                              >>> At least I know that it wasn't something I'd missed out when declaring[/color][/color]
                              > the 2[color=green][color=darkred]
                              >>> projects initially.
                              >>> Will have to remember this one in future (I've been struggling with this[/color][/color]
                              > all[color=green][color=darkred]
                              >>> afternoon)
                              >>>
                              >>> Dave
                              >>>
                              >>>
                              >>>
                              >>>
                              >>> "Erik Frey" <eriksjunk@hotm ail.com> wrote in message
                              >>> news:uCVltrOHEH A.3556@TK2MSFTN GP10.phx.gbl...
                              >>>> "Dave S" <dave_xxx70@hot mail.com> wrote in message
                              >>>> news:ac629e82aa b3c366fe2c0c20e 78b5eee@news.te ranews.com...
                              >>>>> Anyone know how to get round the problem of circular references in
                              >>> VB.NET
                              >>>>> (sorry I'm a .NET newbie).
                              >>>>> I create one project which has 2 classes in it, MyHandler and[/color][/color]
                              > MyObject.[color=green][color=darkred]
                              >>>>> MyHandler just needs to call another class in a different project and
                              >>> pass
                              >>>> a
                              >>>>> parameter of type MyObject to it.
                              >>>>> So the class in the different project needs to import the first[/color][/color]
                              > project[color=green][color=darkred]
                              >>>>> namespace so it know what MyObject is.
                              >>>>> The MyHandler class needs to import the second project namespace so it
                              >>> can
                              >>>>> call the class in the second project.
                              >>>>
                              >>>> Typically this is adressed by creating a third project that contains[/color][/color]
                              > your[color=green][color=darkred]
                              >>>> business types in it.
                              >>>>
                              >>>> Then have your other two projects refer to this new project, to have
                              >>> access
                              >>>> to the common type (so in your case, put MyObject in the third[/color][/color]
                              > project).[color=green][color=darkred]
                              >>>>
                              >>>> Also, keep in mind that even if you have three projects in a solution,[/color][/color]
                              > you[color=green][color=darkred]
                              >>>> still have to add the project references manually (right click on
                              >>>> References - > Add References -> Projects tab)
                              >>>>
                              >>>> Erik
                              >>>>
                              >>>>[/color][/color][/color]

                              Comment

                              Working...