I am using operator overloading to add two matrix of same size.
public const int size = 3;
int[,] matrix =new int[size,size];
Question1:
//What does the below function do?
public int this[int x, int y]
{
get { return matrix[x, y]; }
set { matrix[x, y] = value; }
}
Question2://why can i not use the name(matrix) to get the values from the array e.g?
public int matrix[int x, int y] //getting error on this
{
get { return matrix[x, y]; }
set { matrix[x, y] = value; }
}Question3:
If i comment the below code it gives me error
i.e "Cannot apply indexing with [] to an expression of type".........etc
//public int this[int x, int y]
// {
// get { return matrix[x, y]; }
// set { matrix[x, y] = value; }
// }
WAITING FOR REPLY?
THANK YOU?
StefanPosted Apr 23, 2009, 10:43 PM
Question 1: This is called a property. Properties can be used for many things. Mainly, though, they are used to allow an external class to access a private object. So if you have a private array that you want to use in another class, you make a property that allows you to access it. Another cool feature about properties is that they allow you to make a object read-only, just by removing the "setter". Properties have several other useful features, and I suggest you do a google search to find out more.
Question 2: You cannot make a property that has the same name as another object. So if you have a private array called matrix, you must name the property something else.
Question 3: I cannot answer this without seeing the whole error message or the rest of your code.
Hope this helps!