Results 1 to 4 of 4

Thread: [2005] Shared objects VS Local objects

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Location
    Surabaya, Indonesia
    Posts
    163

    [2005] Shared objects VS Local objects

    I have these variable:
    vb Code:
    1. Public Shared SQLConn As SqlClient.SqlConnection

    It use for doing ADO.NET connection. So, after I finished database processing, I close and re-use that variable. Is it wise? Which are better than I use this on every ADO.NET transaction?
    vb Code:
    1. Using MyConn As New SqlClient.SqlConnection
    2.     'DB processing here
    3. End Using

    Since I heard that Garbage Collector won't remove an object immediately, I thought an idea about using a single object repeatedly than create and dispose an object each processing.

    Regards,

    Michael
    Last edited by michaelrawi; Dec 7th, 2007 at 04:37 AM. Reason: Add the VB code tag
    Check my Blog at VB Corner,Component Crafts

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Shared objects VS Local objects

    Declaring a connection at the class level is fine, but there's no reason for the variable to be Shared.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Location
    Surabaya, Indonesia
    Posts
    163

    Re: [2005] Shared objects VS Local objects

    What I want is to use the object in DB processing in other class as well. That's why I declare it on shared level.

    Is it safe / wise ?
    Check my Blog at VB Corner,Component Crafts

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Shared objects VS Local objects

    It's safe but unnecessary.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width