Scope issue

  • Thread starter Thread starter ORC
  • Start date Start date
O

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
 
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.
 
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
 
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/<#[email protected]>

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



---
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]
 
ORC said:
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.
 
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
 
ORC said:
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.
 
Back
Top