Number ToString errors

I have to be doing something stupid here, but when I try ToString on number types (int, double) it gives me “Could not evaluate the expression”. I have verified that it has a valid value, hovering over the double shows 23.22, but I can’t seem to turn it into a string. My variable is class scope and set by MeasurementComplete from a Temperature & Humidity sensor. I have tried:


Temperature.ToString();
Temperature.ToString("0.0");
Temperature.ToString("F1");
((double)23.22).ToString();
((int)23).ToString();

I have tried other values, big, small, whole, decimal, negative, but I get the error every time. I would appreciation a good swift digital kick in the pants on what I’m doing wrong.

DateTime.Now.ToString(“MM/dd/yyyy hh:mm:ss”) works fine as does boolean ToString, so it seems to just be numbers.

This is the code I am using to display the temperature from the Temperature and Humidity sensor, hope it helps:

    void OnTempReading(TemperatureHumidity sender, double temperature, double relativeHumidity)
    {
        Debug.Print("Temperature = " + temperature + "C " + (temperature * 9 / 5 + 32) + "F");
        oledDisplay.SimpleGraphics.ClearNoRedraw();
        oledDisplay.SimpleGraphics.DisplayText((temperature * 9 / 5 + 32).ToString("N") + " F", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Blue, xloc, 30);
        oledDisplay.SimpleGraphics.DisplayText(temperature.ToString("N") + " C ", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Orange, xloc, 0);

Thanks for the advice, here is the code a little more readable:


        void OnTempReading(TemperatureHumidity sender, double temperature, double relativeHumidity)
        {
            currentTemperature = temperature;
            Debug.Print("Temperature = " + temperature + "C " + (temperature * 9 / 5 + 32) + "F");
            oledDisplay.SimpleGraphics.ClearNoRedraw();
            oledDisplay.SimpleGraphics.DisplayText((temperature * 9 / 5 + 32).ToString("N") + " F", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Blue, xloc, 30);
            oledDisplay.SimpleGraphics.DisplayText(temperature.ToString("N") + " C ", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Orange, xloc, 0);

Ok, I have isolated the problem to the event that I was trying to do the conversion in. If I convert inside the MeasurementComplete event it works fine, but if I try to convert to string inside the Packet Received event I get the error. I have it working so that I store the value as a string instead of a double, but I don’t understand why I get an error when converting inside the Packet Received event.

Here’s the code. xml.AppendLine(@ " <sensor name="“Temperature”" value=""" + Temperature.ToString() + @ “”" />"); throws the error. When it does error I can use the immediate window to see that a valid value is in Temperature, but even in the immediate window, Temperature.ToString() throws an error. Moved the conversion to a different event and it seems to work fine.


        private double Temperature = -1;

        void ProgramStarted()
        {
            temperatureHumidity.MeasurementComplete += new TemperatureHumidity.MeasurementCompleteEventHandler(temperatureHumidity_MeasurementComplete);
            temperatureHumidity.StartContinuousMeasurements();

            Networking.Adapter.Start(new byte[] { 0x5c, 0x86, 0x4a, 0x00, 0x00, 0xde }, "mip", Networking.InterfaceProfile.Cerberus_Socket6_ENC28);
            Networking.Adapter.OnHttpReceivedPacketEvent += new Networking.Adapter.HttpPacketReceivedEventHandler(Adapter_OnHttpReceivedPacketEvent);
            Networking.Adapter.ListenToPort(80);  // Listen on Port 80, the default web server port

            Debug.Print("Program Started");
        }

        void Adapter_OnHttpReceivedPacketEvent(Networking.HttpRequest request)
        {
            System.Text.StringBuilder xml = new System.Text.StringBuilder();
            xml.AppendLine(@ "  <sensor name=""Temperature"" value=""" + Temperature.ToString() + @ """ />");
            byte[] webPage = System.Text.Encoding.UTF8.GetBytes(xml.ToString());
            var s = new System.IO.MemoryStream(webPage);
            Networking.HttpResponse resp = new Networking.HttpResponse(s);
            resp.ContentType = "text/xml";
            request.SendResponse(resp);
            Networking.Adapter.StopListeningToPort(80);
            Networking.Adapter.ListenToPort(80);
        }

        void temperatureHumidity_MeasurementComplete(TemperatureHumidity sender, double temperature, double relativeHumidity)
        {
            Temperature = temperature;
        }

Ok, you are right, it must be thread issues. I’m rather surprised to see a thread issue on a conversion when I was able to get the value without issue, but that does appear to be the problem.