使用按值传递还是按引用传递? [英] 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);
}
它的打印时间(Value At)就像这个.
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());
}
}
它正在打印输出,如this.
但我希望心率变异性打印在Value at"的正下方.我该怎么做?
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 ?
我之前在 Stack 上问了更详细的问题溢出
推荐答案
但我希望心率变异性打印在Value at"的正下方.我该怎么做?
But I want the Heart Rate Variability to print just below "Value at". How do I make that work ?
您的问题与通过值或引用"完全无关.CharacteristicLatestValue
只是 XAML 页面上的 TextBlock 控件.它用于在 UI 上显示文本,如下所示:
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:
01:11:25:453 处的值:心率:124
如果您想显示no 16:51"、Heart Rate Varibility:661841865028902"等,这些文本如下所示:
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
您只需要在 CharacteristicLatestValue.Text
之后添加它们,如下所示:
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");
这篇关于使用按值传递还是按引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!