Basic class question

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

Hi,

I want that class C is only accessable by B.
This means that only B can access methods inside C.
Class A cannot access C. But may ask B.

How do I do this?

Thanks!
 
Make class C a private nested class inside class.

Here is an example program:

public interface IMyInterface
{
string DoSomething();
}

public class MyClassA
{
public static void Main()
{
MyClassA a = new MyClassA();

Console.WriteLine(a.DoSomethingInMyClassC());
Console.ReadLine();
}

public string DoSomethingInMyClassC()
{
MyClassB b = new MyClassB();
return b.GetAccessToMyClassC().DoSomething();
}
}

public class MyClassB
{
// MyClassC is private and only MyClassB can access it.
private class MyClassC : IMyInterface
{
public string DoSomething()
{
return "You are doing something in MyClassC!";
}
}

// MyClassB can create a new instance of MyClassC and return its
interface.
public IMyInterface GetAccessToMyClassC()
{
return new MyClassC();
}
}


Hope this helps.
--
Brian Delahunty
Ireland

http://briandela.com/blog

INDA SouthEast - http://southeast.developers.ie/ - The .NET usergroup I
started in the southeast of Ireland.
 
Not exactly where I'm looking for.

I want something like this, without the creating of object.

namespace presentation {
public class mypres
{
// Get value form logic.
// It may and cannot access database.mydatabase directly.
string tmp = logic.mylogic.myvalue(); // ... value
}
}

namespace logic {
public class mylogic
{
// Get value from datbase.
// This one may access database.mydatabase.
public string myvalue() { return database.mydatabase; } // value
}
}

namespace database {
public class mydatabase
{
protected string test() { return "123"; }
}
}

But this don't work, because I don't know how to set the public, protected,
etc.
Can you help?

Thanks!
 
That, unfortunately, is not possible in the current version of C#. The
best you can do is move B and C to a separate assembly and declare C to
be internal.

Regards
Senthil
 
Back
Top