Another noob question

A

azz131

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?
 
J

Jon Skeet [C# MVP]

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
P

Peter Duniho

[...]
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
 
N

Nicholas Paldino [.NET/C# MVP]

MyClass is not private, but rather internal.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nicholas Paldino said:
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]
- (e-mail address removed)

azz131 said:
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?
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

azz131 said:
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.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Nicholas said:
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. :)
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
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.
 
J

Jon Skeet [C# MVP]

Göran Andersson said:
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."
 

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