473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 1241
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave Williams | last post by:
First off...total noob to VB. So far have learned a lot in 1.5 days and feel fairly comfortable at throwing screens up. However, I want to start writing forms that revolve around an access...
10
by: Matt Hollingsworth | last post by:
Hello, Very new to python, so a noob question. When I've written stuff in JavaScript or MEL in the past, I've always adopted the variable naming convention of using a $ as the first character...
1
by: davestrike | last post by:
I am a noob to sql and asp programming. I am working on a db for the gaming squad I am a member of. One of the pages I created is a roster list of all the squad members. Part of this roster is...
3
by: We need more power captain | last post by:
Hi, I know less than a noob, I've been asked to do some compiles in VC++ 6 without knowing too much at all. (I'm a COBOL program normally so this is all too much for me) I open VC++6, open...
29
by: mike_wilson1333 | last post by:
I would like to generate every unique combination of numbers 1-5 in a 5 digit number and follow each combo with a newline. So i'm looking at generating combinations such as: (12345) , (12235),...
9
by: davetelling | last post by:
I am not a programmer, I'm an engineer trying to make an interface to a product I'm designing. I have used C# to make a form that interrogates the unit via the serial port and receives the data. I...
6
by: Lang Murphy | last post by:
I'm baaaaack... some of you answered a question I had last week. Only problem is: I'm a dope who doesn't understand most of what y'all posted. Raw noob when it comes to .Net and C#. So I'm going...
3
cypherzero
by: cypherzero | last post by:
Using this header file (mstring.h) for a class defined in a seperate file (mstring.cpp) I recieve 8 'unresolved external symbol' errors when trying to use the class in my main source file (main.cpp)....
2
by: Carnell, James E | last post by:
I am thinking about purchasing a book, but wanted to make sure I could get through the code that implements what the book is about (Artificial Intelligence a Modern Approach). Anyway, I'm not a...
1
by: SCRIPT KEEPER | last post by:
Hello, I am a complete noob and just starting off with csharp so I apologize for my basic question. I am wanting to start powershell from inside a batch script and then to pass the powershell args...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.