Overloading []

P

piotrek

Hi.
Iam creating Matrix class and i wanna access members of an object like:
matrixObject[i.j] = anyNumber;

How to overload [] operator?
PK
 
S

Soren S. Jorgensen

Hi,

Something like:

public class MyMatricObject
{
public object this[int x, int y]
{
get; set; // Fill
}

public object this[string x, string y]
{
get; set; // Fill
}
}

Kr. Soren
 
P

piotrek

Thank you, but how to set the member then?

When i can access member by []... then i need to set the value.
Now in my code it looks like:

public float this[int x, int y]

{

set

{

M[x, y] = 0;

}

get

{

return M[x, y];

}

}



How to replace 0 in set accesor, by the value after = sign?

PK
 
S

Soren S. Jorgensen

Hi,

Use the "value" keyword

public float this[int x, int y]
{
get
{
return M[x,y];
}
set
{
M[x,y] = value;
}
}

Kr. Soren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top