Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I have two classes defined in namespace: for example

namespace MyNamespace
{
public class demo1
{
public static int Myfunction1()
{
return demo2.Myfunction2(100,400);
}
}

public class demo2
{
public static int Myfunction2(int a, int b)
{
retutn a+b
}
}
}


I call the Myfunction using the following
int result= MyNamespace.demo1.Myfunction1() which calls Myfunction2

similarly i can call MyNamespace.demo2.Myfunction2() but i want it to be not accessible this way. Because i want user just call Myfunction1 which in turn call Myfunction2.user should not be able to call Myfunction2 by any way. How to do this?
Posted

Don't use static methods. Create classes with methods and then create their instances to use these methods.
 
Share this answer
 
v2
Comments
xpertzgurtej 15-Apr-15 5:46am    
making them static was my requirement..Can't change them..Please suggest a solution having them as it is..
Abhinav S 15-Apr-15 5:59am    
Workaround. Make the method protected. It will only be accessible in overriden classes.
If the user you're referring to will use your classes through an assembly-reference, you can prevent him from accessing a particular class by marking it as internal[^]. Internal types or members are accessible only within files in the same assembly.

If the user is able to put his code into the same assembly as your classes, then you would have to make the class that he shouldn't use a private or protected nested class. But I wouldn't recommend doing that if this would serve only the purpose of "hiding" the class and not actually making sense in the context of your class design.

Edit:
C#
// a nested class that can't be accessed "from outside":

namespace MyNamespace
{
   public class demo1
   {
      private class demo2 // or protected instead of private
      {
         public static int Myfunction2(int a, int b)
         {
            retutn a+b
         }
      }

      public static int Myfunction1()
      {
         return demo2.Myfunction2(100,400);
      }
   }
}
 
Share this answer
 
v2
Comments
xpertzgurtej 15-Apr-15 6:26am    
Both the classes are in same namespace. And user is calling them form the same project(from the front end) I want user can call Myfunction1 but not Myfunction2
Sascha Lefèvre 15-Apr-15 6:28am    
Does Myfunction2 have to be in a different class than Myfunction1 ?
xpertzgurtej 15-Apr-15 6:38am    
Yes. My class structure is like that..
Sascha Lefèvre 15-Apr-15 6:40am    
Would it make sense to make demo2 a nested class of demo1?
xpertzgurtej 15-Apr-15 6:54am    
that will create complexity..still if there are no other options then it could it a nested class. How to do that?

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