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
 
Ad

Advertisements

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
 
J

Joanna Carter [TeamB]

"piotrek" <[email protected]> a écrit dans le message de [email protected]

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

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

Joanna
 
Ad

Advertisements

Ad

Advertisements


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

Similar Threads

overloading 4
Overloading 31
overload the * 3
Overloading or not? 2
OVERLOADING 1
lock and overloads 5
operator overloading 1
Operator Overloading? 4

Top