define a property which accepts a parameter

Hello!

What's the C# syntax to define a property which accepts parameters. The VB syntax whould be:

Public ReadOnly Property Item(ByVal index As Integer) As Object
Get
Return CType(Me.List.Item(index), MemberDataValidationError)
End Get
End Property




Answer this question

define a property which accepts a parameter

  • Siggy01

    Great! We all posted our answers at the same time!

  • Arczi999

    Make Strings a property that returns a collection or array of strings.



  • Padmaja T Chavali

    found it!

    public object this [int index]
    {
    get
    {
    return mCollection[index];
    }
    }



  • Thiago Guimarães

    well that's a nice answer but it is not helpful.

    I'll explain my need:

    I want a property, lets say its name is Strings and it gets an index as a parameter so I can get and set the item:

    MyClass c = new MyClass()

    String s = c.String(1)

    c.Item = "new value"

    can it be done

    Thanks.



  • John David

    You don't.
  • giftgirls

    You use the this keyword to define an indexer

    public object this[int index] { get { ... } }



  • Tridex

    As already Mattias write you, for that you need to use indexer. By default every collection type has indexer of type int. But if you need to access the class property, by providing property name, then create indexer of string type:

    [IndexerName("Item")]
    public object this[string name]
    {
    get
    {
    switch(name)
    {
    case "FirstName":
    return FirstName; //where FirstName is example of property name of the class on which indexer belongs

    ....... other properties

    default:
    throw new ArgumentException("Unknown property name");
    }
    }
    set
    {
    swich(name)
    {
    case "FirstName":
    FistName = (string) value;
    break;

    ..... other properties

    default:
    throw new ArgumentException("Unknown property name");
    }
    }
    }

    There are some isues to be considered, like you work with object type, so you need always to cast when you get the property or in the indexer when you set the property value;

  • Atiz

    Ori,

    I feel your pain. I too want similiar behaviour for COM interop reasons.

    You'd think it would be easy for them to add the following property type support in a future version of CSharp:

    private int somePrivateArray[];

    int SomeProperty(int index)

    {

    get {

    return somePrivateArray[index];

    }

    set {

    somePrivateArray[index] = value;

    }

    }

    Extra cudos if they added support for Property overloading:

    int SomeProperty(int index)

    {

    get ; set;

    }

    int SomeProperty(int index, int index2)

    {

    get ; set;

    }

    int SomeProperty

    {

    get ; set;

    }

    This solution is so much cleaner than any proposed solution I have seen using indexer overloading. This would also fix COM Interop issues when a type library uses a propget/propput member with a parameter. Currently a RCW doesn't appear to support those members properly.


  • gul_111111

    I understood that it is not supported and worked without it, but yes - it will be lovely to have a property with parameter(s). It is too late for me, but maybe my children will enjoy this feature Smile

  • Medes_

    that's nice

    but how do I give my property a name



  • Lionb

    For this purpose you can use C# indexers.

    class proba
    {
    int[] array = new int[]{1,2,3};

    public int this[int i /*other parametars like object o, ArrayList ar, ...*/]
    {
    get
    {
    if (i < array.Length)
    return this.arrayIdea;
    else
    throw new Exception("Wrong index!");
    }
    }
    }

    You can use this indexer like this:
    proba pr = new proba();
    int x = pr[1];

    Hope this helps.


  • vibhavari

    thanks, I know c# has string arrays and collections

    but I want a property - I want to write my own logic when a set operation is performed

    and again, I know I can use a set method

    but I want propertty mainly for COM interop, so the type library will use this feature and be similiar to an old type library I have



  • shivavrata

    Then make Strings a property which returns a class which has it's indexer defined as you like.

    Or make Strings a method which takes a parameter. The syntax is only going to be slightly different between the two.



  • thedo

    there is no difference with the property get.

    with a method I get exaclty what a peoperty with paramter gives

    there is a problem with the property set



  • define a property which accepts a parameter