Dealing with multiple booleans [closed] - c#

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 days ago.
This post was edited and submitted for review 2 days ago.
Improve this question
I have a Customer data object. That object has to be evaluated by different functions. Each function will return true or false.
Since each function evaluate different case scenarios, I need to submit the Customer object to each one of them.
The way the functions were written, only one of them is going to return true.
(The functions are used in another part of the code, so I am using them as it is, so I don't have to repeat code).
The WorkStatus is an Enum. So, if the function returns true, then the Enum will be set with the specified value.
So:
WorkStatus result = 0;
if (notStarted) result = WorkStatus.NotStarted;
if (started) result = WorkStatus.Started;
if (inProgress) result = WorkStatus.InProgress;
if (withBacklog) result = WorkStatus.WithBacklog;
if (finalized) result = WorkStatus.Finalized;
return (int)result;
I made this way, but I am sure there is some better way. I just can't figure this out.
Obs.: I wrote the question again. I hope it's more clear. Sorry for the previous lack of details.

Related

How to check array for a value in C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to help someone out and this isn't my area of expertise so thought maybe someone can help me help someone else.
I have a field called Master that contains an array. I also have a field called Original that contains a string. I want to check if the string in Original is in the array field called Field1 and then with an if statement do something if true / false
"Original":"1234",
"Master":[{"ID":1,"Field1":12345},
{"ID":2,"Field1":123456},
{"ID":3,"Field1":1234},
{"ID":4,"Field1":12344}]
The array can be different each time and have a different amount of records in it.
Can anyone help me?
if Original and Master would be properties of the same class and in a variable named instance then you could use Linq to do:
bool isPresent = instance.Master.Any(entry => entry.Field1 == instance.Original);
Obviously you would need to first serialize the json to an instance of this class.

What is MAX function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
var approve = UnitOfWork.Query.Lexis.ApprovedLexis2(DialogService.User.UserID, List.Where(x=>x.Check).Max(x=>x.TxnDate), _batch);
what does line of code returns? can someone explains that? TYIA
List.Where(x=>x.Check).Max(x=>x.TxnDate)
Presumably here List is a list/collection/some IEnumerable<T> for a type T that has (at least) a bool member named Check, and some other member TxnDate (presumably a DateTime of the transaction date).
The Where applies a predicate filter, i.e. it creates a filtered sequence of the items where Check is true. The Max finds the greatest (in terms of x>y, implemented by IComparable[<T>]) of the .TxnDate of each item in the filtered sequence.
So: the expression returns the greatest (last, timewise) transaction date of all the "checked" items in List. If there are no "checked" items, it will throw an exception (you can't ask for the largest of no values; or rather, you can ask, but you won't get a sensible answer).

Unit Testing with ForEach [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Assuming:
"personList" is a list of Person objects
"agent" is an object that can provide activity about Shop objects (a property of Person)
"GetShopActivity" returns a list of shopping activity objects for a Person
I have this line in a test project:
personList.ForEach(p => new List<Person>(p.Shops)
.ForEach(t=> Assert.IsNotNull(agent.GetShopActivity(t, startDate, endDate))));
How can I make it better?
There is probably a lot you could do, but the first thing to do would be to make it more readable. Perhaps something like this:
var nullActivities =
from p in partnerList
from t in p.Tenants
let activity = agent.GetShopActivity(t, startDate, endDate)
where activity == null
select activity;
Assert.Empty(nullActivities);
Moreover:
you may want to think about
a test should be simple (i.e. it should have a Cyclomatic Complexity of 1).
it should be immediately evident to a person reading the test what scenario and behaviour is being tested (in case the values of startDate and endDate are significant it might be beneficial to give them less generic names).
prefer having only a single assertion as that makes it easy to know where the test failed when it fails.

why is Next() method called next? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Why does Random.Next() have Next in it's name? I know what it does, but the name doesn't seem to correspond to it.
It returns the next number in the infinite sequence of numbers generated from your Random instance's seed.
In computer science jargon, a "generator" is a specific kind of function: one that returns a different result each time it is called. It is traditional to call this function something like next(), because they are often used to return the next piece of a sequence (perhaps infinite). RNGs are just a special case of generator function, returning the next value in a calculated sequence.

what is best way to prepare C# objects in separate methods [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What is better method to use for object preparation logic:
a) with return value
List<Users> users = LoadUsers();
users = PrepareUsers(users);
b) or with void type
List<Users> users = LoadUsers();
PrepareUsers(users)
Are you setting properties on existing User objects or are you creating new ones?
If you're simply changing existing objects, then there's no reason why you'd want to return them, it's redundant. Worse, it's misleading - the client will think his objects were left untouched and that you're creating new objects when in fact you're not.
If you're creating new ones, well then, you obviously need to return them.
Alternative b. since you are working with the same user objects, there is no reason to reassign the variable.

Categories