在 C# 中将双精度值从一个 class 发送到另一个

Sending double value from one class to another in C#

我想知道如何将双精度值从一个 class 发送到另一个。我正在使用 Kinect,当双 "distance" 在我的 public class main 中计算时,我的手已关闭,我希望将该信息发送给我的另一个 public class 监控。由于我是 C# 编程的初学者,因此非常感谢显示的示例。

private void DrawHand(HandState handState, Point handPosition, DrawingContext drawingContext)
    {
        switch (handState)
        {
            case HandState.Closed:
                drawingContext.DrawEllipse(this.handClosedBrush, null, handPosition, HandSize, HandSize);
                // Distance calculation
                foreach (var body in bodies)
                {
                    // Get the positions
                    pRH = body.Joints[JointType.HandRight];
                    pLH = body.Joints[JointType.HandLeft]; // pLH gets to be "home" position in the meanwhile
                    //Some maths
                    double sqDistance = Math.Pow(pRH.Position.X - pLH.Position.X, 2) +
                        Math.Pow(pRH.Position.Y - pLH.Position.Y, 2) +
                        Math.Pow(pRH.Position.Z - pLH.Position.Z, 2);


                    // This is the information I want to send to public class Monitor
                    double distance = Math.Sqrt(sqDistance);

                }
                break;
        }
    }

我不确定你想达到什么目的,但是你可以 return 值

return value;

这是由

的调用class收到的
double temp = method();

如果您尝试将值加倍发送到 class,那么您应该使用参数。

例如:-

Monitor monitor1 = new Monitor(distance);

或通过

Monitor monitor1 = new Monitor();
monitor1.setDistance(distance);

我不确定我理解你的问题是不是你想要的

 public class Monitor
        {      
public  Monitor(double distance)//Constructor with double as parameter 
            {

            }
        }

当你实例化这个class

Monitor mo=new Monitor(distance);

您可以通过多种方式将您的距离变量发送到监视器class

1. Constructor

2. Properties

3. Method

 private void DrawHand(HandState handState, Point handPosition, DrawingContext drawingContext)
    {
        switch (handState)
        {
            case HandState.Closed:
                drawingContext.DrawEllipse(this.handClosedBrush, null, handPosition, HandSize, HandSize);
                // Distance calculation
                foreach (var body in bodies)
                {
                    // Get the positions
                    pRH = body.Joints[JointType.HandRight];
                    pLH = body.Joints[JointType.HandLeft]; // pLH gets to be "home" position in the meanwhile
                    //Some maths
                    double sqDistance = Math.Pow(pRH.Position.X - pLH.Position.X, 2) +
                        Math.Pow(pRH.Position.Y - pLH.Position.Y, 2) +
                        Math.Pow(pRH.Position.Z - pLH.Position.Z, 2);


                    // This is the information I want to send to public class Monitor
                    double distance = Math.Sqrt(sqDistance);
                    // Create object of Monitor class and pass the distance 
                    Moniotr obj = new Monitor(distance);
                   // using Method 
                    Moniotr obj = new Monitor();
                    obj.setDistance(distance);
                  // using public property
                  Moniotr obj = new Monitor();
                  obj.Distance = distance;
                }
                break;
        }
    }

这是示例监视器Class使用构造方法

public class Monitor 
{
   public Monitor(double distance)
   {
   }
}

这里是示例监视器Class采用方法方法

public class Monitor 
    {
       public void setDistance(double distance)
       {

       }
    }

这是使用 属性 方法

的示例监视器 class
public class Monitor 
{
  private double _distance;
  public Distance
  {
    get{return _distance;}
    set{_distance=value;}
  }
}