CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Changing value in variable from another called class

  1. #1
    Join Date
    Aug 2008
    Posts
    108

    Exclamation Changing value in variable from another called class

    Hey all I have been racking my brain trying to figure out what it is I am missing from the code below in order for the value "7:50 pm" to be placed into the variable theCntDownTimerTime from the TinyWS.cs class.

    Let say the code below starts out with the time "7:00 pm".

    I send the command from PostMAN (7:50 pm) and it picks it up just fine and the jsonSent.changeCNT does have the correct value of "7:50 pm". However, after this it seems to lose that value once the timer updates in the clockCoiuntDown.xaml.cs page for the theCntDownTimerTime.ToLower(). That value is still the same value (7:00 pm) as it was when the program started up and read my text file to get the default time.

    clockCountDown.xaml.cs
    Code:
    namespace laptopWatcher {  
      public partial class clockCountDown: Window {  
        public string theCntDownTimerTime = "";  
      
        public clockCountDown() {  
          InitializeComponent();  
          //get countdown time  
          theCntDownTimerTime = getTimeToStart();  
        }  
      
        private void Window_Loaded(object sender, RoutedEventArgs e) {  
          var desktopWorkingArea = SystemParameters.WorkArea;  
          this.Left = desktopWorkingArea.Right - this.Width - 20;  
          this.Top = desktopWorkingArea.Top + this.Height - 30;  
          var ts = ((60 - DateTime.Now.Second) * 1000 - DateTime.Now.Millisecond);  
      
          clockTimer.Tick += new EventHandler(clockTimer_tick);  
          clockTimer.Interval = new TimeSpan(0, 0, 35);  
          clockTimer.Start();  
      
          //Populate the time once to show to user  
          bgTxt.Text = DateTime.Now.ToString("h:mm tt");  
          txt.Text = DateTime.Now.ToString("h:mm tt");  
      
          //check if time for count down  
          if (txt.Text.ToLower().Equals(theCntDownTimerTime.ToLower())) {  
            _timer.Stop();  
            TimeSpan time = TimeSpan.Parse(theCntDownTimerTime);  
            cdownTimer((_time).TotalSeconds);  
          }  
      
          _tws = new TinyWS();  
          Thread t = new Thread(new ThreadStart(_tws.startWS));  
      
          t.Start();  
        }  
      
        private void clockTimer_tick(object sender, EventArgs e) {  
          bgTxt.Text = DateTime.Now.ToString("h:mm tt");  
          txt.Text = DateTime.Now.ToString("h:mm tt");  
          Console.WriteLine(txt.Text.ToLower() + " vs " + theCntDownTimerTime.ToLower());  
        }  
      
        public string getTimeToStart() {  
          string line;  
      
          using(StreamReader sr = new StreamReader(Environment.CurrentDirectory + "\\timestart.txt")) {  
            line = sr.ReadLine();  
          }  
      
          return line.ToLower();  
        }  
      
        // etc etc....  
      }  
    }
    TinyWS.cs
    Code:
    namespace laptopLogin {  
      class TinyWS: clockCountDown {  
        HttpListener listener = new HttpListener();  
        clockCountDown ccd = new clockCountDown();  
      
        public class receivedData {  
          public string changeCNT {  
            get;  
            set;  
          }  
          public int timeAdd {  
            get;  
            set;  
          }  
          public bool internet {  
            get;  
            set;  
          }  
          public bool shutdownNow {  
            get;  
            set;  
          }  
        }  
      
        public void startWS() {  
          var prefixes = new List < string > () {  
            "http://*:8888/"  
          };  
      
          foreach(string s in prefixes) {  
            listener.Prefixes.Add(s);  
          }  
      
          listener.Start();  
          Console.WriteLine("Listening...");  
      
          while (true) {  
            HttpListenerContext context = listener.GetContext();  
            HttpListenerRequest request = context.Request;  
      
            string documentContents;  
      
            using(Stream receiveStream = request.InputStream) {  
              using(StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8)) {  
                documentContents = readStream.ReadToEnd();  
              }  
            }  
      
            Console.WriteLine($"Recived request for {request.Url}");  
            Console.WriteLine(documentContents);  
            HttpListenerResponse response = context.Response;  
      
            //Check out the response  
            receivedData jsonSent = JsonConvert.DeserializeObject < receivedData > (documentContents);  
      
            if (jsonSent.changeCNT.ToLower() != ccd.getTimeToStart()) {  
              //The default shutdown time has changed so update the txt file.  
              using(StreamWriter sw = new StreamWriter(Environment.CurrentDirectory + "\\timestart.txt", false)) {  
                sw.WriteLine(jsonSent.changeCNT);  
              }  
      
              ccd.theCntDownTimerTime = jsonSent.changeCNT;  
            }  
      
            //Reply  
            string responseString = "hit";  
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);  
            response.ContentLength64 = buffer.Length;  
            Stream output = response.OutputStream;  
            output.Write(buffer, 0, buffer.Length);  
            output.Close();  
          }  
        }  
      
        public void stopWS() {  
          listener.Stop();  
        }  
      }  
    }
    So what would I be missing? It looks fine to me but just doesn't store the value over.

    I also have tried:
    Code:
    public string theCntDownTimerTime = "";  
    public string _newCNT {  
      get {  
        return theCntDownTimerTime;  
      }  
      set {  
        theCntDownTimerTime = value;  
      }  
    }
    And on the TinyWS.cs page I have it sending by like ccd._newCNT = jsonSent.changeCNT;

    But that also does not hold the new value.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,477

    Re: Changing value in variable from another called class

    What happens when you set some breakpoints and step through the code?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)