Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0
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 8-Dec-12 5:04am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You cannot do this in classes, however you can create interfaces to show/hide methods you want (this can be circumvented).
 
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);
}
  Permalink  
Comments
Rahul Rajat Singh at 14-Dec-12 3:35am
   
Perfect. +5.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
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.
A1Only = A2Only;
A1Only.DoSomethingElse();
A2Only.DoSomething()
All of those will cause compilation errors.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

i dont think c# has any thing to do your porpuse but you can get your goal with this code
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
  Permalink  

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

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 DamithSL 225
3 Afzaal Ahmad Zeeshan 202
4 Maciej Los 190
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100