generating public int this[int index] {}

  • Thread starter Thread starter 2G
  • Start date Start date
2

2G

Hi,

Could anyone give me a hint how to generate this using codedom, I know how
to generate properties and methods .... but this I can't seem to find :s.

public int this[int index] {

get {

}

}



Thanks
 
2G said:
Could anyone give me a hint how to generate this using codedom, I know how
to generate properties and methods .... but this I can't seem to find :s.

public int this[int index] {

get {

}

}

See CodeMemberProperty.Parameters.
 
Hi Jon, could you be a bit more specific, I don't see how I should do this
using the parameters.


Jon Skeet said:
2G said:
Could anyone give me a hint how to generate this using codedom, I know how
to generate properties and methods .... but this I can't seem to find :s.

public int this[int index] {

get {

}

}

See CodeMemberProperty.Parameters.
 
2G said:
Hi Jon, could you be a bit more specific, I don't see how I should do this
using the parameters.

You basically write it exactly as if you were writing a method which
took parameters - so in your sample case, you'd treat it as if it were
a method which took an int parameter (for the getter - the setter takes
an additional parameter which is the value, of course, and I suspect
you don't need to specify that).
 
Ok, I added the parameter as I would with a method but this still doesn't
seem to do the trick.
The only thing I get from it is (unless I supply the .Name, then the
property is named but still has no indexer):

public ProduktenRow

{

get

{

return ((ProduktenRow)(this.Rows[index]));

}

}



CodeMemberProperty cmp2 = new CodeMemberProperty();

cmp2.Type = new CodeTypeReference(table + "Row");

cmp2.Attributes = MemberAttributes.Public | MemberAttributes.Final;

cmp2.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int),
"index"));

cmp2.HasGet = true;

cmp2.HasSet = false;

cmp2.GetStatements.Add(new CodeMethodReturnStatement(

new CodeCastExpression(table + "Row",

new CodeArrayIndexerExpression(new CodeFieldReferenceExpression(

new CodeThisReferenceExpression(), "Rows"), new
CodeArgumentReferenceExpression("index")))));



ps: thanks for the fast responses
 
2G said:
Ok, I added the parameter as I would with a method but this still doesn't
seem to do the trick.
The only thing I get from it is (unless I supply the .Name, then the
property is named but still has no indexer):

Have you tried naming the property as Item?
 
2G said:
Aah yes, thanks !! :), but I hate those "need to know things"

Well, to be fair it *is* specified in the docs for
CodeMemberProperty.Parameters:

<quote>
For any property that has the special name "Item" and one or more
parameters, it will declare an indexer property for the class. However,
not all languages support the declaration of indexers.
</quote>
 
hmmm, my eye must have missed that :s

Jon Skeet said:
Well, to be fair it *is* specified in the docs for
CodeMemberProperty.Parameters:

<quote>
For any property that has the special name "Item" and one or more
parameters, it will declare an indexer property for the class. However,
not all languages support the declaration of indexers.
</quote>
 
Back
Top