Link to home
Start Free TrialLog in
Avatar of Tagyourareit
Tagyourareit

asked on

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

Avatar of Peter Hutchison
Peter Hutchison
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of Tagyourareit
Tagyourareit

ASKER

Could you please provide me an example for both solutions?
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 };          
...
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.
ASKER CERTIFIED SOLUTION
Avatar of Peter Hutchison
Peter Hutchison
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
An awesome help and great answer! :)