Property Indexer

H

hufaunder

I have found some examples on MSDN (http://msdn.microsoft.com/library/
default.asp?url=/library/en-us/csref/html/
vcwlkIndexedPropertiesTutorial.asp), Code Project, etc about property
indexers. In these examples the indexer uses a reference back to the
main class. Why is this done this way? Why not store a reference to
the list of values that are indexed by the indexer rather then a
reference to the whole main class? In other words, why do the define

private readonly MainClass1 m_subClassOwner;

rather then

private readonly List<SubClass> m_subClassList;

Bellow you find a sample code that implements both ways:


using System;
using System.Collections.Generic;
using System.Text;

namespace PropIndexTest
{
class Program
{
static void Main(string[] args)
{
String name1 = new MainClass1().SubClassValues[0].Name;
String name2 = new MainClass2().SubClassValues[0].Name;
}
}

public class SubClass
{
String m_name;
public SubClass(String name) { m_name = name; }
public String Name { get { return m_name; } }
}


//***** MSDN Way
public class MainClass1
{
private SubClassIndexer1 m_subClassIndexer;
protected internal List<SubClass> m_subClassList;

public MainClass1()
{
m_subClassList = new List<SubClass>();
m_subClassList.Add(new SubClass("Name 1")); //just add one
for testing.
m_subClassIndexer = new SubClassIndexer1(this);
}
public SubClassIndexer1 SubClassValues
{
get { return m_subClassIndexer; }
}
}

public class SubClassIndexer1
{
/*!!!!*/private readonly MainClass1 m_subClassOwner;

public SubClassIndexer1(MainClass1 seriesOwner)
{
m_subClassOwner = seriesOwner;
}


public SubClass this[Int32 index]
{
get {
return m_subClassOwner.m_subClassList[index];
}
}
}

//***** Alternative Way
public class MainClass2
{
private SubClassIndexer2 m_subClassIndexer;
protected internal List<SubClass> m_subClassList;

public MainClass2()
{
m_subClassList = new List<SubClass>();
m_subClassList.Add(new SubClass("Name2")); //just add one
for testing.
m_subClassIndexer = new SubClassIndexer2(m_subClassList);
}
public SubClassIndexer2 SubClassValues
{
get { return m_subClassIndexer; }
}
}

public class SubClassIndexer2
{
/*!!!!*/private readonly List<SubClass> m_subClassList;

public SubClassIndexer2(List<SubClass> subClassList)
{
m_subClassList = subClassList;
}


public SubClass this[Int32 index]
{
get
{
return m_subClassList[index];
}
}
}
}
 
J

Jesse McGrew

I have found some examples on MSDN (http://msdn.microsoft.com/library/
default.asp?url=/library/en-us/csref/html/
vcwlkIndexedPropertiesTutorial.asp), Code Project, etc about property
indexers. In these examples the indexer uses a reference back to the
main class. Why is this done this way? Why not store a reference to
the list of values that are indexed by the indexer rather then a
reference to the whole main class? In other words, why do the define

private readonly MainClass1 m_subClassOwner;

rather then

private readonly List<SubClass> m_subClassList;

One reason to do it that way is so you can raise an event in the main
class when an item is modified (if the indexer is writable).

Jesse
 

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

Top