I'm trying to calculate the values when one variable has another variable in the formula. I've used the lambda approach as suggested by @Rain366, however, this approach has a limitation and I received this error,
Error CS0200 Property or indexer 'Class3.Storage.b' cannot be assigned to -- it is read only
.
I've tried using the get and set approach public static int b { get; set; } => a + 5;
, but I believe the method of doing it is wrong. Is there a way to use lambda to write values or possibly merge it with a setter?
What I would like to see is the value of b constantly updating itself (therefore I've used the lambda approach) and the flexibility of editing the value of b by using something like Storage.b++;
Thank you!
Code as shown below.
class Class3
{
public class Storage
{
public static int a = 100;
public static int b => a + 5;
public static int c;
}
public static void Main()
{
Methods Test = new Methods();
Console.WriteLine("Original a value: {0}", Storage.a);
Console.WriteLine("b value: {0}", Storage.b);
Console.WriteLine("c value: {0}", Storage.c);
Test.Met1();
Console.WriteLine("After met1: {0}", Storage.a);
Console.WriteLine("b value: {0}", Storage.b);
Console.WriteLine("c value: {0}", Storage.c);
Test.Met2();
Console.WriteLine("After met2: {0}", Storage.a);
Console.WriteLine("b value: {0}", Storage.b);
Console.WriteLine("c value: {0}", Storage.c);
Test.Met3();
Console.WriteLine("After met3: {0}", Storage.a);
Console.WriteLine("b value: {0}", Storage.b);
Console.WriteLine("c value: {0}", Storage.c);
Storage.b += 1;
Console.WriteLine("b value: {0}", Storage.b);
}
public class Methods
{
public void Met1()
{
Storage.a -= 10;
Storage.c = Storage.a;
}
public void Met2()
{
Storage.a -= 10;
Storage.c = Storage.a;
}
public void Met3()
{
Console.WriteLine("{0}", Storage.a);
Storage.c = Storage.a;
Met1();
Met2();
if (Storage.a > 10)
{
Met3();
}
}
}
}