Search code examples
c#pass-by-referencepass-by-value

Using Pass by value or Pass by Reference?


I have two methods in my code. Below is one of them.

 private async void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
    {
        var newValue = FormatValueByPresentation(args.CharacteristicValue, presentationFormat);
        var message = $"Value at {DateTime.Now:hh:mm:ss.FFF}: {newValue}";
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () => CharacteristicLatestValue.Text = message);
    }

And it's printing time (Value At) like this.

Now, this is the second method.

 private static ushort ParseHeartRateValue(byte[] data)
    {

        const byte heartRateValueFormat = 0x04;

        byte flags = data[0];
        ushort offset = 1;

        bool HRC2 = (flags & 0x80) > 0; 

        if (HRC2) //if BPM is un uint16
        {
            short hr = BitConverter.ToInt16(data, offset);
            offset += 2;
            System.Diagnostics.Debug.WriteLine("We have 16:" + hr.ToString("x"));

        }
        else // if BPM is uint8
        {
            byte hr = data[offset];
            offset += 1;
            System.Diagnostics.Debug.WriteLine("no 16:" + hr.ToString("x"));
        }



        bool ee = (flags & (1 << 3)) != 0;
        if (ee)
            offset += 2;



        // bool rr = ((flags & 1 << 4) != 0);
        bool rr = ((flags & 0x10) != 0);
        if (rr)
        {
            int count = (data.Length - offset) / 2;
            for (int i = 0; i < count; i++)
            {


                ushort value = BitConverter.ToUInt16(data, offset);

                intervals.Add((double)value); // Added
                if (intervals.Count > 190) // Added
                    intervals.RemoveAt(0);// Added 
                double mean = intervals.Average();// Added
                double sumOfSquareDiff = intervals.Select(val => (val - mean) * (val - mean)).Sum(); // Added
                double vrHR = Math.Sqrt(sumOfSquareDiff / intervals.Count); // Added

                double intervalLengthInSeconds = value / 1024.0;
                offset += 2;

                System.Diagnostics.Debug.WriteLine("Heart Rate Variability:" + vrHR.ToString());
            }
        }

And it's printing the output like this.

But I want the Heart Rate Variability to print just below "Value at". How do I make that work ?

Should I do pass by value or reference? Any other suggestions ?

I asked more detailed question earlier here on Stack Overflow


Solution

  • But I want the Heart Rate Variability to print just below "Value at". How do I make that work ?

    Your question completely was unrelated to 'pass by value or reference'. The CharacteristicLatestValue just is a TextBlock control on XAML page. It's used to show text on UI like the following:

    Value at 01:11:25:453: Heart Rate: 124

    If you want to show 'no 16:51', 'Heart Rate Varibility:661841865028902' etc these texts blow it like the following:

    Value at 01:11:25:453: Heart Rate: 124
    no 16:51
    Heart Rate Varibility:661841865028902
    

    You just need to add them after the CharacteristicLatestValue.Text like the following:

    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () => CharacteristicLatestValue.Text = "Value at 01:11:25:453: Heart Rate: 124"+"\r\n"+ "no 16:51"+"\r\n"+ "Heart Rate Varibility:661841865028902"+"\r\n");