0

my x1 value is not available

namespace revisoes { class Correct { private double x1, x2; static void Main() { Correct x1 = new Correct(); x1 = 100; Console.WriteLine(x1); } } }

18th Jan 2018, 1:30 PM
Efraim IĂ©
Efraim IĂ© - avatar
5 Answers
+ 11
@Ipang No sorry! It's because the program entry point (main) is within the same class as the instance variable. It wouldn't work if the variable is from another class. 😉
19th Jan 2018, 6:09 AM
Zephyr Koo
Zephyr Koo - avatar
+ 10
You got 2 definitions of x1 here, one for a double-precision value and one for the class object itself. Perhaps you can fix it by following:- Correct obj = new Correct(); obj.x1 = 100; Console.WriteLine(obj.x1);
18th Jan 2018, 3:20 PM
Zephyr Koo
Zephyr Koo - avatar
+ 9
@Zephyr ooh I see now : ) Thank you for the explanation, learned a new thing from this, will make a note on that : )
19th Jan 2018, 6:23 AM
Ipang
+ 8
@Ipang You're welcome! 😄
20th Jan 2018, 3:25 AM
Zephyr Koo
Zephyr Koo - avatar
+ 7
@Zephyr, I am sorry, I don't really understand why the private x1 here is accessible to be exposed through an instance as if it were public?
18th Jan 2018, 4:01 PM
Ipang