Simplify this code
-
if(!reader.IsDBNull(4))
{
c.SourceVideoClip=reader.GetString(4).Trim();
}Can the above code, be refactored to use the tertiary operator?
/\ |_ E X E GG
-
Does this work:
c.SourceVideoClip= (!reader.IsDBNull(4)) ? c.SourceVideoClip = reader.GetString(4).Trim() : null;
/\ |_ E X E GG
No, when false no assignment should occur. Where did the null come from ? The one possibility I see is: c.SourceVideoClip= (!reader.IsDBNull(4)) ? reader.GetString(4).Trim() : c.SourceVideoClip; but even that is not absolutely correct; in several situations it could differ from the original (when the condition is false): when c is null; when SourceVideoClip is a property without a getter; when it is a property with side effects in its getter; when it is a property that is not returning what you set it to (you should not do this, but you could...) when it is volatile, i.e. other threads (or hardware) may cause it to change; There may be a better attempt if something more was known about the c type ! :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
if(!reader.IsDBNull(4))
{
c.SourceVideoClip=reader.GetString(4).Trim();
}Can the above code, be refactored to use the tertiary operator?
/\ |_ E X E GG
Sure SELECT param1, param2, param3, param4, ISNULL(SourceVideoClip, '') FROM MyTable c.SourceVideoClip=reader.GetString(4).Trim();
only two letters away from being an asset