473,387 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Property Indexer

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<SubClassm_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<SubClassm_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<SubClassm_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<SubClassm_subClassList;

public SubClassIndexer2(List<SubClasssubClassList)
{
m_subClassList = subClassList;
}
public SubClass this[Int32 index]
{
get
{
return m_subClassList[index];
}
}
}
}

Feb 21 '07 #1
1 2391
On Feb 20, 9:24 pm, "hufaun...@yahoo.com" <hufaun...@yahoo.comwrote:
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<SubClassm_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

Feb 23 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dotnetjunky | last post by:
Hi all Is it correct that there's a link between indexer and Items property in C# ? If yes, please show me what is it ? thanks in advance. -- Dotnetjunky
4
by: Dotnetjunky | last post by:
Hi, I'm really get confused on this issue. Hope that someone can help me get clear after this post. Take the class System.Web.Caching.Cache for example. In the documentation, there's a...
2
by: Nad | last post by:
Good day, I have a question about the Page.Session property used in ASP.NET This property is defined as follows: public virtual HttpSessionState Session {get;} and HttpSessionState is a...
15
by: Prachi Dimble | last post by:
Hi, In vb.Net one can pass arguments to properties. How does one achieve it in c#? Given below is the vb.net code for passing arguments to property getters and setters.. Thanks, Prachi ...
5
by: rwoo_98 | last post by:
I am trying to pass a reference to a method --SomeMethod. This table resides in a dataset and I refer to the datatable in the dataset using an indexer. However when I attempt to do this I get the...
4
by: Geordie | last post by:
Hi, I'm in the process of converting a production VS 2003 application to VS 2005. To simplify the conversion I'm converting a small piece at a time and then unit testing the code to confirm it...
8
by: Rainer Queck | last post by:
Hi NG, how can I implement more then one array property to a class? I have read about the Indexer, but as far as I can see this would only work for a Collection type of class like list or...
6
by: pinetaj | last post by:
Hello, I have a question of using 'property' on accessing elements of array. There is an array member in a class. I'd like to restrict accessing the elements of the array through property. And...
10
by: dfetrow410 | last post by:
Is there an example of how to store and access an array in a Property Dave
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.