473,378 Members | 1,539 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,378 software developers and data experts.

Fields in Interfaces, Indexer

SITUATION:
I have a class "MainClass" that has an indexer "SubClass1Indexer".
This indexer returns an instance that implements a particular
interface "ISubClass1". This interface defines another indexer
"SubClass2Indexer". Bellow is some code with comments. It should
compile.

PROBLEM:
I would like to write something like this:

MainClass cm = new MainClass();
cm.SubClass1Values[0].SubClass2Values[0].ToString();

Note that "SubClass2Indexer" needs access to an internal field of
"SubClass1". This only seems to be possible if a field is allowed in
an interface, i.e. ISubClass1. That is not the case, though.

NONE IDEAL SOLUTIONS:
1) Make "this[]" in SubClass2Indexer aware of all the current and
future implementations of "ISubClass1".
2) Do a casting after "cm.SubClass1Values[0]" but then I have to write
separate indexers "SubClass2Indexer" for each implementation of
"ISubClass1".

QUESTION:
How can I solve the shortcomings in the above solutions? Or in general
how is this best handled?

CODE:
using System;
using System.Collections.Generic;
using System.Text;

namespace testProj
{
class Program
{
static void Main(string[] args)
{
MainClass cm = new MainClass();
cm.SubClass1Values[0].SubClass2Values[0].ToString();
}
}

//CLASSES
public class MainClass
{
private SubClass1Indexer m_subClass1Indexer;
protected internal List<ISubClass1m_subClass1List; //
SubClass1Indexer needs access to this.

public SubClass1Indexer SubClass1Values
{
get { return m_subClass1Indexer; }
}
}

public class SubClass1 : ISubClass1
{
private SubClass2Indexer m_subClass2Indexer;
protected internal List<ISubClass2m_subClass2List; //
SubClass2Indexer needs access to this.

public SubClass2Indexer SubClass2Values
{
get { return m_subClass2Indexer; }
}
}

//INTERFACES
public interface ISubClass1
{
//!!! If the following would be possible then in
SubClass2Indexer
// I would not have to make the function 'this[]' be aware of
all
// the ISubClass1 implementations.
//protected internal List<ISubClassm_subClass2List;

SubClass2Indexer SubClass2Values { get; }
}

public interface ISubClass2
{
}

//INDEXERS
public class SubClass1Indexer
{
private readonly MainClass m_subClass1Owner;

public SubClass1Indexer(MainClass seriesOwner)
{
m_subClass1Owner = seriesOwner;
}

public ISubClass1 this[Int32 index]
{
get {
return m_subClass1Owner.m_subClass1List[index];
}
}
}

public class SubClass2Indexer
{
private readonly ISubClass1 m_subClass2Owner;

public SubClass2Indexer(ISubClass1 subClass2Owner)
{
m_subClass2Owner = subClass2Owner;
}

public ISubClass2 this[Int32 index]
{
//The command commented out would be possible if one could
// define a field in an interface. Now I'll need a switch
// to handle all the IClass1 implementations. So I can't
just
// add a new implementation without adjusting this
function.
//get { return m_subClass2Owner.m_subClass2List[index]; }
get { return
((SubClass1)m_subClass2Owner).m_subClass2List[index]; }
}
}
}

Feb 15 '07 #1
2 1957
Your structure is confusing which means there may be a simpler design.
However, I think you should be able to solve the issue by giving
SubClass1Indexer a constructor that took a List<ISubClass1rather than a
MainClass.

Post again if this doesnt work out.

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"hu*******@yahoo.com" wrote:
SITUATION:
I have a class "MainClass" that has an indexer "SubClass1Indexer".
This indexer returns an instance that implements a particular
interface "ISubClass1". This interface defines another indexer
"SubClass2Indexer". Bellow is some code with comments. It should
compile.

PROBLEM:
I would like to write something like this:

MainClass cm = new MainClass();
cm.SubClass1Values[0].SubClass2Values[0].ToString();

Note that "SubClass2Indexer" needs access to an internal field of
"SubClass1". This only seems to be possible if a field is allowed in
an interface, i.e. ISubClass1. That is not the case, though.

NONE IDEAL SOLUTIONS:
1) Make "this[]" in SubClass2Indexer aware of all the current and
future implementations of "ISubClass1".
2) Do a casting after "cm.SubClass1Values[0]" but then I have to write
separate indexers "SubClass2Indexer" for each implementation of
"ISubClass1".

QUESTION:
How can I solve the shortcomings in the above solutions? Or in general
how is this best handled?

CODE:
using System;
using System.Collections.Generic;
using System.Text;

namespace testProj
{
class Program
{
static void Main(string[] args)
{
MainClass cm = new MainClass();
cm.SubClass1Values[0].SubClass2Values[0].ToString();
}
}

//CLASSES
public class MainClass
{
private SubClass1Indexer m_subClass1Indexer;
protected internal List<ISubClass1m_subClass1List; //
SubClass1Indexer needs access to this.

public SubClass1Indexer SubClass1Values
{
get { return m_subClass1Indexer; }
}
}

public class SubClass1 : ISubClass1
{
private SubClass2Indexer m_subClass2Indexer;
protected internal List<ISubClass2m_subClass2List; //
SubClass2Indexer needs access to this.

public SubClass2Indexer SubClass2Values
{
get { return m_subClass2Indexer; }
}
}

//INTERFACES
public interface ISubClass1
{
//!!! If the following would be possible then in
SubClass2Indexer
// I would not have to make the function 'this[]' be aware of
all
// the ISubClass1 implementations.
//protected internal List<ISubClassm_subClass2List;

SubClass2Indexer SubClass2Values { get; }
}

public interface ISubClass2
{
}

//INDEXERS
public class SubClass1Indexer
{
private readonly MainClass m_subClass1Owner;

public SubClass1Indexer(MainClass seriesOwner)
{
m_subClass1Owner = seriesOwner;
}

public ISubClass1 this[Int32 index]
{
get {
return m_subClass1Owner.m_subClass1List[index];
}
}
}

public class SubClass2Indexer
{
private readonly ISubClass1 m_subClass2Owner;

public SubClass2Indexer(ISubClass1 subClass2Owner)
{
m_subClass2Owner = subClass2Owner;
}

public ISubClass2 this[Int32 index]
{
//The command commented out would be possible if one could
// define a field in an interface. Now I'll need a switch
// to handle all the IClass1 implementations. So I can't
just
// add a new implementation without adjusting this
function.
//get { return m_subClass2Owner.m_subClass2List[index]; }
get { return
((SubClass1)m_subClass2Owner).m_subClass2List[index]; }
}
}
}

Feb 16 '07 #2
Ciaran,

That indeed seems to be a solution. The reason I was passing MainClass
to the ctr of SubClass1Indexer is because samples from MSDN and
CodeProject both did it this way. See here:

http://msdn.microsoft.com/library/de...esTutorial.asp

Class WordCollection is the indexer and it's constructor takes the
parent class Document. MSDN says: "For each "indexed property," you
define a nested class, which contains a reference back to the main
class instance."

Is there a reason why most people do it this way and not the way you
suggested (which seems to work better for me)?

Feb 16 '07 #3

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

Similar topics

14
by: Nikhil Patel | last post by:
Hi all, Is it possible to have more than one indexer in a class. If not, are there any good alternative ways to achieve similar functionality. Thanks... -Nikhil
3
by: Mike | last post by:
Hello I am trying to read information from the GAL using C# .NET. I am able to return a list of all the addresses and names, however, when I walk through each address I cannot access the...
7
by: Oren | last post by:
hi I have created a class with a simple indexer what is the syntax for adding the function "Remove" ... myClass.colors=Color.Black; // WORKS O myClass.colors.Remove(); // ?? .......
7
by: Steph | last post by:
I'm learning C#. I need to implement an indexer using an array list. I have the following code and I'm getting an error "Inconsistent accessibility: indexer return type CRegs is less accessible...
5
by: Clive Dixon | last post by:
Is it possible to access an indexer of a base class with identical signature, e.g. class Class1 { public object this { get { // ...
60
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
3
by: Nash Alexx | last post by:
Hello! I am quite new to C#, and one concept that really gives me a headache is "indexer". I have gone through the MSDN examples, and, at some level know how to use indexers. But, the thing is I...
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
17
by: SemSem | last post by:
i want to know waht is an index and how we use it with a simple example including the main of the program . thanx -- Islam Khalil,
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.