Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Indexer Question

Indexer Question

Scheduled Pinned Locked Moved C#
tutorialdata-structuresquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Knechtges
    wrote on last edited by
    #1

    I have a class in which I have a property that returns an array of enums (which are declared in a class that is used by this class through a using statement). When I use this class, I access the property through the indexer. The enum and accessor look like: enum State { Flashing, Played, NotPlayed } class1.balls[i] = State.Played; Now, what I want to do is take action when the property is set through the indexer like in the example above. I want to know the value of the indexer (i) and what it is being set to. All the internet examples I see use a separate class for this, but that doesn't make sense to me. I can't see how to make the separate class access things within the original class I have the public property in. Thanks!

    L T L 3 Replies Last reply
    0
    • D David Knechtges

      I have a class in which I have a property that returns an array of enums (which are declared in a class that is used by this class through a using statement). When I use this class, I access the property through the indexer. The enum and accessor look like: enum State { Flashing, Played, NotPlayed } class1.balls[i] = State.Played; Now, what I want to do is take action when the property is set through the indexer like in the example above. I want to know the value of the indexer (i) and what it is being set to. All the internet examples I see use a separate class for this, but that doesn't make sense to me. I can't see how to make the separate class access things within the original class I have the public property in. Thanks!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The C# spec has this to say about indexers: Indexer declarations are similar to property declarations, with the main differences being that indexers are nameless (the “name” used in the declaration is this, since this is being indexed) and that indexers include indexing parameters. The indexing parameters are provided between square brackets. edit: page 60/553 of ECMA-334 4th edition (June 2006) Which means that you can not declare an indexer with a name, you can only use indexers to write expressions of the form instance[parameter list] To give it a name, you have to cheat - for example by having a field (with the wanted name, of course) of a type that has an indexer. Usually, that would be a field of an other type, which you created solely to be indexed. That type should, of course, have enough information to be indexed, but you could pass in the entire "parent" instance (edit: at the ctor, when you initialize that field).

      1 Reply Last reply
      0
      • D David Knechtges

        I have a class in which I have a property that returns an array of enums (which are declared in a class that is used by this class through a using statement). When I use this class, I access the property through the indexer. The enum and accessor look like: enum State { Flashing, Played, NotPlayed } class1.balls[i] = State.Played; Now, what I want to do is take action when the property is set through the indexer like in the example above. I want to know the value of the indexer (i) and what it is being set to. All the internet examples I see use a separate class for this, but that doesn't make sense to me. I can't see how to make the separate class access things within the original class I have the public property in. Thanks!

        T Offline
        T Offline
        T M Gray
        wrote on last edited by
        #3

        Arrays are way to simple a structure for what you are looking for. If you want events fired when items are added to the list, try inheriting from Collection<T> and implementing the events. e.g.[^]

        L 1 Reply Last reply
        0
        • T T M Gray

          Arrays are way to simple a structure for what you are looking for. If you want events fired when items are added to the list, try inheriting from Collection<T> and implementing the events. e.g.[^]

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Or he could use an indexer, as he suggested.. must simpler and faster that way

          1 Reply Last reply
          0
          • D David Knechtges

            I have a class in which I have a property that returns an array of enums (which are declared in a class that is used by this class through a using statement). When I use this class, I access the property through the indexer. The enum and accessor look like: enum State { Flashing, Played, NotPlayed } class1.balls[i] = State.Played; Now, what I want to do is take action when the property is set through the indexer like in the example above. I want to know the value of the indexer (i) and what it is being set to. All the internet examples I see use a separate class for this, but that doesn't make sense to me. I can't see how to make the separate class access things within the original class I have the public property in. Thanks!

            L Offline
            L Offline
            LookSharp
            wrote on last edited by
            #5

            You do need a separate class to do what you want to do, and it needs to expose an event that the class that uses it must handle in order to respond to changes in ball states. Here's an example (compiles under VS2008):

            namespace BallStateExample
            {
            enum State {Flashing, Played, NotPlayed}
            delegate void BallStateChanged(BallStateChangedEventArgs e);

            class BallStateChangedEventArgs
            	{
            	public int BallIndex { get; set; }
            	public State NewState { get; set; }
            	}
            
            	
            class Balls
            	{
            	private readonly State\[\] \_balls;
            	
            	public Balls(int numBalls)
            		{
            		\_balls = new State\[numBalls\];
            		}
            		
            	public event BallStateChanged BallStateChanged;
            
            	private void OnBallStateChanged(int index, State newState)
            		{
            		if (BallStateChanged != null)
            			BallStateChanged(new BallStateChangedEventArgs { BallIndex = index, NewState = newState });
            		}
            		
            	public State this\[int index\]
            		{
            		get { return \_balls\[index\]; }
            		set
            			{
            			\_balls\[index\] = value;
            			OnBallStateChanged(index, value);
            			}
            		}
            	}
            	
            	
            class BallUser
            	{
            	private const int NUM\_BALLS = 42;
            	
            	public BallUser()
            		{
            		Balls = new Balls(NUM\_BALLS);
            		Balls.BallStateChanged += Balls\_StateChanged;
            		}
            		
            	private void Balls\_StateChanged(BallStateChangedEventArgs e)
            		{
            		// Take action in response to a ball state change
            		}
            
            	// ...
            
            	// Auto-property (so no backing store need be declared)
            	// Private set accessor to prevent consumers of BallUser from
            	// assigning to the property (an action reserved for the constructor of BallUser).
            	public Balls Balls { get; private set; }
            
            	// ...
            	}
            }
            
            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups