ArrayList and Indexer - Help!

G

Guest

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[int].
Would someone please tell me why?

public class Whouse : IEnumerable

public CRegs this[ int index ] // Indexer
{
get
{
return(myRegs[ this ]);
}
}

ArrayList myRegs = new ArrayList(); //
 
G

Guest

Steph,

You have to cast the return value to CRegs and pass index in instead of
"this":

return((CRegs)myRegs[ index]);
 
M

Marcos Stefanakopolus

What is CRegs? Is it a public class? If you've declared it as a private
class internal to Whouse, that's probably your problem.
 
G

Guest

Tu-Thach,
Thanks for helping me! (Cam on!) I've just tried the return as you
indicated and I'm still getting the same error. The error is on this line:
public CRegs this[ int index ]

Tu-Thach said:
Steph,

You have to cast the return value to CRegs and pass index in instead of
"this":

return((CRegs)myRegs[ index]);

Steph said:
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[int].
Would someone please tell me why?

public class Whouse : IEnumerable

public CRegs this[ int index ] // Indexer
{
get
{
return(myRegs[ this ]);
}
}

ArrayList myRegs = new ArrayList(); //
 
G

Guest

Marcos,
It's a good question. I'm supposed to do an indexer and I'm not doing it
right.

If I get these statements in UML:
CRegs this[ int i ] { get; }
ArrayList myRegs;

Is it possible to make an indexer out of them?
Thanks!

Marcos Stefanakopolus said:
What is CRegs? Is it a public class? If you've declared it as a private
class internal to Whouse, that's probably your problem.

Steph said:
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[int].
Would someone please tell me why?

public class Whouse : IEnumerable

public CRegs this[ int index ] // Indexer
{
get
{
return(myRegs[ this ]);
}
}

ArrayList myRegs = new ArrayList(); //
 
G

Guest

Steph,

Where are you defining your CRegs class? Do you have the code for that? It
sounds like it is not accessible (private).

Steph said:
Tu-Thach,
Thanks for helping me! (Cam on!) I've just tried the return as you
indicated and I'm still getting the same error. The error is on this line:
public CRegs this[ int index ]

Tu-Thach said:
Steph,

You have to cast the return value to CRegs and pass index in instead of
"this":

return((CRegs)myRegs[ index]);

Steph said:
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[int].
Would someone please tell me why?

public class Whouse : IEnumerable

public CRegs this[ int index ] // Indexer
{
get
{
return(myRegs[ this ]);
}
}

ArrayList myRegs = new ArrayList(); //
 
G

Guest

This is how CRegs is defined:

class CRegs
{
public CRegs(double initVal1, bool stat1) {
....
}
}


Marcos Stefanakopolus said:
What is CRegs? Is it a public class? If you've declared it as a private
class internal to Whouse, that's probably your problem.

Steph said:
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[int].
Would someone please tell me why?

public class Whouse : IEnumerable

public CRegs this[ int index ] // Indexer
{
get
{
return(myRegs[ this ]);
}
}

ArrayList myRegs = new ArrayList(); //
 
B

Bruce Wood

You need to say

public class CRegs
{
....

If you don't say anything else, the access level defaults to
"internal", which is why the compiler was complaining: You can't define
a public method or property that returns a type that's only internal,
because some callers will be able to see the method or property, but
not have access to the definition of the return type.
 

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