Values changing in struct

Hello experts,

I need an assistance, i am learing C# at the moment and i was wondering what i am doing wrong. Below you will find my code and in it i am creating a stct and 2 simple functions that do some calculations.

Now inside of a function CalcMidPoint i am changing Point b value ... if i check value b inside CalcMidPoint its changed and works as it should but "outside" of said function b is unchenged. Values are at default (Point b = new Point() { x = 10, y = 5, z = 1 };)

Can you please tell me what am i doing wrong?


 class Program
    {

        static void Main(string[] args)
        {

            Program program = new Program();

            program.ArangeForMove();

                    
            Console.ReadLine();
        }

             
        public void ArangeForMove()
        {
            Point a = new Point() { x = 10000, y = 10000, z = 20000 };  
            Point b = new Point() { x = 10, y = 5, z = 1 };          
            Point c = new Point() { x = 10, y = 5, z = 1 };          

            while(a.x != c.x && a.y != c.y && a.z != c.z)
            {
                

                if (CalcMidPoint(a, b, c))
                {
                    
                    //set mid coordinate as starting point coordinate! 
                    a.x = b.x;
                    a.y = b.y;
                    a.z = b.z;

                    //set static end point to point b
                    b.x = c.x;
                    b.y = c.y;
                    b.z = c.z;

                    
                }
                
            }
          
        }
        
        public static bool CalcMidPoint(Point a, Point b, Point c)
        {
            float distance = (float)Math.Sqrt(Math.Pow(b.x - a.x, 2) + Math.Pow(b.y - a.y, 2) + Math.Pow(b.z - a.z, 2));
            if (distance < 100)
            {
                
                Console.WriteLine("Final distance is: " + distance + " ... on coordinates: X:" + b.x + "  Y:" + b.y + "  Z:" + b.z + "\r");
                return true;
            }
            else
            {
                b.x = (a.x + b.x) / 2;
                b.y = (a.y + b.y) / 2;
                b.z = (a.z + b.z) / 2;

                return CalcMidPoint(a,b,c);
            }
        }

        //structure
        public struct Point
        {
            public float x;
            public float y;
            public float z;
        }

    }

Open in new window

TagyourareitAsked:
Who is Participating?
 
Peter HutchisonSenior Network Systems SpecialistCommented:
Try changing the following line, so parameters are passed by reference rather than value:

public static bool CalcMidPoint(ref Point a, ref Point b,ref Point c)
        {
...
}
0
 
Peter HutchisonSenior Network Systems SpecialistCommented:
You need to declare your variables for Point a,b, and c in the main subroutine rather than the functions which will make them available to all functions. Alternatively, you can use the 'static' keyword in the declaration to make the values 'stick' between calls.
0
 
TagyourareitAuthor Commented:
Could you please provide me an example for both solutions?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Peter HutchisonSenior Network Systems SpecialistCommented:
Solution 1:

class Program
    {
           // Variables available for class but not outside the class
            private Point a = new Point() { x = 10000, y = 10000, z = 20000 };  
            private Point b = new Point() { x = 10, y = 5, z = 1 };          
            private Point c = new Point() { x = 10, y = 5, z = 1 };  

        static void Main(string[] args)
        {
     
 
            Program program = new Program();
            program.ArangeForMove();                  
            Console.ReadLine();
        }
 ...
Solution 2:

 public void ArangeForMove()
        {
            static Point a = new Point() { x = 10000, y = 10000, z = 20000 };  
            static Point b = new Point() { x = 10, y = 5, z = 1 };          
            static Point c = new Point() { x = 10, y = 5, z = 1 };          
...
0
 
TagyourareitAuthor Commented:
Hello,

i have already tried that and solution 1 doesn't work. Returns same results as my original example.

Solution 2 doesn't work fails with error ... The modifier 'static' is not valid for this item.
0
 
TagyourareitAuthor Commented:
An awesome help and great answer! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.