Problems with C # variables

-1

I would like to know if it is possible to update or refresh (or something like that) a variable, without assigning new values. For example. I declare a vector and give it values, then declare a variable. N contains a formula. vector [0] = 2; vector [1] = 10; N = vector [i] * 10; At a certain moment I want that i = 1; Fx = N; and I want the value that N has to be updated before being stored.

    
asked by Adrián Avilés Arcos 21.07.2018 в 16:43
source

1 answer

2

There is no such thing as what you ask for, but you can have properties that return the computed value.

Option 1

It occurs to me that you can have a generic class that receives a function as a formula:

public class Formula<TResult>
{
    public Formula(Func<TResult> formula)
    {
        Value = formula;
    }

    // cada vez que invoques a la propiedad Value, se ejecuta la función 
    // que pasaste por parámetro a la clase
    public TResult Value { get; }
}

And to use it:

int value = 5; // el valor original
// en este caso, el '<int>' en el constructor le dice de que tipo es el valor que regresa la fórmula
var N = new Formula<int>(() => value * 10); 
var result1 = N.Value; // result1 = 50
value = 10;
var result2 = N.Value; // result2 = 100

If you want to avoid putting the data type of the result of the formula, that is, removing the "" in "new Formula ", you can create a static class with a factory type method :

public static class Formula
{
    public static Formula<TResult> From<TResult>(Func<TResult> formula)
    {
        return new Formula<TResult>(formula);
    }   
}

And so you do not have to write the data type of the result of the formula since the compiler will infer it:

int value = 5;
var N = Formula.From(() => value * 10);
var result1 = N.Value; // result1 = 50
value = 10;
var result2 = N.Value; // result2 = 100

If your formula is more complex, you have two options:

var N = Formula.From(() => {
    int result;

    // aquí irían todas las líneas de código 
    // para calcular el resultado

    return result;
});

ó:

var N = Formula.From(Metodo);
.
.
.
.
private int Metodo()
{
    int result;

    // aquí irían todas las líneas de código 
    // para calcular el resultado

    return result;
}

Option 2

If you want vectors, you can have a class that receives a vector and a formula, example;

public class Formula<T, TResult>
{
    readonly T[] vector;
    readonly Func<T, TResult> formula;

    public Formula(T[] vector, Func<T, TResult> formula)
    {
        this.vector = vector;
        this.formula = formula;
    }

    public TResult this[int index] => formula(vector[index]);
}

And to use it:

var vector = new[] { 2, 10 };
var N = new Formula<int, int>(vector, i => i * 10);

var value1 = N[0]; // value1 = 20
var value2 = N[1]; // value2 = 100

vector[0] = 4;
vector[1] = 20;

var value3 = N[0]; // value3 = 10
var value4 = N[1]; // value4 = 200

And as in the first class, you can create a static class with a static factory type method, I'm going to reuse the previous one:

public static class Formula
{
    // método tipo factory ya creado anteriormente
    public static Formula<TResult> From<TResult>(Func<TResult> formula)
    {
        return new Formula<TResult>(formula);
    }   

    // método factory para la clase Formula de vectores:
    public static Formula<T, TResult> From<T, TResult>(T[] vector, Func<T, TResult> formula)
    {
        return new Formula<T, TResult>(vector, formula);
    }
}

Finally, with those classes, the value that returns the "formula" does not necessarily have to be the same type as the value of the original data, an example:

int meses = 1;
var N = Formula.From(() => DateTime.Today.AddMonths(meses));
var mesSiguiente = N.Value; // mesSiguiente  = la fecha un mes adelante
meses = 5;
var enCincoMeses = N.Value; // enCincoMeses = la fecha 5 meses adelante
    
answered by 21.07.2018 в 18:38