Is possible to loop through an object’s properties and find which ones are null without allot of if statements?
One could using reflection. But you probably want to handle this a different way. What are you trying to do?
object properties get populated from a database and some of are null.
I want to display the properties that aren’t null on my page without using a bunch if statements to see if they’re null / empty.
if not string.isnullorempty(prop.name) then
'some code
end if
if not string.isnullorempty(prop.name2) then
'some code
end if
is there an easier way?
In this context, I don’t see other way.
Can you use the Null Coalescing Assignment Operator (??).
edit - Oh, sorry. I’m thinking in C#.
Slightly easier might be to make a method that doesn’t output the row if things are null, would at least keep you out of a massive number of If/Then statements.
Thanks!
I’ll try that!
Depending on the circumstances it’s sometimes useful to sort this out in sql using the isnull method:-
select id, isnull(stringfield1, ‘’), isnull(intfield1, 0) … and so on.
Why not add a WHERE clause?
SELECT * FROM table WHERE field<>‘NULL’;
Because he doesn’t want to ignore the rows containing NULL values, only to handle the null fields