PC Review


Reply
Thread Tools Rate Thread

Another noob question

 
 
azz131
Guest
Posts: n/a
 
      10th May 2007
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?

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      10th May 2007
azz131 <(E-Mail Removed)> wrote:

<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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      10th May 2007
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 Removed)

"azz131" <(E-Mail Removed)> wrote in message
news:1lJ0i.4088$(E-Mail Removed)...
> 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?



 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      10th May 2007
On Thu, 10 May 2007 11:17:33 -0700, azz131 <(E-Mail Removed)> wrote:

> [...]
> 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
 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      10th May 2007
MyClass is not private, but rather internal.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Nicholas Paldino [.NET/C# MVP]" <(E-Mail Removed)> wrote in
message news:%(E-Mail Removed)...
> 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 Removed)
>
> "azz131" <(E-Mail Removed)> wrote in message
> news:1lJ0i.4088$(E-Mail Removed)...
>> 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?

>
>



 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      10th May 2007
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
 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      10th May 2007
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
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      10th May 2007
Nicholas Paldino [.NET/C# MVP] <(E-Mail Removed)> wrote:
> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      10th May 2007
Göran Andersson <(E-Mail Removed)> wrote:
> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Noob Question 2 =?Utf-8?B?Ty4uLi4=?= Microsoft Excel Programming 6 3rd Oct 2007 11:43 AM
Noob Question, please help rocha1573 Microsoft ADO .NET 1 14th Oct 2006 04:41 AM
noob question khhome06 via AccessMonster.com Microsoft Access Forms 1 26th Jul 2006 01:34 PM
noob Question =?Utf-8?B?QmV2bzEy?= Microsoft Access Forms 1 3rd Apr 2006 05:04 AM
Noob question #2 you Microsoft VB .NET 1 17th Jan 2005 11:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:23 AM.