c# - TypeInitializationException on a struct with no constructor -
i typeinitializationexception
. inner exception dividebyzeroexception
. looking @ stack trace inner exception, this:
à system.decimal.fcalldivide(decimal& d1, decimal& d2) à system.decimal.op_division(decimal d1, decimal d2) à dimensionneljdp.incertitudeobject..cctor() dans \incertitudeobject.cs:ligne 108
the code triggers exception this:
incertitudeobject io = new incertitudeobject(); io.compute_e( config, ( (empilementobject)row[ "empilement" ] ).pile ); //exception here
my investigations on issue lead me see that, when inside io
, there bunch of question marks instead of bunch of variables.
the same exception happens regardless of function call (including when calling io.tostring()
), , io
full of question marks.
i figured incertitudeobject
doesn't initialized correctly, though how beyond me. doesn't have constructor, i'm assuming uses default implicit parameterless constructor of language. really, don't know happening here.
it working fine last week, there wasn't significant changes can remember. same setup works on other projects of solution.
here quick @ struct:
public struct incertitudeobject { private decimal ua; public decimal ua{ { return ua; } } //and dozen more private static decimal[] ref_ua_r = { 4.1m / 1000.0m, 8.2m / 1000.0m, 0.0m }; //this line 108 stack trace points //and there dozen more //bunch of functions things never called when exception happens //distinct lack of constructor }
edit: found problem. didn't know whole static constructor thing. apparently, order of static field important. had switch from:
private static decimal[] ref_ub2_e = { r2 * 10.0m / ( 2.0m * r3 ) / 1000.0m, 20.0m / ( 2.0m * r3 ) / 1000.0m, 50.0m / ( 2.0m * r3 ) / 1000.0m }; private static decimal r2 = (decimal)math.sqrt( 2 ); private static decimal r3 = (decimal)math.sqrt( 3 );
to:
private static decimal r2 = (decimal)math.sqrt( 2 ); private static decimal r3 = (decimal)math.sqrt( 3 ); private static decimal[] ref_ub2_e = { r2 * 10.0m / ( 2.0m * r3 ) / 1000.0m, 20.0m / ( 2.0m * r3 ) / 1000.0m, 50.0m / ( 2.0m * r3 ) / 1000.0m };
in hindsight, should have seen this.
your exception in static constructor. did not write 1 explicitely, 1 generated hold initialization of static fields.
private static decimal[] ref_ua_r = { 4.1m / 1000.0m, 8.2m / 1000.0m, 0.0m };
the code in initializers placed in static constructor compiler.
while particular line wont throw dividebyzeroexception, line apparently is.
Comments
Post a Comment