-
Notifications
You must be signed in to change notification settings - Fork 51
boolean guard question #30
Comments
Hi, I'm glad you liked the library.
var a = 1;
var b = 2;
// The following lambda expression is a MemberExpression that represents the variable "a".
// Guard.Argument compiles that expression to get the value, 1.
Guard.Argument(() => a);
// The following lambda expression is a BinaryExpression that adds "a" and "b".
// It doesn't represent a single parameter which Guard can get the value and name of.
Guard.Argument(() => a + b); If your method accept multiple arguments, you need to have a If you're sure that you want to threat a combination of arguments as a single argument, you can use the void Foo(bool a, bool b)
{
// Here we specify a combination of values, and a custom name
// to be included in exception messages.
Guard.Argument(a || b, "a or b").True();
} Update: However, I would recommend this approach instead: void Foo(bool a, bool b)
{
// Here, both a and b can be false if the other one is not.
// Since "a" is the first argument, we'll blame "b" if it's not consistent with "a".
if (!a)
Guard.Argument(() => b).True("b can only false when a is true.");
} I hope this helps, please let me know if you have any more questions. |
ah so the 'e' in question that it's complaining about is the name of the argument? Yep that helps. The check was to make sure a particular JSON fragment had one of two options in it or not. A sanity check basically. I appreciate the help! |
Kind of.
But when you write Is the JSON fragment a Newtonsoft JObject that you accept as a parameter? Maybe you can use member guards to validate it. But I guess writing a "JSON to model" parser function and using I hope this clarifies things. |
I am a big fan of doing checks on every argument that comes into a given function. I'd rather have something immediately throw an exception when given invalid data than find out ten functions in that my X value is invalid. Unfortunately if not then assert throw exception checks are very spammy. As a dyslexic, Dawn.Guard is awesome.
But I'm having some trouble validating a simple boolean.
throws an exception:
A member expression is expected.
Parameter name: e
at Dawn.Guard.Argument[T](Expression
1 e, Boolean secure) in C:\Users\ekramer\Documents\Visual Studio 2017\Projects\guard\src\Guard.Argument.cs:line 61 at Dawn.Tests.BooleanTests.GuardSupportsBooleans(Nullable
1 true, Nullable`1 false) in C:\Users\ekramer\Documents\Visual Studio 2017\Projects\guard\tests\BooleanTests.cs:line 13I went so far as to clone the repository so I could examine the unit tests for booleans and the very next line of
Guard.Argument(() => @true).True();
works just fine. The only difference is that @true is a nullable boolean.
Could someone explain what I'm doing wrong here?
thanks,
E
The text was updated successfully, but these errors were encountered: