1

I have a simple form with a timer and where I placed a label. I am new to c# but managed it to store time in a string, but I can't show this string during the load of the form since it sits in the timer function...

public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    timer1.Enabled = true;
    timer1.Interval = 1000;

    //clockLabel.Text = "00:00:00";
    clockLabel.Text = time;

}

private void timer1_Tick(object sender, EventArgs e)
{
    string time = DateTime.Now.ToString("hh:mm:ss"); // stored time in string
    clockLabel.Text = time;

}

The problem is that Form1_Load doesn't know the time string. Can someone help a beginner to understand how I can get it to work?

JoshuaTheMiller
  • 2,582
  • 25
  • 27
Dennis
  • 33
  • 4

2 Answers2

2

Well.. you can declare a private string at the top of your code like below:

private string _time; 

public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Interval = 1000;

            //clockLabel.Text = "00:00:00";
            clockLabel.Text = _time;

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            _time = DateTime.Now.ToString("hh:mm:ss"); // stored time in string
            clockLabel.Text = _time;

        }
Masoud Andalibi
  • 3,168
  • 5
  • 18
  • 44
  • Thanks for helping, this is neat! I will now have to learn more about the difference between strings and private strings. – Dennis Jan 09 '17 at 02:49
  • 2
    @Dennis no problem, the thing you need to know are "Whats the difference between `public` and `private` and `protected` which i suggest you read this very carefully because they come very handy from now on : http://stackoverflow.com/questions/614818/what-is-the-difference-between-public-private-protected-and-nothing – Masoud Andalibi Jan 09 '17 at 03:03
  • Awesome! I will go through this now. – Dennis Jan 09 '17 at 03:07
  • @Dennis Goodluck mate! And dont forget to accept the solution =) – Masoud Andalibi Jan 09 '17 at 03:09
  • 1
    I think I see some of the confusion that was occurring now. An interesting thing about C#: [class fields are private by default](http://stackoverflow.com/q/6095980/1542187). Therefore, the example from Mohit's answer and this one are exactly the same except for the fact that `time` is specified as `private` here (and it is implicitly private in the other answer). – JoshuaTheMiller Jan 09 '17 at 05:13
  • To add to one of @Valkyriee's earlier comments: [What should I do when someone answers my question?](http://stackoverflow.com/help/someone-answers) :) – JoshuaTheMiller Jan 09 '17 at 05:22
  • @JoshuaMiller well said – Masoud Andalibi Jan 09 '17 at 06:35
1

You can make the string time variable to Global Variable which could be access anywhere.

string time;
public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    timer1.Enabled = true;
    timer1.Interval = 1000;

    //clockLabel.Text = "00:00:00";
    clockLabel.Text = time;

}

private void timer1_Tick(object sender, EventArgs e)
{
    time = DateTime.Now.ToString("hh:mm:ss");
    clockLabel.Text = time;

}
Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • 1
    Awesome! Didn't know this would be possible. Works great! Thanks for helping! – Dennis Jan 09 '17 at 02:34
  • I would generally suggest not using a "global" variable unless you understand all of the ramifications of it. For anyone coming across this question, I would suggest reading [this answer](http://softwareengineering.stackexchange.com/a/148109/244558) as a starting point for why you should be cautious with global variables. – JoshuaTheMiller Jan 09 '17 at 04:24
  • 1
    Also, the variable `time`, as shown above, appears to be a private [field](https://msdn.microsoft.com/en-us/library/ms173118.aspx) of the `Form1` class. – JoshuaTheMiller Jan 09 '17 at 04:33
  • Hi @Dennis if this answer has solved your question please consider [accepting it](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Mohit S Jan 10 '17 at 01:38
  • Hi Mohit. Didn't know this would be possible, still new to this community but your information's definitely helped me and I will mark this! Thanks for the help! – Dennis Jan 19 '17 at 21:46