Is it possible to force a method to be called only by a specific one?

Asked

Viewed 224 times

3

I have a father method that calls a son method, I wish the son method could only be called exclusively by the father method, it would be possible?

Example:

public void MetodoPai(){
    //codigo
    MetodoFilho();
    //codigo
} 

void MetodoFilho(){
    //codigo
} 


public void OutroMetodoQualquer(){
    //codigo
    MetodoFilho(); //Causar erro na compilação ou algo do tipo
} 
  • This is all mixed up in the same class ?

  • Yes, it’s all within one class

  • No. Perhaps the solution to the problem you are facing is another one. We may try to help through a context and a [mcve]

  • @Diegorafaelsouza you want a more verifiable example than that?

  • @Leonardobonetti what you need are local functions, declared functions of a method that are only available within the scope of that method.

  • @Diegorafaelsouza Explain to me which points of my example disagree with the M.C.V example.

  • You are starting from what you believe to be a solution. My proposal was that you change the perspective of the question, starting from the problem.

  • There is no problem in my code, it is a question whether or not there is a resource, there is no solution to something that does not have a problem, has a doubt.

  • 1

    what Diego meant, is for you to explain what the need of the "son method could only be called exclusively by the father method" because for this need there can be other solutions...

  • @Rovannlinhalis now yes understood, I want to delimit not allow this method to be called by others as a form of organization and prevention that another programmer (or even me) use the method in the future because it only exists according to the parent method.

  • Maybe it’s something pretty useless, but I believe that so the code gets more organized, I may be tremendously wrong too.

  • right, I think the most appropriate thing would be to organize it into classes... Or if you have to use the function more than once within the parent method... you can use the function that Niero put in the answer, And if you’re only going to use it once, I don’t even see why you’d be inside another function...

Show 7 more comments

2 answers

3


If everything is within the same class (only with the comments that this became clear) to local function is the solution. Obviously it is limited to a method, there is no way to make a method be accessed by a list of methods.

Everything has to be within the scope or specified the scope in a general, non-specific way, unless you make an external tool that you have to go through throughout the compilation. One of the reasons for the creation of .NET Compiler Platform is just making this kind of tool. But of course if you don’t go through it won’t prohibit anything.

Remembering that forcing or prohibiting something always occurs when the programmer wants to follow the standard protocols, if he wants to go over the top he always succeeds. This would be protection against inadvertent error, not security against fraud.

If the local function does not meet, the maximum that the default compiler allows is to ban your call outside the class (private), or outside the class hierarchy (protected) or outside the Assembly (internal) or a combination of the latter two, or may limit further and allow a class method to be the only one to access a function. But you can’t nominally say who can access.

It is possible to say that can be accessed by some other Assembly specific with an attribute.

-1

Perhaps the best solution is to separate by classes, where the Father method and the Son method are in the same class, but the child is private and can only be accessed from within the class itself. And a second class that implements the first. The second class will only visualize the Father method because it is public. The Son method will be hidden for this second class.

What "Leonardo Bonetti" meant by problems is that Programs are built to solve a problem. That is, you need to add up two numbers, that is the problem: "add up two numbers". Then you create solutions to solve this problem. Knowing what you need to solve, makes it easier for us to indicate a solution.

Anyway, class is the best way to solve this issue of hiding methods, but the right resolution is only possible knowing the scope of your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.