C
C#2y ago
avishy

Hi im not sure that my code is working as it should(class in c sharp)

providing code here👇(When i add +1 or +10 to the GetX or GetY isn't it supossed to change?)
35 Replies
avishy
avishy2y ago
csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Point
{
internal class Program
{
static void Main(string[] args)
{

Point point1 = new Point();
Point point2 = new Point();
point1.SetX(7);
point1.SetY(10);
point2.SetX(3);
point2.SetY(8);

Console.WriteLine(point1.ToString());
Console.WriteLine(point2.ToString());
double dist = point1.distanceToPoint(point2);
Console.WriteLine(dist);
double distT = point2.distanceToPoint(point1);
Console.WriteLine(distT);

var point3 = point1.MidPoint(point2);
Console.WriteLine(point3.ToString());

Point point4 = point1;
Console.WriteLine(point4.ToString());
double point1X = (point1.GetX() + 1);
double point1Y = (point1.GetY() + 1);
double point2X = (point2.GetX() + 10);
double point2Y = (point2.GetY() + 10);
double dists = point1.distanceToPoint(point2);
Console.WriteLine(dists);
//המרחק לא השתנה
Console.WriteLine(point4.ToString());
Console.WriteLine(point1.ToString());





}
}
}
csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Point
{
internal class Program
{
static void Main(string[] args)
{

Point point1 = new Point();
Point point2 = new Point();
point1.SetX(7);
point1.SetY(10);
point2.SetX(3);
point2.SetY(8);

Console.WriteLine(point1.ToString());
Console.WriteLine(point2.ToString());
double dist = point1.distanceToPoint(point2);
Console.WriteLine(dist);
double distT = point2.distanceToPoint(point1);
Console.WriteLine(distT);

var point3 = point1.MidPoint(point2);
Console.WriteLine(point3.ToString());

Point point4 = point1;
Console.WriteLine(point4.ToString());
double point1X = (point1.GetX() + 1);
double point1Y = (point1.GetY() + 1);
double point2X = (point2.GetX() + 10);
double point2Y = (point2.GetY() + 10);
double dists = point1.distanceToPoint(point2);
Console.WriteLine(dists);
//המרחק לא השתנה
Console.WriteLine(point4.ToString());
Console.WriteLine(point1.ToString());





}
}
}
When i add +1 or +10 to the GetX or GetY isn't it supossed to change?
Buddy
Buddy2y ago
$details
MODiX
MODiX2y ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
avishy
avishy2y ago
i did
Buddy
Buddy2y ago
Actually, it doesn't mention title. But either way, please name your title well, instead of naming it like it doesn't work and I don't know why $code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
avishy
avishy2y ago
i wrote the code
Buddy
Buddy2y ago
You failed the highlighting
avishy
avishy2y ago
i dont know how else to name it
Buddy
Buddy2y ago
make sure you write a new line when you write cs
avishy
avishy2y ago
ok
Buddy
Buddy2y ago
like this
avishy
avishy2y ago
@Buddy delete ur msgs so ppl wonth ave to serch for the code
Buddy
Buddy2y ago
What is SetX? and SetY
avishy
avishy2y ago
its up on the code
Buddy
Buddy2y ago
and the other functions It is not, you have not posted that.
avishy
avishy2y ago
my code is too long
using System;


namespace Point
{
class Point
{
private double x;
private double y;

public Point()
{
}
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
public Point(Point another)
{
this.x = another.GetX();
this.y = another.GetY();
}
public double distanceToPoint(Point another)
{
double dis = Math.Sqrt(Math.Pow(this.x - another.GetX(), 2) + Math.Pow(this.y - another.GetY(), 2));
return dis;
}
public Point MidPoint(Point another)
{
double pthreeX = (this.x + another.GetX()) / 2;
double pthreeY = (this.y + another.GetY()) / 2;
return new Point(pthreeX, pthreeY);
}
public static void PrintDistance(Point pointa, Point pointb)
{
double distance = pointa.distanceToPoint(pointb);
Console.WriteLine("The distance between Point A and Point B is: " + distance);
}
public static bool IsEqualTriangle(Point point1, Point point2, Point point3)
{
double distance1 = point1.distanceToPoint(point2);
double distance2 = point2.distanceToPoint(point3);
double distance3 = point3.distanceToPoint(point1);

if (distance1 == distance2 && distance2 == distance3)
{
return true;
}
return false;
}



public override string ToString()
{
return "(" + this.x + "," + this.y + ")";

}
public double GetX()
{
return this.x;
}
public double GetY()
{
return this.y;
}
public void SetX(double x)
{
this.x = x;
}
public void SetY(double y)
{
this.y = y;
}
}
}
using System;


namespace Point
{
class Point
{
private double x;
private double y;

public Point()
{
}
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
public Point(Point another)
{
this.x = another.GetX();
this.y = another.GetY();
}
public double distanceToPoint(Point another)
{
double dis = Math.Sqrt(Math.Pow(this.x - another.GetX(), 2) + Math.Pow(this.y - another.GetY(), 2));
return dis;
}
public Point MidPoint(Point another)
{
double pthreeX = (this.x + another.GetX()) / 2;
double pthreeY = (this.y + another.GetY()) / 2;
return new Point(pthreeX, pthreeY);
}
public static void PrintDistance(Point pointa, Point pointb)
{
double distance = pointa.distanceToPoint(pointb);
Console.WriteLine("The distance between Point A and Point B is: " + distance);
}
public static bool IsEqualTriangle(Point point1, Point point2, Point point3)
{
double distance1 = point1.distanceToPoint(point2);
double distance2 = point2.distanceToPoint(point3);
double distance3 = point3.distanceToPoint(point1);

if (distance1 == distance2 && distance2 == distance3)
{
return true;
}
return false;
}



public override string ToString()
{
return "(" + this.x + "," + this.y + ")";

}
public double GetX()
{
return this.x;
}
public double GetY()
{
return this.y;
}
public void SetX(double x)
{
this.x = x;
}
public void SetY(double y)
{
this.y = y;
}
}
}
here
Buddy
Buddy2y ago
you can use this website if it's too long $paste
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
avishy
avishy2y ago
its ok it works but ty but i need help with my question
Buddy
Buddy2y ago
You are not printing the variables that you have set
avishy
avishy2y ago
ToString is returning them
Buddy
Buddy2y ago
double point1X = (point1.GetX() + 1);
double point1Y = (point1.GetY() + 1);
double point2X = (point2.GetX() + 10);
double point2Y = (point2.GetY() + 10);
double point1X = (point1.GetX() + 1);
double point1Y = (point1.GetY() + 1);
double point2X = (point2.GetX() + 10);
double point2Y = (point2.GetY() + 10);
You must use the newly declared variables it does not set the values of point1, point2
avishy
avishy2y ago
how can i change them then without changing the set i mean
Buddy
Buddy2y ago
You can't, unless you add that to your Point class or make x / y public
avishy
avishy2y ago
so i cant change it in main without changing the set?
Buddy
Buddy2y ago
GetX / GetY returns a copy of x, it is not the reference of x. $refvsvalue
avishy
avishy2y ago
but then i add to it the +1 it doesnt affect the actual point?
Buddy
Buddy2y ago
So based on your current code, the only way to add to X is:
point1.SetX(point1.GetX() + 1);
point1.SetX(point1.GetX() + 1);
avishy
avishy2y ago
ohh okay ty how do i call PrintDistance in main?
Buddy
Buddy2y ago
Point.PrintDistance as it is a static method $static
MODiX
MODiX2y ago
In C#, static allows you to have members (classes, methods, etc) that are not tied to any particular instance and are therefore always, globally, accessible. When applying static members, take the following considerations: • If there are to be multiple instances of a class, do not use static • If you need to track state, do not use static • If you are going to have multi threaded workflows, do not use static unless you're aware of the caveats static is best used for stateless methods, extension methods/classes and in areas where you understand the pros/cons. Read more here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static
avishy
avishy2y ago
thank you but if i try that with IsEqualTrinagnle it doesn't work, is it not a static method? does bool count as static?
Buddy
Buddy2y ago
static keyword count as static