Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Creating a console typewriter-like program

Creating a console typewriter-like program

Scheduled Pinned Locked Moved C#
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sharpiesharpie
    wrote on last edited by
    #1

    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?

    G 1 Reply Last reply
    0
    • S sharpiesharpie

      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?

      G Offline
      G Offline
      girm
      wrote on last edited by
      #2

      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);

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups