如何从静态方法修改不同的类字段?

问题描述

我是 C# 世界的新手,我正在尝试创建我的第一个项目。 我想创建读取 Ant+ 心率传感器并分析接收到的数据的应用程序。 我创建了 2 个类

public class Athlete
    { 
        public string Name { get; set; }
        public string Surname { get; set; }
        public int HBrate { get; set; }
        public int HBcount { get; set; }
        }
    }
public class AntSensor
    {
        public string Name { get; set; }
        public string SensorID { get; set; }
        public byte ChannelNr { get; set; }
    }
}

用户界面中选择运动员和传感器后,我初始化心率传感器并打开接收器通道

public void AthleteStart()
        {
            if ((AthleteSelected != null) || (ANTSensorSelected != null))
            {

                AthleteLabelTxt = $"{AthleteSelected.Name}";
                ANT_Device device0 = new ANT_Device();
                ANT_Channel channel0 = device0.getChannel(0x00);
                HRProject.AntApps(AthleteSelected,device0,channel0,new string[0]);
            }
        }

然后在一些控制通信协议的代码之后:

static void DeviceResponse(ANT_Response response)
        {
            // some code to control the response 
            // and the a call to Update my sensor data
            SensorUpdate(response.getDataPayload());
          
        }

如何在此处放置一些更新 AthleteSelected 或 ANTSensorSelected 上的 HBrate 或 HBcount 的代码

解决方法

正如蒂姆·施梅尔特 (Tim Schmelter) 已经评论过的那样:所给的信息对于找出您失败的时间点没有多大用处。

但是尝试以更一般的方式回答您的问题: 从静态方法更新类(但您指的是类实例)属性,需要将类实例作为该方法的参数。

public class MyInstanceClass
{
    public string MyValue { get; set; }
}

public static class MyStaticClass
{
    public static void DoSomething(MyInstanceClass instance)
    {
         instance.MyValue = "New Value";
    }
}
,

我不使用 ANTSensorSelected 因为我想直接更新 AthleteSelected 但如果需要我最终可以添加。 对于通信,我使用来自 Garmin 的 ANT_Managed_Library 此处的 ANT_Response 类:

public class ANT_Response
    {
        /// <summary>
        /// The object that created this response (ie: The corresponding ANTChannel or ANTDevice instance).
        /// </summary>
        public object sender;

        /// <summary>
        /// The channel parameter received in the message. Note: For some messages this is not applicable.
        /// </summary>
        public byte antChannel;

        /// <summary>
        /// The time the message was received.
        /// </summary>
        public DateTime timeReceived;

        /// <summary>
        /// The MessageID of the response
        /// </summary>
        public byte responseID;

        /// <summary>
        /// The raw contents of the response message
        /// </summary>
        public byte[] messageContents;

        internal ANT_Response(object sender,byte antChannel,DateTime timeReceived,byte IDcode,byte[] messageContents)
        {
            this.sender = sender;
            this.antChannel = antChannel;
            this.timeReceived = timeReceived;
            this.responseID = IDcode;
            this.messageContents = messageContents;
        }

很长的代码让我尝试添加最重要的部分:

channel0.channelResponse += new dChannelResponseHandler(ChannelResponse); // Add channel response function to receive channel event messages

/// Delegate for Channel Response Event
    /// </summary>
    /// <param name="response">Message details received from device</param>
    public delegate void dChannelResponseHandler(byte antChannel,ANT_Response response);

这里是ChannelResponse

static void ChannelResponse(byte antChannel,ANT_Response response)
        {
            
            try
            {
                switch ((ANT_ReferenceLibrary.ANTMessageID)response.responseID)
                {
                    case ANT_ReferenceLibrary.ANTMessageID.RESPONSE_EVENT_0x40:
 //some extra code with more cases
  SensorUpdate(response.getDataPayload());

这里是getDataPayLoad

/// Returns the 8-byte data payload of an ANT message. Throws an exception if this is not a received message.
        /// </summary>
        /// <returns></returns>
      public byte[] getDataPayload()
        {
            if (messageContents.Length == 9
                && (responseID == (byte)ANT_ReferenceLibrary.ANTMessageID.BROADCAST_DATA_0x4E
                     || responseID == (byte)ANT_ReferenceLibrary.ANTMessageID.ACKNOWLEDGED_DATA_0x4F
                     || responseID == (byte)ANT_ReferenceLibrary.ANTMessageID.BURST_DATA_0x50
               ))
                return messageContents.Skip(1).ToArray();   //Just skip the channel byte and return the payload
            else
                return splitExtMessage(extMsgParts.DataPayload);    // Extended message
        }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...