TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Olivier Muhring
1.5k
262
9.5k
Can I simply my lengthy shorthand string comparison?
Dec 11 2018 7:32 AM
In one of my applications I receive a datastruct built like this:
> parent:
> {
> x,
> y,
> z,
> child[]:
> {
> x,
> y
> }
> }
I select the parent object, and then loop through all of the child objects. I use this data to create a 3rd object.
As you can see, some of the properties of the parent object can also be found in the child object. The parent object always has priority, so whenever I loop through the child objects I need to check whether or not I have a value @ the parent level for properties x & y. If I do, then I use the parent's value, if not it's the child's values that are being used.
Originally I created separate methods to these checks for me. I had one for int's, booleans and strings.
In the pull request I was asked to remove those methods and use the ? operator for my comparisons. I personally found my original way of working more readable, but OK.
Now, for the string comparisons I found that the expression to be used was somewhat lengthy:
var B = parent.X !=
null
? (!
string
.IsNullOrEmpty(parent.X.XName) ? parent.X.XName
: child.X !=
null
? (!
string
.IsNullOrEmpty(child.X.XName) ? child.X.XName
:
string
.Empty)
:
string
.Empty)
: (child.X !=
null
? !
string
.IsNullOrEmpty(child.X.XName) ? child.X.XName
:
string
.Empty;
I'm wondering if there are ways to simplify it?
Reply
Answers (
1
)
How to restore 'windows hosting' in sqlite database ..?
The resource cannot be found