# 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();

}

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

}
``````
###### Who is Participating?

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.

Senior 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
Author Commented:
Could you please provide me an example for both solutions?
0
Senior 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();
}
...
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
Author 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
Senior 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

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

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