C # does not accept c1, c2, c3 as defined vars

if(API>30) { double c1 = 0.0178; double c2 = 1.187; double c3 = 23.931; } else { double c1 = 0.0362; double c2 = 1.0937; double c3 = 25.7240; } double Rs2 = (c1 * sg * Math.Pow(p, c2)) * Math.Exp(c3 * (API / T)); 

C # does not accept c1 , c2 , c3 in my code as certain vars, how can I solve this?

+4
source share
2 answers

Each variable has a scope . When a variable in c is defined in a block (which means between { and a } ), it is limited to this area, that is, it can only be referenced in this area. Therefore, in the last line, you are outside the scope of c1 , c2 , c3 and cannot refer to them.

You need to define them outside the block:

 double c1, c2, c3; if(API>30) { c1 = 0.0178; c2 = 1.187; c3 = 23.931; } else { c1 = 0.0362; c2 = 1.0937; c3 = 25.7240; } double Rs2 = (c1 * sg * Math.Pow(p, c2)) * Math.Exp(c3 * (API / T)); 
+13
source

Just declare them from the if/else scope.

 double c1, c2, c3; if(API>30) { c1 = 0.0178; c2 = 1.187; c3 = 23.931; } else { c1 = 0.0362; c2 = 1.0937; c3 = 25.7240; } double Rs2 = (c1 * sg * Math.Pow(p, c2)) * Math.Exp(c3 * (API / T)); 
+5
source

Source: https://habr.com/ru/post/1493160/


All Articles