Method inside method or class within class

1

Created methods Above(), Below(), Highest(), Lowest(), Evens(), Odds(), Matching(), Repeating(), Unique() ...

I need to create a structure / implement these methods one inside the other, as if they were organized in directories, within a class Roll() .

The goal is for those who try to use the Roll class to be able to leave their code as close to human language as possible:

ApplyDamage (Physical (Roll (5,d6).Sum.OnlyEvens.Above(2)), ChosenTarget);   

Example:

using System.Linq;
using System.Collections;
using System.Collections.Generic;

public class Roll
{
    List<int>   scores;
    int         maxRange;
    public void Roll(int amountOfDices, int maxRange)
    {
        this.maxRange = maxRange;
        for (int i = 0; i < amountOfDices; i++)
        {
             scores.Add(Random.Range(1,maxRange));
        }
    }//Construtor

    public int Sum (scores)//este é o MEU Sum
    {
        public int OnlyEvens(scores){
            public int Above(scores, int threshold)
            {
                return(Evens(Above(scores)).Sum());
            }
            public int Below(scores, int threshold)
            {
                return(Evens(Below(scores)).Sum());
            }
            return(Evens(scores).Sum());
        }
        return(scores.Sum());//este Sum() é proveniente do System.Linq
    }
}
    
asked by anonymous 04.04.2016 / 04:27

1 answer

1

I'll answer what you give. The question is confusing and full of errors (nor will I comment on the use of .Net 2.0 that has not been supported for a long time). It may be that the answer is not what the AP wants, but it's what you can do with what's in the question.

To chain methods there is a technique called fluent interface . When used in exaggeration it does more harm than good.

The secret to doing this is to always return the this (the object itself being manipulated), so the return of it is used as input to the next method.

But much of the code does not make sense. As the question does not clarify what needs to be done, I can not respond more adequately than this.

The Roll() constructor does not have to do anything because it already returns the object. Actually the code in the question is wrong saying that it should return void , which makes me think the question is artificial and therefore difficult to answer.

The displayed call of Sum() and its setting does not match. Actually the call does not make sense, it does not have relatives, for example. The call should be .Sum() .

OnlyEvens() and the others are methods that must be separated. Until version 6, C # does not allow methods within methods, even in version 7 its use is for something completely different. It does not even make sense there. Placing one method within another does not achieve the expected goal.

To do this would have to reform the whole class. I do not know if it pays. In order for the result to be preserved and return this , it should be placed somewhere. But then the class would probably have a given spurious just to meet a false requirement.

A basic idea:

public int Sum() {
    return scores.Sum();
}
public Roll OnlyEvens() {
    return Evens(scores); //imagino que este método existe em algum lugar e faça o esperado
}
public Roll Above(int threshold) {
    return Above(scores, threshold); //se isto não existir, precisa por o algoritmo aqui
}

Since OnlyEvens() and Above() are filters, they should be applied before summing:

ApplyDamage(Physical(Roll(5, d6).OnlyEvens().Above(2).Sum()), ChosenTarget);

A better way to do the method if I understood the purpose:

public Roll OnlyEvens() {
    scores = scores.Where(x => x % 2);
    return this; //imagino que este método existe em algum lugar e faça o esperado
}
public Roll Above(int threshold) {
    scores = scores.Where(x =>, x > threshold);
    return this;
}

Conclusion

I think there's a lot to be cleaned up before you get the expected result. It would probably be best to make it work perfectly in the traditional way and only when this occurs do you ask a question to switch to a fluent interface.

    
04.04.2016 / 13:14