Indexed properties


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: Indexed properties

  1. #1
    Konstantin Kipa Guest

    Indexed properties

    Hi

    Does anyone know how to create an indexed property?

    For example, I want to have a class MyClass and be able to access one of the
    properties as

    MyClass b = new MyClass();
    MyClass.myarray[1] = 345;

    is is possible. I read through documentation about Indexers but this is not
    what I want.

    Konstantin






  2. #2
    Roberto Martinez-Brunet Guest

    Re: Indexed properties

    Just expose a public array. Or an array property.

    Roberto

    "Konstantin Kipa" <konstantin@metaobjects.net> wrote in message
    news:3a38dd52@news.devx.com...
    > Hi
    >
    > Does anyone know how to create an indexed property?
    >
    > For example, I want to have a class MyClass and be able to access one of

    the
    > properties as
    >
    > MyClass b = new MyClass();
    > MyClass.myarray[1] = 345;
    >
    > is is possible. I read through documentation about Indexers but this is

    not
    > what I want.
    >
    > Konstantin
    >
    >
    >
    >
    >




  3. #3
    David Bayley Guest

    Re: Indexed properties

    Konstantin,

    You can declare the property as an array, System.Collections.ArrayList, or a
    custom collection derived from ArrayList, as in...

    using System.Collections;

    public class MyClass {

    private int[] myArray = new int[100];
    private ArrayList myArrayList = new ArrayList();
    private Collection myCollection = new Collection;

    public int[] MyArray {
    get { return myArray; }
    }

    public ArrayList MyArrayList {
    get { return myArrayList; }
    }

    public Collection MyCollection{
    get { return myCollection; }
    }

    public class Collection : ArrayList {
    public override object this[int index] {
    get { return base.Item[index]; }
    set { base.Item[index] = value; }
    }
    }

    }

    // usage...
    MyClass b = new MyClass();
    int n;

    b.MyArray[50] = 345;
    n = b.MyArray[50];

    b.MyArrayList.Add(10);
    b.MyArrayList.Add(20);
    b.MyArrayList[1] = 30;
    n = (int)b.MyArrayList[1];

    b.MyCollection.Add(10);
    b.MyCollection.Add(20);
    b.MyCollection[1] = 30;
    n = (int)b.MyCollection[1];


    --
    David.

    Konstantin Kipa <konstantin@metaobjects.net> wrote in message
    news:3a38dd52@news.devx.com...
    > Hi
    >
    > Does anyone know how to create an indexed property?
    >
    > For example, I want to have a class MyClass and be able to access one of

    the
    > properties as
    >
    > MyClass b = new MyClass();
    > MyClass.myarray[1] = 345;
    >
    > is is possible. I read through documentation about Indexers but this is

    not
    > what I want.
    >
    > Konstantin
    >
    >
    >
    >
    >




  4. #4
    Jeff Peil Guest

    Re: Indexed properties

    Konstantin,

    C# does not support indexed properties.

    It does support an indexer for a class, but it sounds like you are already
    familiar with the subtle difference.

    If the type of the property named "myarray" supported an indexer, for
    example if it were an ArrayList, then you would be able to index on it.

    "Konstantin Kipa" <konstantin@metaobjects.net> wrote in message
    news:3a38dd52@news.devx.com...
    > Hi
    >
    > Does anyone know how to create an indexed property?
    >
    > For example, I want to have a class MyClass and be able to access one of

    the
    > properties as
    >
    > MyClass b = new MyClass();
    > MyClass.myarray[1] = 345;
    >
    > is is possible. I read through documentation about Indexers but this is

    not
    > what I want.





  5. #5
    Jonathan Allen Guest

    Re: Indexed properties

    > C# does not support indexed properties.

    Well that sucks. I am (almost) surprised that something so simple to do in
    VB.Net is impossible in C#.

    --
    Jonathan Allen


    "Jeff Peil" <jpeil@bigfoot.com> wrote in message
    news:3a390a84@news.devx.com...
    > Konstantin,
    >
    > C# does not support indexed properties.
    >
    > It does support an indexer for a class, but it sounds like you are already
    > familiar with the subtle difference.
    >
    > If the type of the property named "myarray" supported an indexer, for
    > example if it were an ArrayList, then you would be able to index on it.
    >
    > "Konstantin Kipa" <konstantin@metaobjects.net> wrote in message
    > news:3a38dd52@news.devx.com...
    > > Hi
    > >
    > > Does anyone know how to create an indexed property?
    > >
    > > For example, I want to have a class MyClass and be able to access one of

    > the
    > > properties as
    > >
    > > MyClass b = new MyClass();
    > > MyClass.myarray[1] = 345;
    > >
    > > is is possible. I read through documentation about Indexers but this is

    > not
    > > what I want.

    >
    >
    >




  6. #6
    Jeff Peil Guest

    Re: Indexed properties


    "Jonathan Allen" <greywolfcs@bigfoot.com> wrote in message
    news:3a393d81@news.devx.com...
    > > C# does not support indexed properties.

    >
    > Well that sucks. I am (almost) surprised that something so simple to do in
    > VB.Net is impossible in C#.


    Not like it isn't possible to create a synthetic equivalent as follows
    (though I wouldn't suggest doing so, imo it's far better to stick with a
    language's common conventions.)

    public class Foo
    {
    public class <PropName>IndexImpl
    {
    Foo outer;
    internal IndexedProp(Foo container) { outer = container; }
    public <Type> this[<indexing params>] {
    get { return outer.Get<PropName>(<indexing params>);}
    set { outer.Set<PropName>(<indexing params>, value);}
    }
    public <PropName>IndexImpl <PropName> { get { return new
    <PropName>IndexImpl(this); } }
    private <Type> Get<PropName>(<indexing params>) {/*...*/}
    private void Set<PropName>(<indexing params>, <Type> value) {/*...*/}
    }



  7. #7
    Jonathan Allen Guest

    Re: Indexed properties

    >imo it's far better to stick with a language's common conventions

    I agree.

    When it comes down to messy work arounds or complaining until the language
    supports it, I prefer the later.

    --
    Jonathan Allen


    "Jeff Peil" <jpeil@bigfoot.com> wrote in message
    news:3a394619$1@news.devx.com...
    >
    > "Jonathan Allen" <greywolfcs@bigfoot.com> wrote in message
    > news:3a393d81@news.devx.com...
    > > > C# does not support indexed properties.

    > >
    > > Well that sucks. I am (almost) surprised that something so simple to do

    in
    > > VB.Net is impossible in C#.

    >
    > Not like it isn't possible to create a synthetic equivalent as follows
    > (though I wouldn't suggest doing so, imo it's far better to stick with a
    > language's common conventions.)
    >
    > public class Foo
    > {
    > public class <PropName>IndexImpl
    > {
    > Foo outer;
    > internal IndexedProp(Foo container) { outer = container; }
    > public <Type> this[<indexing params>] {
    > get { return outer.Get<PropName>(<indexing params>);}
    > set { outer.Set<PropName>(<indexing params>, value);}
    > }
    > public <PropName>IndexImpl <PropName> { get { return new
    > <PropName>IndexImpl(this); } }
    > private <Type> Get<PropName>(<indexing params>) {/*...*/}
    > private void Set<PropName>(<indexing params>, <Type> value) {/*...*/}
    > }
    >
    >




  8. #8
    Konstantin Kipa Guest

    Re: Indexed properties

    Hi

    The interesting thing is, what is the standard pattern to do this? For
    example, I want to create read-only indexed property and do some
    calculations on get {} method. How can I do this?

    The pattern was - I had a private variable (array or collection) and access
    this variable from my read-only indexed property.

    Any ideas?

    Thanks in advance

    Konstantin Kipa



  9. #9
    Jeff Peil Guest

    Re: Indexed properties

    The standard pattern in C# is to use a readonly indexer.

    "Konstantin Kipa" <konstantin@metaobjects.net> wrote in message
    news:3a39e24e@news.devx.com...
    > Hi
    >
    > The interesting thing is, what is the standard pattern to do this? For
    > example, I want to create read-only indexed property and do some
    > calculations on get {} method. How can I do this?
    >
    > The pattern was - I had a private variable (array or collection) and

    access
    > this variable from my read-only indexed property.
    >
    > Any ideas?
    >
    > Thanks in advance
    >
    > Konstantin Kipa
    >
    >




  10. #10
    Konstantin Kipa Guest

    Re: Indexed properties

    > The standard pattern in C# is to use a readonly indexer.

    Right. How can I acces from indexer to a private variable in a class

    Say, we have a class

    public class Foo
    {
    private ArrayList arrayList;

    // this property has to be an indexer as you said
    public MyIndexer readonlyproperty
    {
    get;
    }
    }

    Ok. Indexer would be a different class and should have an acces to arrayList
    private variable in the class Foo. How is it possible? If we talking about
    some variable inside Indexer class itself, class Foo has to have access to
    this variable. BUT that means, I shoud give this variable higher level of
    visibility rather than private. Which VERY bad for safety.

    ?!?

    >
    > "Konstantin Kipa" <konstantin@metaobjects.net> wrote in message
    > news:3a39e24e@news.devx.com...
    > > Hi
    > >
    > > The interesting thing is, what is the standard pattern to do this? For
    > > example, I want to create read-only indexed property and do some
    > > calculations on get {} method. How can I do this?
    > >
    > > The pattern was - I had a private variable (array or collection) and

    > access
    > > this variable from my read-only indexed property.
    > >
    > > Any ideas?
    > >
    > > Thanks in advance
    > >
    > > Konstantin Kipa
    > >
    > >

    >
    >




  11. #11
    David Bayley Guest

    Re: Indexed properties

    Konstantin,

    Either, use up the one and only "indexer" available on Foo...

    public class Foo {
    private ArrayList arrayList;
    public int this[int index] {
    get { return (int)arrayList[index]; }
    }
    }
    // use with "i = foo[5];"

    Or write you're own nested class, and pass the arrayList to the
    constructor...

    public class Foo {
    private ArrayList arrayList = new ArrayList();
    private Indexer myIndexer = new Indexer(arrayList);

    public Indexer MyIndexer {
    get { return myIndexer; }
    }

    public class Indexer {
    private ArrayList arrayList;
    internal Indexer(ArrayList arrayList) {
    this.arrayList = arrayList;
    }
    public int this[int index] {
    get { return (int)arrayList[index]; }
    }
    }

    }
    // use with "i = foo.MyIndexer[5];"


    Konstantin Kipa <konstantin@metaobjects.net> wrote in message
    news:3a3a005e$1@news.devx.com...
    > > The standard pattern in C# is to use a readonly indexer.

    >
    > Right. How can I acces from indexer to a private variable in a class
    >
    > Say, we have a class
    >
    > public class Foo
    > {
    > private ArrayList arrayList;
    >
    > // this property has to be an indexer as you said
    > public MyIndexer readonlyproperty
    > {
    > get;
    > }
    > }
    >
    > Ok. Indexer would be a different class and should have an acces to

    arrayList
    > private variable in the class Foo. How is it possible? If we talking about
    > some variable inside Indexer class itself, class Foo has to have access to
    > this variable. BUT that means, I shoud give this variable higher level of
    > visibility rather than private. Which VERY bad for safety.
    >
    > ?!?
    >
    > >
    > > "Konstantin Kipa" <konstantin@metaobjects.net> wrote in message
    > > news:3a39e24e@news.devx.com...
    > > > Hi
    > > >
    > > > The interesting thing is, what is the standard pattern to do this? For
    > > > example, I want to create read-only indexed property and do some
    > > > calculations on get {} method. How can I do this?
    > > >
    > > > The pattern was - I had a private variable (array or collection) and

    > > access
    > > > this variable from my read-only indexed property.
    > > >
    > > > Any ideas?
    > > >
    > > > Thanks in advance
    > > >
    > > > Konstantin Kipa
    > > >
    > > >

    > >
    > >

    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center