Yet another global vars question

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

    #1

    Yet another global vars question


    I've been programming with Delphi for the past 4 years or so and while
    Delphi does allow globals, I use them very judiciously. I say that I
    *do* use them because I think that in some cases they are a good choice.

    Now in C# there are no global vars. MSDN and google say that I can use
    static classes (err, or static methods in classes) like Type.TypeOf,
    etc.

    What if I have an object that contains resources for the rest of my
    application such as a pre-opened database connection or maybe static
    data that makes sense to keep in memory because it's used so often. Is
    overriding the constructor of every object in the application the only
    (best? Advised?) way of sharing an object accross the application?

    Thanks for any clarity..

    --
    Warm Regards,
    Lee

    "Upon further investigation it appears that your software is missing
    just one thing. It definitely needs more cow bell..."
  • Jon Skeet [C# MVP]

    #2
    Re: Yet another global vars question

    Lee <luv2program200 0@yahoo.com> wrote:[color=blue]
    > I've been programming with Delphi for the past 4 years or so and while
    > Delphi does allow globals, I use them very judiciously. I say that I
    > *do* use them because I think that in some cases they are a good choice.
    >
    > Now in C# there are no global vars. MSDN and google say that I can use
    > static classes (err, or static methods in classes) like Type.TypeOf,
    > etc.
    >
    > What if I have an object that contains resources for the rest of my
    > application such as a pre-opened database connection or maybe static
    > data that makes sense to keep in memory because it's used so often. Is
    > overriding the constructor of every object in the application the only
    > (best? Advised?) way of sharing an object accross the application?[/color]

    No, in those cases you can use public static members - preferrably
    either constants or properties which can return the data (possibly
    having lazily loaded them, etc).

    For database connections, however, you should usually just let the
    built-in connection pooling do the job for you - open the connection
    and close it as early as possible, and the connection pool will keep
    the *actual* connection open for you.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Michael Nemtsev

      #3
      Re: Yet another global vars question

      Hello lee,

      For your reason pooling and cache is solutions.
      Connections generally keeps in pool.
      If u want some object to be in memmory - keep them in cache.
      Sharing object accross application depends on this app. it's either on singleton/monostate
      object, or horisonted scaled object

      l> I've been programming with Delphi for the past 4 years or so and
      l> while Delphi does allow globals, I use them very judiciously. I say
      l> that I *do* use them because I think that in some cases they are a
      l> good choice.
      l>
      l> Now in C# there are no global vars. MSDN and google say that I can
      l> use static classes (err, or static methods in classes) like
      l> Type.TypeOf, etc.
      l>
      l> What if I have an object that contains resources for the rest of my
      l> application such as a pre-opened database connection or maybe static
      l> data that makes sense to keep in memory because it's used so often.
      l> Is overriding the constructor of every object in the application the
      l> only (best? Advised?) way of sharing an object accross the
      l> application?
      l>
      l> Thanks for any clarity..
      l>
      l> "Upon further investigation it appears that your software is missing
      l> just one thing. It definitely needs more cow bell..."
      l>
      ---
      WBR,
      Michael Nemtsev :: blog: http://spaces.msn.com/laflour

      "At times one remains faithful to a cause only because its opponents do not
      cease to be insipid." (c) Friedrich Nietzsche


      Comment

      Working...