Reputation: 129
I had a problem and I've asked around this community to assist me in solving the issue. The issue is to automatically update all the variables when a variable has been modified.
I've got tips to use lambda to automatically update the variable b
.
After this, I read a post stating about using get & set. However, even when I do something like this below, variable b
wouldn't update.
private static int b;
public int B
{
get { return b; }
set { b = a + 5; }
}
I couldn't also find a way to use lambda and get set together. Below is the code I use for testing.
class Class3
{
public class Storage
{
public static int a = 100;
//public static int b => a + 5;
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();
}
}
}
}
What I would like to see is b
updating every time a
changes value. In addition, the code shall allow the Storage.b += 1;
to update the value of b.
Is there a way to work around this? I really need help with this. If there is a link I could read, please share it with me. Thank you!
Upvotes: 1
Views: 194
Reputation: 674
Edit - This works for both directly updating the value of B and also updating it when the value of A changes.
I modified the Storage
and Methods
classes
public class Storage
{
private int _a;
public int A
{
get { return _a; }
set
{
_a = value;
B = value + 5;
}
}
public int B { get; set; }
public int C { get; set; }
public Storage()
{
A = 100;
}
}
public class Methods
{
private Storage _storage;
public Methods(Storage storage)
{
_storage = storage;
}
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();
}
}
}
Inside Main
var storage = new Storage();
Methods Test = new Methods(storage);
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);
Upvotes: 1
Reputation: 26372
Maybe something like this could work for you. You'll just need to use properties and hide the member variables.
Of course, as mentioned, I would also suggest that you don't use static variables and getters.
public static class Storage
{
private static int a = 100;
public static int A {
get { return a; }
set { a = value; b = a + 5; }
}
private static int b = a + 5;
public static int B {
get { return b; }
set { b = value; }
}
public static int c;
}
Upvotes: 1
Reputation: 311
Here it is how the class should be:
public class Storage
{
public int a { get; set; }
public int b => a + 5;
public int c { get; set; }
public void Met1()
{
this.a -= 10;
this.c = this.a;
}
public void Met2()
{
this.a -= 10;
this.c = this.a;
}
public void Met3()
{
Console.WriteLine("{0}", this.a);
this.c = this.a;
Met1();
Met2();
if (this.a > 10)
{
Met3();
}
}
}
Here it is how you can use it:
static void Main(string[] args)
{
Storage storage = new Storage();
storage.a = 100;
Console.WriteLine("Original a value: {0}", storage.a);
Console.WriteLine("b value: {0}", storage.b);
Console.WriteLine("c value: {0}", storage.c);
storage.Met1();
Console.WriteLine("After met1: {0}", storage.a);
Console.WriteLine("b value: {0}", storage.b);
Console.WriteLine("c value: {0}", storage.c);
storage.Met2();
Console.WriteLine("After met2: {0}", storage.a);
Console.WriteLine("b value: {0}", storage.b);
Console.WriteLine("c value: {0}", storage.c);
storage.Met3();
Console.WriteLine("After met3: {0}", storage.a);
Console.WriteLine("b value: {0}", storage.b);
Console.WriteLine("c value: {0}", storage.c);
Console.ReadKey();
//storage.b += 1;
//Console.WriteLine("b value: {0}", storage.b);
}
Please note that if b is equal to a + 5, you cannot assign a value to b.
Upvotes: 0