Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8001493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:06:22+00:00 2026-06-04T16:06:22+00:00

I am planning to use a shared variable to implement a logging facility. Have

  • 0

I am planning to use a shared variable to implement a logging facility. Have a look at the code below:

Imports System.IO

Public Class TestClass

    Public Shared objError As New StreamWriter("C:\Test.txt")

End Class

 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            TestClass.objError.WriteLine("Error 1")
            TestClass.objError.WriteLine("Error 2")
            TestClass.objError.WriteLine("Error 3")
            'TestClass.objError.Close()
            TestClass.objError.WriteLine("Error 4")
            TestClass.objError = Nothing
        Catch ex As Exception

        End Try
    End Sub

I don’t understand how this shared variable is created and destroyed (I assume it is created before the form_load and destroyed by the form_unload). I also don’t understand why it is possible to set the reference to the static variable to Nothing; surely the variable should exist until the program ends? (Q1, part 1) I realise this is a simple question.

Is there a better way to implement a logging mechanism? (Q1 part 2). The logging mechanism writes errors and log entries.

UPDATE
I think I have found my answer here: http://msdn.microsoft.com/en-us/library/z2cty7t8.aspx. “A static variable continues to exist and retains its most recent value. The next time your code calls the procedure, the variable is not reinitialized, and it still holds the latest value that you assigned to it. A static variable continues to exist for the lifetime of the class or module that it is defined in.”

I am not convinced that this is the best way to create a logging facility. Therefore part 2 to my question is still open.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:06:24+00:00Added an answer on June 4, 2026 at 4:06 pm

    You’re confusing between the area of memory reserved for the variable Public Shared objError and the instance of an object created and assigned to this area As New StreamWriter("C:\Test.txt").

    When you declare the variable objError, the compiler reserves the memory required, but doesn’t fill it with a valid value (an instance of an object).
    Is the New command that initializes this area with an object instance of a StreamWriter.

    When you assign to the variable the value Nothing you are wiping the instance of the StreamWriter (really I prefer to call explicitily Flush and Close), but you are not deleting the area of memory reserved for the Shared variable.

    Regarding the log. You ask ‘which is the best way’ then the best method is to use an external component as you have been recommended. It will be very difficult to create a better logging system, log4net has been tried and tested, it’s free and it is considered the best.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am planning to use serialization to do clone. I have to make my
I have a Rails 3 app and I am planning to use the ActiveRecord
I have a new project starting up, and I was planning to use git
I'm planning to use this piece of code in my Asp.net app string strUserInputtedHashedPassword
I am planning to use Hadoop on EC2. Since we have to pay per
Am planning to use cookies to communicate between two browser windows. Am wondering if
I am planning to use SCTP for a embedded Linux project. Anybody has some
I am planning to use Auto reset Event Handle for Inter Thread communication. EventWaitHandle
I am planning to use libcurl in my project. I had downloaded the library
We are planning to use bazaar as a version control tool over cvs and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.