This question already has an answer here:
If condition failing with expression too complex
(1 answer)
Closed 7 years ago.
Now, I've seen this swift error posted all over stackoverflow. However, the way they handle it won't seem to work in my situation.
if (
fieldType == "checkbox" ||
fieldType == "time" ||
fieldType == "birthdate" ||
fieldType == "datetime" ||
fieldType == "phone" ||
fieldType == "payment" ||
fieldType == "stripe" ||
fieldType == "paypal" ||
fieldType == "paypalpro" ||
fieldType == "address" ||
fieldType == "2co")
{
Not sure how I can fix it. There must be some way to lower the complexity of it.
What I would do is just start with an array of literals:
let fieldType = "Howdy"
let possibilities = ["checkbox","time"] // ... add the others too...
if find(possibilities, fieldType) != nil { // or: if contains(possibilities,fieldType)
}
Related
This works but I am trying to figure out a better way to write it. I want to replace this if condition. I have multiple cases like this
if text == "a" || text == "ac" || text == "acc" || text == "acco" || text == "accou"
|| text == "accoun" || text == "account"
{
flagAccount = false
} else {
flagAccount = true
}
I thought of using CharacterSet but cant seem to figure it out
The condition is equivalent to
"account".hasPrefix(text) && text != ""
Note hat text != "" is needed because "account".hasPrefix("") is true.
As Rob suggested, you might write this in an if statement like so, using !text.isEmpty might be nit more readable:
if !text.isEmpty, "account".hasPrefix(text) {
}
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 6 years ago.
I have a table of questions and a table of answers
I always need to display the list of questions regardless and there is a corresponsing asnwer, I need to grab that answer (response)
I use the following code
var Questions = db.Questions.Where(x => x.isActive);
var Answers = db.Answers.Where(x => x.AssessmentID == 99);
AssessmentResponseVM model = new AssessmentResponseVM();
foreach (var question in Questions)
{
AnswerAndQuestions q = new AnswerAndQuestions { };
q.QuestionText = question.QuestionText;
q.QuestionID = question.ID;
q.Response = Answers.Where(a => a.QuestionID == question.ID).SingleOrDefault().Response; <--- throws exception if there is no answer for the question
model.Questions.Add(q);
}
But get this error
Object reference not set to an instance of an object.
On this line
q.Response = Answers.Where(a => a.QuestionID == question.ID).SingleOrDefault().Response;
change
q.Response = Answers.Where(a => a.QuestionID == question.ID).SingleOrDefault().Response;
to this code
q.Response=Answers.Any(a=>a.QuestionID==question.ID)?Answers.firstOrDefault(a => a.QuestionID == question.ID).Response:new Response();
q.Response = Answers.Any(a => a.QuestionID == question.ID) ? Answers.Where(a => a.QuestionID == question.ID).FirstOrDefault().Response;
If you're allowed to use C# 6.0, I would suggest to try new null-conditional operator.
q.Response = Answers.Where(a => a.QuestionID == question.ID).SingleOrDefault()?.Response;
It returns null when Answers.Where(a => a.QuestionID == question.ID).SingleOrDefault() returns default value, which is null.
If you need something else, not null, you can use null-coalescing operator.
q.Response = Answers.Where(a => a.QuestionID == question.ID).SingleOrDefault()?.Response ?? response, what you need;
row = {"joining_date"=>"18/07/2015", "name"=>" Joe Doe ", "company"=>" Google", "location"=>" New York ", "role"=>"developer", "email"=>"joe#doe.com", "mobile"=>"11-(640)123-45674", "address"=>"4 XYZ Road", "validity"=>"true"}
row is invalid only if any one of the fields(joining_date, name, company, location, email, address) is nil or not present.
def is_valid?
valid = true
if row[:name] == nil || row[:joining_date] == nil || row[:address] == nil || row[:email] == nil || row[:company] == nil || row[:location] == nil
valid = false
end
valid
end
Is there any way that I can simplify and refactor the above method in rails to find it more efficient using regex?
Probably, but I wouldn't use a regex as it's in a hash. As you're using rails you can use present? or blank?.
row.values.any?(&:blank?)
Would return true if any are blank
for your case
def is valid?
row.values.all?(&:present?)
end
I am looking for a method (preferably just one Rails or Ruby method) that will validate if a variable is blank but is not false (since I have some boolean variables that should not be blank? == true since they have values).
Say I have four types: nil, '', 0, false.
I am looking for a simple method that is equivalent of the following:
(var.nil? || var == '') && var !== false
This is the simplest I can come up with:
var.blank? && var !== false
If there's no other way, then I guess I'll just have to create a custom-method for this.
I am looking for a simple method that is equivalent of the following:
var.nil? or var == '' or var == false
The method you're looking for is blank?
def foo1(var)
var.nil? || var == '' || var == false
end
def foo2(var)
var.blank?
end
foo1(nil) == foo2(nil) # => true
foo1('') == foo2('') # => true
foo1(false) == foo2(false) # => true
foo1(0) == foo2(0) # => true
After edit
I am looking for a simple method that is equivalent of the following:
(var.nil? || var == '') && var !== false
Yeah, just make a custom method. Or, if you're feeling particularly evil, you can patch FalseClass#blank? to return the opposite value.
Now that your question became understandable, I shall answer.
(var.nil? || var == '') && var !== false
can be simplified to:
var.to_s.empty?
You cannot do it in a single method, but you can do it in two. It will also return true for [], {}, for example.
I am developing MVC app and I am using the LINQ in controller.
I am trying to get one rechord with below query, but its giving an error...
Approval oAP = new Approval();
oAP = db.Approvals.Where(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount));
Is there any wrong syntax ?
Got the answer
oAP = db.Approvals.Where(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount)).FirstOrDefault();
Change this
e.ApprovedBy.Id = loggedEmployee.Id
For
e.ApprovedBy.Id == loggedEmployee.Id
You're comparing not assigning values.
Also you may add this
oAP = db.Approvals.Where(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount)).FirstOrDefault();
Because i'm assuming that you want to return only one
Some remarks:
You should be able to drop the Where:
oAP = db.Approvals.FirstOrDefault(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount));
Personally, I try to avoid the First and FirstOrDefault functions, because if you know there is only one record and if you want to enforce this, you can use SingleOrDefault:
oAP = db.Approvals.SingleOrDefault(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount));
If you know there will always be (more than) one record, you can drop the 'OrDefault' part and use First() or Single().