473,804 Members | 3,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fields in Interfaces, Indexer

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

PROBLEM:
I would like to write something like this:

MainClass cm = new MainClass();
cm.SubClass1Val ues[0].SubClass2Value s[0].ToString();

Note that "SubClass2Index er" 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 SubClass2Indexe r aware of all the current and
future implementations of "ISubClass1 ".
2) Do a casting after "cm.SubClass1Va lues[0]" but then I have to write
separate indexers "SubClass2Index er" 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.Collecti ons.Generic;
using System.Text;

namespace testProj
{
class Program
{
static void Main(string[] args)
{
MainClass cm = new MainClass();
cm.SubClass1Val ues[0].SubClass2Value s[0].ToString();
}
}

//CLASSES
public class MainClass
{
private SubClass1Indexe r m_subClass1Inde xer;
protected internal List<ISubClass1 m_subClass1List ; //
SubClass1Indexe r needs access to this.

public SubClass1Indexe r SubClass1Values
{
get { return m_subClass1Inde xer; }
}
}

public class SubClass1 : ISubClass1
{
private SubClass2Indexe r m_subClass2Inde xer;
protected internal List<ISubClass2 m_subClass2List ; //
SubClass2Indexe r needs access to this.

public SubClass2Indexe r SubClass2Values
{
get { return m_subClass2Inde xer; }
}
}

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

SubClass2Indexe r SubClass2Values { get; }
}

public interface ISubClass2
{
}

//INDEXERS
public class SubClass1Indexe r
{
private readonly MainClass m_subClass1Owne r;

public SubClass1Indexe r(MainClass seriesOwner)
{
m_subClass1Owne r = seriesOwner;
}

public ISubClass1 this[Int32 index]
{
get {
return m_subClass1Owne r.m_subClass1Li st[index];
}
}
}

public class SubClass2Indexe r
{
private readonly ISubClass1 m_subClass2Owne r;

public SubClass2Indexe r(ISubClass1 subClass2Owner)
{
m_subClass2Owne r = 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_subClass2Owne r.m_subClass2Li st[index]; }
get { return
((SubClass1)m_s ubClass2Owner). m_subClass2List[index]; }
}
}
}

Feb 15 '07 #1
2 1982
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
SubClass1Indexe r a constructor that took a List<ISubClass1 rather than a
MainClass.

Post again if this doesnt work out.

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

PROBLEM:
I would like to write something like this:

MainClass cm = new MainClass();
cm.SubClass1Val ues[0].SubClass2Value s[0].ToString();

Note that "SubClass2Index er" 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 SubClass2Indexe r aware of all the current and
future implementations of "ISubClass1 ".
2) Do a casting after "cm.SubClass1Va lues[0]" but then I have to write
separate indexers "SubClass2Index er" 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.Collecti ons.Generic;
using System.Text;

namespace testProj
{
class Program
{
static void Main(string[] args)
{
MainClass cm = new MainClass();
cm.SubClass1Val ues[0].SubClass2Value s[0].ToString();
}
}

//CLASSES
public class MainClass
{
private SubClass1Indexe r m_subClass1Inde xer;
protected internal List<ISubClass1 m_subClass1List ; //
SubClass1Indexe r needs access to this.

public SubClass1Indexe r SubClass1Values
{
get { return m_subClass1Inde xer; }
}
}

public class SubClass1 : ISubClass1
{
private SubClass2Indexe r m_subClass2Inde xer;
protected internal List<ISubClass2 m_subClass2List ; //
SubClass2Indexe r needs access to this.

public SubClass2Indexe r SubClass2Values
{
get { return m_subClass2Inde xer; }
}
}

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

SubClass2Indexe r SubClass2Values { get; }
}

public interface ISubClass2
{
}

//INDEXERS
public class SubClass1Indexe r
{
private readonly MainClass m_subClass1Owne r;

public SubClass1Indexe r(MainClass seriesOwner)
{
m_subClass1Owne r = seriesOwner;
}

public ISubClass1 this[Int32 index]
{
get {
return m_subClass1Owne r.m_subClass1Li st[index];
}
}
}

public class SubClass2Indexe r
{
private readonly ISubClass1 m_subClass2Owne r;

public SubClass2Indexe r(ISubClass1 subClass2Owner)
{
m_subClass2Owne r = 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_subClass2Owne r.m_subClass2Li st[index]; }
get { return
((SubClass1)m_s ubClass2Owner). 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 SubClass1Indexe r 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
8375
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
4362
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 MAPI.AddressEntry.Fields(proptag) property. VS .NET will not allow the code to compile. I am looking to pull out more info than just the addresses and names. My environment consists of the following CDO 1.2 Exchange 5. Outlook 200 VS .NET 200 ...
7
485
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(); // ?? .... ColorCollection _colors=new ColorCollection() public ColorCollection Color
7
4621
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 than indexer Whouse.this. Would someone please tell me why? public class Whouse : IEnumerable public CRegs this // Indexer { get
5
2300
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
5066
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. consistently may make the code easier to understand for someone else, etc. Using "this." makes it immediately clear, for example, that the variable being referred to is a member of the same class and is not declared in the method as a local variable. ...
3
1480
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 do not understand the specification that says something along this line "with indexers you can index objects of a class like an array". The thing is, in all the examples, there is only one "new" call when only one object is created! There is no...
7
2118
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 ----------------------------------------------------------------------------- This is a consortium of ideas from another thread on topic ----------------------------------------------------------------------------- One of the big issues of organizing items within a class, is there are many...
17
2093
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,
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9569
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.