我想知道如何将一个double值从一个类发送到另一个类。我正在使用Kinect,当我的公共课主要计算双倍的“距离”时,我会关闭手,我希望将这些信息发送给我的其他公共类Monitor。由于我是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;
}
}
答案 0 :(得分:0)
我不确定您要实现的目标,但是您可以通过
返回值return value;
调用类由
接收double temp = method();
如果您尝试将值double发送到某个类,则应使用参数。
例如: -
Monitor monitor1 = new Monitor(distance);
或
Monitor monitor1 = new Monitor();
monitor1.setDistance(distance);
答案 1 :(得分:0)
我不确定我明白你的问题是你想要的
public class Monitor
{
public Monitor(double distance)//Constructor with double as parameter
{
}
}
实例化此类时
Monitor mo=new Monitor(distance);
答案 2 :(得分:0)
您可以通过多种方式将距离变量发送到监控班级
<强> 1。构造强>
<强> 2。属性强>
第3。方法强>
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;
}
}
以下是带有构造函数方法的Sample Monitor类
public class Monitor
{
public Monitor(double distance)
{
}
}
以下是Sample Monitor Class With Method方法
public class Monitor
{
public void setDistance(double distance)
{
}
}
以下是带有Property方法的Sample Monitor类
public class Monitor
{
private double _distance;
public Distance
{
get{return _distance;}
set{_distance=value;}
}
}