Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
scenario is a class contains 5 methods and users are there ,first user need to access first 3 method and 2nd user access remain 2 methods.

how we provide this kind of functionality.
Posted
Comments
DamithSL 27-Jun-12 23:28pm    
Role based method access or something else you need?
jameschowdary 30-Jun-12 0:34am    
no rolebased,in oops using c#
Sergey Alexandrovich Kryukov 28-Jun-12 1:46am    
You cannot restrict access (in traditional sense of this word), just because "users" only exist in run time. The post by Damith is good.
--SA

If you need Role based Method access

role-based-access-to-methods[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Jun-12 1:44am    
The question is very vague, but role-based access (based on throwing "access" exceptions, in fact) is a good point, have my 5.
--SA
If the scenario is not realy a role based access. and what you want is two objects say
Object1 and Object2
where Object1 can access 3 methods of that class and
Object2 can access 2 methods of that class, then you may need to Do as follow.

Create two interfaces and a class as shown below.

C#
interface IUser1
{
    void Method1();
    void Method2();
    void Method3();
}
interface IUser2
{
    void Method4();
    void Method5();
}

class Users: IUser1,IUser2
{
    public void Method1()
    {
    }

    public void Method2()
    {
    }

    public void Method3()
    {
    }

    public void Method4()
    {
    }

    public void Method5()
    {
    }
}


now create object for the class.

C#
            IUser1 userA = new Users();
            userA.Method1();
            userA.Method2();
            userA.Method3();

            IUser2 userB = new Users();
            userB.Method4();
            userB.Method5();

//Or you can cast the objects as below.

            IUser1 userA = new Users();
            userA.Method1();
            userA.Method2();
            userA.Method3();

            IUser2 userB = userA;
            userB.Method4();
            userB.Method5();


now here userA can access only
Method1();
Method2();
Method3();

and userB can access only
Method4();
Method5();


now if you are trying to solve role based problems then return the corresponding Interface object which is appropriate for the role and your issue will be solved.
Hope this will help.
 
Share this answer
 
v3

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