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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TagyourareitAuthor Commented:
An awesome help and great answer! :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.