Search code examples
c#classdoublereturn-valuekinect

Sending double value from one class to another in C#


I wonder how I can send a double value from one class to another. I'm working with a Kinect and when the double "distance" is calculated in my public class main while I have my hand closed I wish to send that information to my other public class Monitor. Shown examples would be highly appreciated as I'm beginner in C# programming.

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;
        }
    }

Solution

  • You can send your distance variable to monitor class in many ways

    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;
            }
        }
    

    Here is Sample Monitor Class With Constructor approach

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

    Here is Sample Monitor Class With Method approach

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

    Here is Sample Monitor class with Property approach

    public class Monitor 
    {
      private double _distance;
      public Distance
      {
        get{return _distance;}
        set{_distance=value;}
      }
    }