Share Data Between Classes

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

    #1

    Share Data Between Classes

    I'm working on a VB.Net DLL project. It has 6 classes. One of the classes
    has the properties for the database path, database name and server name. The
    purpose of this class is so that the DLL can be tested on different servers.
    Without using the keyword "Shared", how can I share these data to the other
    5 classes?

    Thanks for any ideas!

    Cesar


  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Share Data Between Classes

    Cesar,
    Oh! out of curiosity, why do you not want to use "Shared"?

    Hope this helps
    Jay

    "cksj" <cksanjose@hotm ail.com> wrote in message
    news:%23Zgygrxd DHA.3280@tk2msf tngp13.phx.gbl. ..[color=blue]
    > I'm working on a VB.Net DLL project. It has 6 classes. One of the classes
    > has the properties for the database path, database name and server name.[/color]
    The[color=blue]
    > purpose of this class is so that the DLL can be tested on different[/color]
    servers.[color=blue]
    > Without using the keyword "Shared", how can I share these data to the[/color]
    other[color=blue]
    > 5 classes?
    >
    > Thanks for any ideas!
    >
    > Cesar
    >
    >[/color]


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: Share Data Between Classes

      Cesar,
      Without Shared, each consumer of the class will need to create an instance
      of the class then access the properties. Which for this sort of thing the
      Singleton Pattern is better, you do however need to use Shared to implement
      a Singleton.

      You could make the class itself a Singleton, when means the properties will
      not be marked shared, however a read only shared Instance field will be
      created in the class.

      Public Class DbSettings

      Public Shared Readonly Instance As New DbSettings()

      Private Sub New()
      End Sub

      Public Property DatabasePath...

      End Class

      Then whenever you need to use any of the properties of the class you use
      DbSettings.Inst ance.DatabasePa th.

      Hope this helps
      Jay

      "cksj" <cksanjose@hotm ail.com> wrote in message
      news:%23Zgygrxd DHA.3280@tk2msf tngp13.phx.gbl. ..[color=blue]
      > I'm working on a VB.Net DLL project. It has 6 classes. One of the classes
      > has the properties for the database path, database name and server name.[/color]
      The[color=blue]
      > purpose of this class is so that the DLL can be tested on different[/color]
      servers.[color=blue]
      > Without using the keyword "Shared", how can I share these data to the[/color]
      other[color=blue]
      > 5 classes?
      >
      > Thanks for any ideas!
      >
      > Cesar
      >
      >[/color]


      Comment

      • Martin Platt

        #4
        Share Data Between Classes

        Cesar,

        You would have to make the other classes create your
        settings class internally. That would make it fairly
        loosely coupled, so you can test it on various backends.
        I'd say though, that if that is a helper type class, you
        might at least want consider why you wouldn't want to
        make it shared (or static as it is in C#)
        Somewhere or other you need to make the database settings
        class instance available within the memory spaces of the
        other 5 classes. To me, those are your two choices. If
        you're creating internally, then you have plenty of
        choices on how you might then go about doing this. If
        the 5 classes need the settings directly, then you could
        create a private property that controls the creation of
        the settings object for the user of the other 5 classes,
        or you could create it when the class in created, or you
        could create and destroy it only when it is needed to
        name but a few ways....

        Hope that's of help....[color=blue]
        >-----Original Message-----
        >I'm working on a VB.Net DLL project. It has 6 classes.[/color]
        One of the classes[color=blue]
        >has the properties for the database path, database name[/color]
        and server name. The[color=blue]
        >purpose of this class is so that the DLL can be tested[/color]
        on different servers.[color=blue]
        >Without using the keyword "Shared", how can I share[/color]
        these data to the other[color=blue]
        >5 classes?
        >
        >Thanks for any ideas!
        >
        >Cesar
        >
        >
        >.
        >[/color]

        Comment

        • Don Nablo

          #5
          Re: Share Data Between Classes

          If you have a dbconnection object that you want the other 5 classes to use
          the classic approach would be to just dim the db class object and pass it
          along to each method that requires in in the other classes

          so you would just have a method like

          public function getItem(ses as dbconnection, itemCode as string) as class1


          "cksj" <cksanjose@hotm ail.com> wrote in message
          news:#ZgygrxdDH A.3280@tk2msftn gp13.phx.gbl...[color=blue]
          > I'm working on a VB.Net DLL project. It has 6 classes. One of the classes
          > has the properties for the database path, database name and server name.[/color]
          The[color=blue]
          > purpose of this class is so that the DLL can be tested on different[/color]
          servers.[color=blue]
          > Without using the keyword "Shared", how can I share these data to the[/color]
          other[color=blue]
          > 5 classes?
          >
          > Thanks for any ideas!
          >
          > Cesar
          >
          >[/color]


          Comment

          • cksj

            #6
            Re: Share Data Between Classes

            Jay,

            Thanks for your help.
            I read that using Shared in DLL is not the best practice to use.

            Cesar

            "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in message
            news:OSF85GydDH A.1620@TK2MSFTN GP12.phx.gbl...[color=blue]
            > Cesar,
            > Oh! out of curiosity, why do you not want to use "Shared"?
            >
            > Hope this helps
            > Jay
            >
            > "cksj" <cksanjose@hotm ail.com> wrote in message
            > news:%23Zgygrxd DHA.3280@tk2msf tngp13.phx.gbl. ..[color=green]
            > > I'm working on a VB.Net DLL project. It has 6 classes. One of the[/color][/color]
            classes[color=blue][color=green]
            > > has the properties for the database path, database name and server name.[/color]
            > The[color=green]
            > > purpose of this class is so that the DLL can be tested on different[/color]
            > servers.[color=green]
            > > Without using the keyword "Shared", how can I share these data to the[/color]
            > other[color=green]
            > > 5 classes?
            > >
            > > Thanks for any ideas!
            > >
            > > Cesar
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: Share Data Between Classes

              Cesar,[color=blue]
              > I read that using Shared in DLL is not the best practice to use.[/color]
              Correct Shared should not be your first choice, however there are times when
              it is appropriate. It depends on the type of app. Like any construct
              available in .NET its usage has its pros & cons.

              As I said in my other email, you need to use it for the Singleton Pattern,
              what you asked sounds like a good application of the Singleton Pattern,
              assuming this is not an ASP.NET app. ASP.NET apps you need to use session &
              context aware methods of implementing a singleton. Which is where public
              shared methods (properties, subs & functions) are better than public shared
              fields. You can change the code of the method to better anticipate the
              environment you are running it.

              The Singleton Pattern is a common OOP Design Pattern to allow global access
              to an object. For details on the Singleton Patten and other Design Patterns
              see:
              1. "Design Patterns - Elements of Reusable Object-Oriented Software" by the
              GOF (Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides)
              2. "Patterns of Enterprise Application Architecture" by Martin Fowler
              3. "Visual Basic Design Patterns - VB 6.0 and VB.NET" by James W. Cooper.

              #3 is a good companion for #1, #2 shows some advanced patterns. All three
              are from Addison Wesley.

              Hope this helps
              Jay

              "cksj" <cksanjose@hotm ail.com> wrote in message
              news:eyRkqO6dDH A.2368@TK2MSFTN GP09.phx.gbl...[color=blue]
              > Jay,
              >
              > Thanks for your help.
              > I read that using Shared in DLL is not the best practice to use.
              >
              > Cesar
              >
              > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in[/color]
              message[color=blue]
              > news:OSF85GydDH A.1620@TK2MSFTN GP12.phx.gbl...[color=green]
              > > Cesar,
              > > Oh! out of curiosity, why do you not want to use "Shared"?
              > >
              > > Hope this helps
              > > Jay
              > >
              > > "cksj" <cksanjose@hotm ail.com> wrote in message
              > > news:%23Zgygrxd DHA.3280@tk2msf tngp13.phx.gbl. ..[color=darkred]
              > > > I'm working on a VB.Net DLL project. It has 6 classes. One of the[/color][/color]
              > classes[color=green][color=darkred]
              > > > has the properties for the database path, database name and server[/color][/color][/color]
              name.[color=blue][color=green]
              > > The[color=darkred]
              > > > purpose of this class is so that the DLL can be tested on different[/color]
              > > servers.[color=darkred]
              > > > Without using the keyword "Shared", how can I share these data to the[/color]
              > > other[color=darkred]
              > > > 5 classes?
              > > >
              > > > Thanks for any ideas!
              > > >
              > > > Cesar
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              Working...