so I created a class with two arrays (vector and vectorNorm) and wanted to include some setter and getter functions.
using System;
public class Vectors {
double[] vector = new double[2];
double length;
double[] vectorNorm = new double[2];
public double[] Vector {
private set {
Console.WriteLine("set called");
this.vector = value;
}
get {
Console.WriteLine("get called");
return this.vector;
}
}
public double[] VektorNorm {
get {
return vector;
}
set {
vector = value;
}
}
public double Length {
get {
return length;
}
set {
length = value;
}
}
public Vectors(double parVector1, double parVector2) {
Vector[0] = parVector1;
Vector[1] = parVector2;
}
public Vectors() {
;
}
public void calcLength() {
Length = (Vector[0]*Vector[0])+(Vector[1]*Vector[1]);
Console.WriteLine("Length " + Math.Sqrt(length));
Console.ReadLine();
}
}
My programm that executes looks like this
using System;
public class VectorsProg {
public static void Main() {
var vec1 = new Vectors(3, 4);
vec1.calcLength();
vec1.Vector[0] = 10;
Console.WriteLine(vec1.Vector[0]);
Console.ReadLine();
}
}
The output on console looks like this
get called
get called
get called
get called
get called
get called
Length 5
get called
get called
10
So the Console.WriteLine("set called") never gets executed but the value of vec1.vector[0] changes. I also included the "private set" that doesn't even allow other classes to use the set function but the value still changes.
Is my code wrong in some way? Any solutions?