469,133 Members | 1,019 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,133 developers. It's quick & easy.

Another noob question

Hi, i want to access an array of objects inside a method like this

using System;
using System.Collections.Generic;

namespace ObjectArray
{

class MainClass{
class MyClass
{
public int x=0;
MyClassTo[] obj=new MyClassTo[2];

public MyClass(){
obj[0]=new MyClassTo();
obj[1]=new MyClassTo();
}
public void display(){
show(this);
}
}
class MyClassTo{
int x;
int y;
}

public static void show(MyClass my){ // Error here
obj[0].x=10; //How do i access the
object array?

}
public static void Main(string[] args){
Console.ReadLine();
}
}
}
but i get an error "Inconsistent accessibility: parameter type
'ObjectArray.MainClass.MyClass' is less accessible than method
'ObjectArray.MainClass.show(ObjectArray.MainClass. MyClass)' (CS0051) - "
What am i doing wrong?

May 10 '07 #1
8 1149
Since the show method is public, all parameters to that method have to
be accessible. The MyClass class is private, which is why you get the
error. Change the MyClass class to public, and it will work.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"azz131" <bi********@ntlworld.comwrote in message
news:1l*****************@newsfe1-win.ntli.net...
Hi, i want to access an array of objects inside a method like this

using System;
using System.Collections.Generic;

namespace ObjectArray
{

class MainClass{
class MyClass
{
public int x=0;
MyClassTo[] obj=new MyClassTo[2];

public MyClass(){
obj[0]=new MyClassTo();
obj[1]=new MyClassTo();
}
public void display(){
show(this);
}
}
class MyClassTo{
int x;
int y;
}

public static void show(MyClass my){ // Error here
obj[0].x=10; //How do i access the
object array?

}
public static void Main(string[] args){
Console.ReadLine();
}
}
}
but i get an error "Inconsistent accessibility: parameter type
'ObjectArray.MainClass.MyClass' is less accessible than method
'ObjectArray.MainClass.show(ObjectArray.MainClass. MyClass)' (CS0051) - "
What am i doing wrong?

May 10 '07 #2
azz131 <bi********@ntlworld.comwrote:

<snip>
but i get an error "Inconsistent accessibility: parameter type
'ObjectArray.MainClass.MyClass' is less accessible than method
'ObjectArray.MainClass.show(ObjectArray.MainClass. MyClass)' (CS0051) - "
What am i doing wrong?
You're trying to use MyClass - which is only accessible to other types
within the same assembly - as a parameter to the show method, which is
public (i.e. available to all types, regardless of assembly).

Either make the show method internal, or make MyClass public.

Oh, and you'll need to add some way of accessing the array within
MyClass, as otherwise MyClassTo doesn't have any way of getting to the
data.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 10 '07 #3
On Thu, 10 May 2007 11:17:33 -0700, azz131 <bi********@ntlworld.comwrote:
[...]
but i get an error "Inconsistent accessibility: parameter type
'ObjectArray.MainClass.MyClass' is less accessible than method
'ObjectArray.MainClass.show(ObjectArray.MainClass. MyClass)' (CS0051) - "
What am i doing wrong?
The compiler is telling you that the access modifiers for the two things
don't match. In particular, your class is not public, but the method is.
That means even though your method is visible to callers outside of the
class, the type used in the parameter list is not. Since it's not nice to
publish a method to callers but keep the type of the parameter to that
method secret, the compiler complains. :)

Pete
May 10 '07 #4
MyClass is not private, but rather internal.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP02.phx.gbl...
Since the show method is public, all parameters to that method have to
be accessible. The MyClass class is private, which is why you get the
error. Change the MyClass class to public, and it will work.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"azz131" <bi********@ntlworld.comwrote in message
news:1l*****************@newsfe1-win.ntli.net...
>Hi, i want to access an array of objects inside a method like this

using System;
using System.Collections.Generic;

namespace ObjectArray
{

class MainClass{
class MyClass
{
public int x=0;
MyClassTo[] obj=new MyClassTo[2];

public MyClass(){
obj[0]=new MyClassTo();
obj[1]=new MyClassTo();
}
public void display(){
show(this);
}
}
class MyClassTo{
int x;
int y;
}

public static void show(MyClass my){ // Error here
obj[0].x=10; //How do i access the
object array?

}
public static void Main(string[] args){
Console.ReadLine();
}
}
}
but i get an error "Inconsistent accessibility: parameter type
'ObjectArray.MainClass.MyClass' is less accessible than method
'ObjectArray.MainClass.show(ObjectArray.MainClass .MyClass)' (CS0051) - "
What am i doing wrong?


May 10 '07 #5
azz131 wrote:
Hi, i want to access an array of objects inside a method like this

using System;
using System.Collections.Generic;

namespace ObjectArray
{

class MainClass{
class MyClass
{
public int x=0;
MyClassTo[] obj=new MyClassTo[2];
If you want to access the member from outside the class, it can't be
private.

public MyClassTo[] obj = new MyClassTo[2];

However, you should consider keeping all member variables private, and
create properties for exposing them outside the class.
>
public MyClass(){
obj[0]=new MyClassTo();
obj[1]=new MyClassTo();
}
public void display(){
show(this);
}
}
class MyClassTo{
int x;
int y;
public int x;
public int y;
}

public static void show(MyClass my){ // Error here
obj[0].x=10; //How do i access the
object array?
my.obj[0].x = 10;
>
}
public static void Main(string[] args){
Console.ReadLine();
}
}
}
but i get an error "Inconsistent accessibility: parameter type
'ObjectArray.MainClass.MyClass' is less accessible than method
'ObjectArray.MainClass.show(ObjectArray.MainClass. MyClass)' (CS0051) - "
What am i doing wrong?
You have made the show method public, but the class MyClass is private.
That means that the method is visible outside the class, but it can't be
used outside the class as it's impossible to create a value for the
parameter.

--
Göran Andersson
_____
http://www.guffa.com
May 10 '07 #6
Nicholas Paldino [.NET/C# MVP] wrote:
MyClass is not private, but rather internal.
The default accessibility for class members is private, and as MyClass
is a member of MainClass that would make it private, wouldn't it?

I prefer to always specify the accessibility level, so that there never
is any doubt what it is. :)

--
Göran Andersson
_____
http://www.guffa.com
May 10 '07 #7
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
MyClass is not private, but rather internal.
You were right first time, actually - I hadn't noticed before, but it's
nested inside MainClass, so it's private by default.

To the OP: Why do you have all these nested classes? They should crop
up pretty rarely in most code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 10 '07 #8
Göran Andersson <gu***@guffa.comwrote:
Nicholas Paldino [.NET/C# MVP] wrote:
MyClass is not private, but rather internal.
The default accessibility for class members is private, and as MyClass
is a member of MainClass that would make it private, wouldn't it?

I prefer to always specify the accessibility level, so that there never
is any doubt what it is. :)
On the other hand, you should generally make things as private as you
can get away with, which is what the default is. I like the fact that
by specifying an access modifier, I'm saying "I want this to be more
public than it would be by default."

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 10 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dave Williams | last post: by
10 posts views Thread by Matt Hollingsworth | last post: by
3 posts views Thread by We need more power captain | last post: by
29 posts views Thread by mike_wilson1333 | last post: by
9 posts views Thread by davetelling | last post: by
6 posts views Thread by Lang Murphy | last post: by
2 posts views Thread by Carnell, James E | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.