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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:39:40+00:00 2026-05-13T05:39:40+00:00

Is there any possible way to access the field – str in the Class

  • 0

Is there any possible way to access the field – str in the Class Program and the variable num – in the main function?

class Program
{
    string str = "This is a string";
    static void Main(string[] args)
    {
        int num = 100;
        Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
        var timer = new System.Timers.Timer(10000);
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        timer.Start();
        for (int i = 0; i < 20; i++)
        {
            Debug.WriteLine(Thread.CurrentThread.ManagedThreadId + " " + "current I is " + i.ToString());
            Thread.Sleep(1000);
        }
        Console.ReadLine();
    }

    static void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        Debug.WriteLine(str);
        Debug.WriteLine(num);
        Debug.WriteLine(Thread.CurrentThread.ManagedThreadId + "  current is timer");
        //throw new NotImplementedException();
    }
}

Best Regards,

  • 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-05-13T05:39:40+00:00Added an answer on May 13, 2026 at 5:39 am

    The field just needs to be made static.

    static string str = "This is a string";
    

    To access num you need to use a lambda expression.

    timer.Elapsed += new ElapsedEventHandler((s, e) =>
    {
         Debug.WriteLine(str);
         Debug.WriteLine(num);
         Debug.WriteLine(Thread.CurrentThread.ManagedThreadId + "  current is timer");
    
    });
    

    You can also use an anonymous method.

    timer.Elapsed += new ElapsedEventHandler(delegate(object sender, ElapsedEventArgs e)
    {
         Debug.WriteLine(str);
         Debug.WriteLine(num);
         Debug.WriteLine(Thread.CurrentThread.ManagedThreadId + "  current is timer");
    
    });
    

    There is one more option. The System.Threading.Timer class allows you to pass a state object.

    var timer = new System.Threading.Timer((state) =>
    {
         Debug.WriteLine(str);
         Debug.WriteLine(state);
         Debug.WriteLine(Thread.CurrentThread.ManagedThreadId + "  current is timer");
    }, num, 10000, 10000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to add a field to a class at runtime (
Is there any way to access server side asp.net variable in javascript? I want
Is there any way to access the backing field for a property in order
Is there any possible way to PLAY a you tube video on page load
Is there any possible way to apply more than CSS to a control through
Possible Duplicate: Overriding the power button in Android Is there any possible way I
I was wondering if there was any possible way to get the number (count)
Is there any way to (robustly) reset any possible :after and :before CSS rules
Possible Duplicate: Is there any way to determine text direction from CultureInfo in asp.net?
My question is, Is there any way or is it possible to call Android

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.