c# - How can I use Code Contracts to ensure that a non-blank, non-null val should always be returned from a method? -
this code:
private static char getbarcodechecksumwithlegacycode(string barcodewithoutczechsum) { contract.requires(!string.isnullorwhitespace(barcodewithoutczechsum)); contract.ensures(contract.result != null); . . .
...doesn't compile ("operator '!=' cannot applied operands of type 'method group' , ''").
this fails same way:
contract.ensures(contract.valueatreturn != null);
how can enforce necessity of method returning result using code contracts?
update
if this:
contract.ensures(contract.result<char>() != '');
...it fails with, "empty character literal"
so way test returned char val being both non-null , non-empty:
contract.ensures(contract.result<char>() != null && contract.result<char>().tostring() != string.empty);
...or null check suffice?
btw, trying use valueatreturn instead of result gives me "no overload method 'valueatreturn' takes 0 arguments"
update 2
this code:
contract.ensures(contract.result<string>() != null && contract.result<string>() != string.empty);
fails with:
in method barcodeczechdigittester.form1.getbarcodechecksumwithlegacycode(system.string): detected call result 'system.string', should 'system.char'.
(twice) also: "the command ""c:\program files (x86)\microsoft\contracts\bin\ccrewrite.exe" "@barcodeczechdigittesterccrewrite.rsp"" exited code 2."
...but changing code works:
contract.ensures(contract.result<char>() != null && contract.result<char>().tostring() != string.empty);
you need have like:
contract.ensures(contract.result<string>() != null);
Comments
Post a Comment