473,379 Members | 1,330 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,379 software developers and data experts.

Scope issue

ORC
Hi,

I have a class A defined in class B. Class A is private because I don't want
object that makes an instance of class B to be able to see the class A
definition. Instead I want to declare an instance of class A in Class B that
is public so that the object that creates the instance of class B will be
able to call methods in class A throght the class B.

In the object Intellisense shows everything correct but I get an compiler
error saying: "Inconsistant accessibility.."

Any idea how to do this the right way?

Thanks
Ole
Nov 16 '05 #1
6 1598
Hi Ole:

You'll have to make the field of type Class A a private field, too. If
the field is going to be visible you'll need to expose the definition
too, else make them both private.

You can still have public methods on Class B that forwards themselves
to the Class A implementation.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Sat, 6 Nov 2004 23:17:39 +0100, "ORC" <or*@sol.dk> wrote:
Hi,

I have a class A defined in class B. Class A is private because I don't want
object that makes an instance of class B to be able to see the class A
definition. Instead I want to declare an instance of class A in Class B that
is public so that the object that creates the instance of class B will be
able to call methods in class A throght the class B.

In the object Intellisense shows everything correct but I get an compiler
error saying: "Inconsistant accessibility.."

Any idea how to do this the right way?

Thanks
Ole


Nov 16 '05 #2
ORC
A better explanation to my problem is an example:

In "ExampleClass.cs":
namespace Example
{
public class Car
{
1* private class clsEngine <----- This must be INvisible "Form1.cs"
{
int horsePower;
}
2* public clsEngine engine; <----- This must be Visible in "Form1.cs"

public Car(int HorsePower)
{
engine.horsePower = HorsePower;
}
}

And In a buttons click event in "Form1.cs":
using Example;
private void button2_Click(object sender, System.EventArgs e)
{
Car Volvo = new Car(150);
HP = Volvo.engine.horsePower;
}

This gives me the compiler error because 1* and 2* must both be either
private or public. How do I handle this so that Intellisense doesn't show me
the clsEngine class but only the engine field.

Thanks
Ole
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:5r********************************@4ax.com...
Hi Ole:

You'll have to make the field of type Class A a private field, too. If
the field is going to be visible you'll need to expose the definition
too, else make them both private.

You can still have public methods on Class B that forwards themselves
to the Class A implementation.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Sat, 6 Nov 2004 23:17:39 +0100, "ORC" <or*@sol.dk> wrote:
Hi,

I have a class A defined in class B. Class A is private because I don't wantobject that makes an instance of class B to be able to see the class A
definition. Instead I want to declare an instance of class A in Class B thatis public so that the object that creates the instance of class B will be
able to call methods in class A throght the class B.

In the object Intellisense shows everything correct but I get an compiler
error saying: "Inconsistant accessibility.."

Any idea how to do this the right way?

Thanks
Ole

Nov 16 '05 #3
The problem is the compiler needs to be able to resolve the form's access to the members of engine which it can't if its not accessible to the form (being private to the car).

Why not make the engine public but give it an internal constructor. This means that the form can see the engine but it can't create an instance. Then you can make the car create the instance (as it will have access to an internal constructor) and pass back the engine instance it has created.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richard/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#$**************@TK2MSFTNGP14.phx.gbl>

A better explanation to my problem is an example:

In "ExampleClass.cs":
namespace Example
{
public class Car
{
1* private class clsEngine <----- This must be INvisible "Form1.cs"
{
int horsePower;
}
2* public clsEngine engine; <----- This must be Visible in "Form1.cs"

public Car(int HorsePower)
{
engine.horsePower = HorsePower;
}
}

And In a buttons click event in "Form1.cs":
using Example;
private void button2_Click(object sender, System.EventArgs e)
{
Car Volvo = new Car(150);
HP = Volvo.engine.horsePower;
}

This gives me the compiler error because 1* and 2* must both be either
private or public. How do I handle this so that Intellisense doesn't show me
the clsEngine class but only the engine field.

Thanks
Ole
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:5r********************************@4ax.com...
Hi Ole:

You'll have to make the field of type Class A a private field, too. If
the field is going to be visible you'll need to expose the definition
too, else make them both private.

You can still have public methods on Class B that forwards themselves
to the Class A implementation.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Sat, 6 Nov 2004 23:17:39 +0100, "ORC" <or*@sol.dk> wrote:
Hi,

I have a class A defined in class B. Class A is private because I don't wantobject that makes an instance of class B to be able to see the class A
definition. Instead I want to declare an instance of class A in Class B thatis public so that the object that creates the instance of class B will be
able to call methods in class A throght the class B.

In the object Intellisense shows everything correct but I get an compiler
error saying: "Inconsistant accessibility.."

Any idea how to do this the right way?

Thanks
Ole


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.782 / Virus Database: 528 - Release Date: 22/10/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #4
ORC <or*@sol.dk> wrote:
A better explanation to my problem is an example:

In "ExampleClass.cs":
namespace Example
{
public class Car
{
1* private class clsEngine <----- This must be INvisible "Form1.cs"
{
int horsePower;
}
2* public clsEngine engine; <----- This must be Visible in "Form1.cs"

public Car(int HorsePower)
{
engine.horsePower = HorsePower;
}
}


If no-one's meant to know what a clsEngine is, how on earth do you
expect them to use an instance of it?

That's the problem. Either the outside world knows about clsEngine, in
which case you can expose the "engine" field (although I'd argue
against making it a public field; use a property instead) or it doesn't
know about clsEngine, in which case it can't understand what the
"engine" field is.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
ORC
OK - I see the point -thanks. But what if one want some of the members in
class clsEngine to be accesible from class Car and not accesible from class
Form1? - will that be possible ?

Thanks
Ole

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
ORC <or*@sol.dk> wrote:
A better explanation to my problem is an example:

In "ExampleClass.cs":
namespace Example
{
public class Car
{
1* private class clsEngine <----- This must be INvisible in "Form1.cs" {
int horsePower;
}
2* public clsEngine engine; <----- This must be Visible in "Form1.cs"
public Car(int HorsePower)
{
engine.horsePower = HorsePower;
}
}


If no-one's meant to know what a clsEngine is, how on earth do you
expect them to use an instance of it?

That's the problem. Either the outside world knows about clsEngine, in
which case you can expose the "engine" field (although I'd argue
against making it a public field; use a property instead) or it doesn't
know about clsEngine, in which case it can't understand what the
"engine" field is.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6
ORC <or*@sol.dk> wrote:
OK - I see the point -thanks. But what if one want some of the members in
class clsEngine to be accesible from class Car and not accesible from class
Form1? - will that be possible ?


Not unless they're in different assemblies, in which case make the
member internal, which will allow it to be accessed from within the
assembly but not outside.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Michael G | last post by:
I am a little surprised that the following that $x is visible outside of the scope in which it is (?)defined(?) (not sure if that is the correct term here). I am trying to find where in the php...
12
by: Keith Chadwick | last post by:
I have a fairly hefty XSLT file that for the sake of debugging and clarity I wish to split into some separate sub-templates contained within the same file. The master template calls an...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
5
by: elzacho | last post by:
I would like to (and commonly do) define my variables in the most specific scope I can. For example... int foo(int a, int b, int c) { /* don't declare temp here if we can help it */ ... ...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
165
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But...
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
0
by: Cousson, Benoit | last post by:
This is a language limitation. That was my understanding as well, but I think it is a pity to have that limitation. Don't you think that the same improvement that was done for method nested scope...
0
by: Calvin Spealman | last post by:
Please re-evaluate your "need" for nesting classes in the first place. On Tue, Aug 12, 2008 at 1:06 PM, Cousson, Benoit <b-cousson@ti.comwrote: -- Read my blog! I depend on your acceptance...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.