Creating a console typewriter-like program
-
I want to make a typewriter like program (you know, writes a string 1 letter at a time), so I tried to use a timer but the timer elapsed event has to be static, so then you don't have access to global variables \= I tried this but it doesn't work:
public string myString = "hello all"; public int i=0; static void Main(string[] args) { System.Timers.Timer t = new System.Timers.Timer(200); t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed); t.Start(); Console.Read(); } static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Program mypr = new Program(); Console.Write(mypr.getString()); mypr.addToI(); } public string getString() { if (i < myString.Length) return myString[i].ToString(); else return null; } public void addToI() { i++; }
because it creates a new instance of "Program" each time the timer is elapsed =\ so how can I do it? -
I want to make a typewriter like program (you know, writes a string 1 letter at a time), so I tried to use a timer but the timer elapsed event has to be static, so then you don't have access to global variables \= I tried this but it doesn't work:
public string myString = "hello all"; public int i=0; static void Main(string[] args) { System.Timers.Timer t = new System.Timers.Timer(200); t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed); t.Start(); Console.Read(); } static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Program mypr = new Program(); Console.Write(mypr.getString()); mypr.addToI(); } public string getString() { if (i < myString.Length) return myString[i].ToString(); else return null; } public void addToI() { i++; }
because it creates a new instance of "Program" each time the timer is elapsed =\ so how can I do it?Hi, Hope I didn't missunderstood your problem. Why not having a static string : public static string myString = "hello all"; so you have no need of creating a program object Marc PS: ElapsedEventHandler do not require a static method try: t.Elapsed += new System.Timers.ElapsedEventHandler(this.MyNonStaticMethod);