Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
Hi There is class CLASS A which contains 15 Methods. And creating the object to CLASS A like A1,A2,A3 .

Instance A1 only access first five Methods of that CLASS A Not the remaining Methods.

Instance A2 only access second five Methods of that CLASS A Not the remaining Methods.

Instance A3 only access Last five Methods of that CLASS A Not the remaining Methods.

How to restrict object in this manner.



Thanks
Posted

You cannot do this in classes, however you can create interfaces to show/hide methods you want (this can be circumvented).

C#
public interface Ifirst
{
   void Method1();
}

public interface Isecond
{
   void Method2(); 
}

public class A : Ifirst, Isecond
{ 
...
}

public void DoSomething(Ifirst o)
{
   o.Method1(); // only accessible
}

public void Main()
{
   A a = new A();

   DoSomething(a);
}
 
Share this answer
 
Comments
Rahul Rajat Singh 14-Dec-12 3:35am    
Perfect. +5.
You can't restrict access on an instance by instance basis - the best you can do is to create an abstract base class which contains the common propteries, methods and so forth, and implement derived versions of that for each set of methods you want to have available.

C#
public abstract class A {}
public class A1 : A
   {
   public void DoSomething(){}
   }
public class A2 : A
   {
   public void DoSomethingElse(){}
   }

...


A either = new A1();
A1 A1Only = new A1();
A2 A2Only = new A2();
either = A2Only;
A1Only.DoSomething();
A2Only.DoSomethingElse();
All of those are legal.
C#
A1Only = A2Only;
A1Only.DoSomethingElse();
A2Only.DoSomething()
All of those will cause compilation errors.
 
Share this answer
 
The best way to do this is to have the interfaces for each set of functions. Have your class implement all three interfaces. the place where you are creating the instances have the instances of the class on the interface variable.

i.e.

interface and methods

I1 -> A1, A2, A3
I2 -> B1, B2, B3

the class
class A : I1, I2

the use

I1 a1 = new A(); this will access only A1,A2,A3
I2 s2 = new A(); this will access only B1,B2,B3

UPDATE: Mehdi Gholam has given a code for this. it seems to be the perfect solution.
 
Share this answer
 
v2
i dont think c# has any thing to do your porpuse but you can get your goal with this code
C#
class Test
{
private void func1()
{
}
private void func2()
{
}
private void func3()
{
}
private void func4()
{
}
private void func5()
{
}
private void func6()
{
}
public void group1(string func)
{
if (func=="func1")
func1();
else if (func=="func2")
func2();
}

public void group2(string func)
{
if (func=="func3")
func3();
else if (func=="func4")
func4();
}

public void group3(string func)
{
if (func=="func5")
func5();
else if (func=="func6")
func6();
}
}


with this lass you grouped the functions and you can use them ...
if you have more function ,you can use switch instead of if...else if
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900