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 7969713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:16:48+00:00 2026-06-04T07:16:48+00:00

Hi I am trying to make a global class that I can use in

  • 0

Hi I am trying to make a global class that I can use in my projects, I intend to use this as my default template, I am a newbie so please be patient 😉

Stopwatch Masterstopwatch = new Stopwatch();

#if DEBUG

    private static void ApplicationLogStart()
    {
        StartTime = DateTime.Now;
        Masterstopwatch.Start();

        String eventName = "Program Loaded";
        String errorDetails = "Program has started Succesfully";
        DataLogEntry(eventName, errorDetails);
    }
    private static void ApplicationLogclosing()
    {
        String eventName = "Program is closing";
        String errorDetails = "Program has closed Succesfully";
        DataLogEntry(eventName, errorDetails);
        StopTime = DateTime.Now;
        Masterstopwatch.Stop();
        Benchmark(StartTime,StopTime,Masterstopwatch.Elapsed);
    }
#endif

I suspect that I am fundimently flawed in my design as I want the Stopwatch Masterstopwatch = new Stopwatch(); to be declared globally without using a method, I suspect that this is not posible, but I need to ask
Thanks

  • 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-04T07:16:49+00:00Added an answer on June 4, 2026 at 7:16 am

    Sounds like you need the Singleton Pattern.

    If you declare a wrapper around stopwatch as follows you can use it anywhere in your app and access the same instance of a Stopwatch.

    // Declare singleton wrapper of a stopwatch, which instantiates stopwatch
    // on construction
    public class StopwatchProxy 
    {
        private Stopwatch _stopwatch;
        private static readonly StopwatchProxy _stopwatchProxy = new StopwatchProxy();
    
        private StopwatchProxy()
        {
            _stopwatch = new Stopwatch();
        }
    
        public Stopwatch Stopwatch { get { return _stopwatch; } } 
    
        public static StopwatchProxy Instance
        { 
            get { return _stopwatchProxy; }
        }
    }
    
    // Use singleton
    class Foo
    {
        void Foo()
        {
            // Stopwatch instance here
            StopwatchProxy.Instance.Stopwatch.Start();
        }
    }
    
    class Bar
    {
        void Bar()
        {
            // Is the same instance as here
            StopwatchProxy.Instance.Stopwatch.Stop();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the new(ish) AS3 global error handling class. I am trying
I am trying to make global structure, which will be seen from any part
Trying to make a custom :confirm message for a rails form that returns data
Trying to make a simple program to catalogue books. Something like this, for example:
Trying to make a simple program to catalogue books. Something like this, for example:
Trying to make a toggle button that hides/shows the right window/pane. Here is a
I am receiving errors and at the same time trying to make this work
I am trying to make a validator that will make sure that at least
I'm trying to make a simple TCP server using Twisted ,which can do some
So my problem is this. I have a class called Globals that contains all

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.