Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a public class that I store my two variables one for current time as string "tx" and the other for timer class "myT" as follow:

C#
public static class MainVariables
    {

        private static string  _tx = "";
        public static string tx
        {
            get { return MainVariables ._tx; }
            set { MainVariables ._tx = value; }
        }
        private static System.Windows.Forms.Timer _myT =
                new System .Windows .Forms .Timer();
        public static System .Windows .Forms .Timer myT
        {
            get { return MainVariables ._myT; }
            set { MainVariables ._myT = value; }
        }


    }


after initializing windows component i have a method that initialize my components called "myInitialize" as follow:
C#
public MainWindow()
        {
            InitializeComponent();
            myInitialize();
        }


in my initialize method I have the following code:
C#
private void myInitialize()
        {
            ShowMeTheTime();
}

and here is the ShowMeTheTime method:

C#
public void ShowMeTheTime()
        {
            MainVariables .myT .Enabled = true;
            MainVariables .myT .Interval = 500;
           
            MainVariables .myT .Tick += new EventHandler( ShoWTIME );
            MainVariables .myT .Start();

        }


        public void ShoWTIME( object obj , EventArgs e )
        {
            MainVariables .tx = string .Empty;
            MainVariables .tx = DateTime .Now .ToLongTimeString();
        }


So here is the question. Why my variable "MainVariables .tx " in "ShoWTIME" method
does not get any value when the timer tick?
However, if I raplace the "MainVariables .tx" with a textbox1.Text the text property of textbox1 get value and show me the current time every second?

Yours SIncerely
Posted
Updated 9-Jul-12 22:43pm
v3

1 solution

It is getting value. i tested it on my end.

check it as

C#
public void ShoWTIME(object obj, EventArgs e)
        {
            MainVariables.tx = string.Empty;
            MainVariables.tx = DateTime.Now.ToLongTimeString();
            Console.WriteLine(MainVariables.tx);
        }


and observer the output window in VS IDE. you will see the time is coming in the variable. the problem could be somewhere else.
 
Share this answer
 
Comments
Mmohmmad 10-Jul-12 5:07am    
IN console application you will get the result. However, build it in a WPF project you will see that the textbox1.text get value but the string value doesn't.
Rahul Rajat Singh 10-Jul-12 5:09am    
YES i tested it on that only. I can still print console messages in windows applications too for debugging purposes.
Mmohmmad 10-Jul-12 5:24am    
When I debug the program the string "tx" doesnot get value. I passed the "tx" value to a textbox and textbox does not show me the current time every .5secs.
Mmohmmad 10-Jul-12 5:31am    
I think the way that I show the "tx" result is inappropariate. I want a class to show me the current time and in that class i set the value of the "tx" variable and in my second class i.e. the window class I want to show this time in a textbox.text. I think the problem is that the value of current time must show in a textbox of window class with "tx" variable. Am I right?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900