Small c# challenge [modified]
-
Let's say you have this class:
class myClass
{
public double? Foo;
}and this piece of code:
myClass temp=new myClass()
SqlDataReader rd = GetDataSomeHow();if(rd.IsDBNull(0))
temp.Foo = null;
else
temp.Foo = rd.GetDouble(0);Rewrite the above statement without if. [Edit] One line solution Sorry if it's too easy for you :) Here is the solution in case you can't solve it: Solution[^]
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
modified on Thursday, October 23, 2008 11:23 AM
-
Let's say you have this class:
class myClass
{
public double? Foo;
}and this piece of code:
myClass temp=new myClass()
SqlDataReader rd = GetDataSomeHow();if(rd.IsDBNull(0))
temp.Foo = null;
else
temp.Foo = rd.GetDouble(0);Rewrite the above statement without if. [Edit] One line solution Sorry if it's too easy for you :) Here is the solution in case you can't solve it: Solution[^]
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
modified on Thursday, October 23, 2008 11:23 AM
Tertiary operator seems like too easy a solution... Peace!
-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles -
Let's say you have this class:
class myClass
{
public double? Foo;
}and this piece of code:
myClass temp=new myClass()
SqlDataReader rd = GetDataSomeHow();if(rd.IsDBNull(0))
temp.Foo = null;
else
temp.Foo = rd.GetDouble(0);Rewrite the above statement without if. [Edit] One line solution Sorry if it's too easy for you :) Here is the solution in case you can't solve it: Solution[^]
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
modified on Thursday, October 23, 2008 11:23 AM
What BS. A tertiary operator is just like an if. [edit] And the fact that nullable types didn't resolve the mismatch between null and DBNull pisses me off to no end.[/edit] [edit2] And yeah, you can really get rid of the if this way:
object Foo;
...
temp.Foo=rd[0];:laugh: Marc
-
Let's say you have this class:
class myClass
{
public double? Foo;
}and this piece of code:
myClass temp=new myClass()
SqlDataReader rd = GetDataSomeHow();if(rd.IsDBNull(0))
temp.Foo = null;
else
temp.Foo = rd.GetDouble(0);Rewrite the above statement without if. [Edit] One line solution Sorry if it's too easy for you :) Here is the solution in case you can't solve it: Solution[^]
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
modified on Thursday, October 23, 2008 11:23 AM
try
{
temp.Foo = rd.GetDouble(0);
}
catch (Exception e)
{
temp.Foo = null;
}that's assuming GetDouble will throw an exception on null.
-
Let's say you have this class:
class myClass
{
public double? Foo;
}and this piece of code:
myClass temp=new myClass()
SqlDataReader rd = GetDataSomeHow();if(rd.IsDBNull(0))
temp.Foo = null;
else
temp.Foo = rd.GetDouble(0);Rewrite the above statement without if. [Edit] One line solution Sorry if it's too easy for you :) Here is the solution in case you can't solve it: Solution[^]
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
modified on Thursday, October 23, 2008 11:23 AM
temp.Foo = rd[0] == DbNull.Value ? null : (double)rd[0]
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
What BS. A tertiary operator is just like an if. [edit] And the fact that nullable types didn't resolve the mismatch between null and DBNull pisses me off to no end.[/edit] [edit2] And yeah, you can really get rid of the if this way:
object Foo;
...
temp.Foo=rd[0];:laugh: Marc
Me too.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
temp.Foo = rd[0] == DbNull.Value ? null : (double)rd[0]
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.you could also rewrite the query
SELECT
isnul(foo, '')
FROM
whereverNeed software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
What BS. A tertiary operator is just like an if. [edit] And the fact that nullable types didn't resolve the mismatch between null and DBNull pisses me off to no end.[/edit] [edit2] And yeah, you can really get rid of the if this way:
object Foo;
...
temp.Foo=rd[0];:laugh: Marc
Marc Clifton wrote:
And yeah, you can really get rid of the if this way: object Foo;...temp.Foo=rd[0];
That's what I would have thought from the C# 2.0 spec. But I have to ask: have you tried it, and observed it to work?
-
you could also rewrite the query
SELECT
isnul(foo, '')
FROM
whereverNeed software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.Ennis Ray Lynch, Jr. wrote:
you could also rewrite the query SELECT isnul(foo, '')FROM wherever
Oh, yes, I ALWAYS want a VARCHAR(0) returned when I'm expecting a double :laugh:
-
Ennis Ray Lynch, Jr. wrote:
you could also rewrite the query SELECT isnul(foo, '')FROM wherever
Oh, yes, I ALWAYS want a VARCHAR(0) returned when I'm expecting a double :laugh:
I wasn't paying any attention. I just squeezed in an emergency production db fix using that exact code so I naturally used the ''.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
you could also rewrite the query
SELECT
isnul(foo, '')
FROM
whereverNeed software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.You could also write a set of extension methods for DbDataReader eg public static double? GetDoubleOrDefault(this DbDataReader src, int index) { return GetDoubleOrDefault(src, index, 0); } public static double? GetDoubleOrDefault(this DbDataReader src, int index, double? def) { return this.IsDbNull(index) == true ? def : this.GetDouble(index) } then your example might be: temp.Foo = rd.GetDoubleOrDefault(0); or temp.Foo = rd.GetDoubleOrDefault(0, 0);
James Simpson Web Developer imebgo@hotmail.com P S - This is what part of the alphabet would look like if Q and R were eliminated
Mitch Hedberg -
You could also write a set of extension methods for DbDataReader eg public static double? GetDoubleOrDefault(this DbDataReader src, int index) { return GetDoubleOrDefault(src, index, 0); } public static double? GetDoubleOrDefault(this DbDataReader src, int index, double? def) { return this.IsDbNull(index) == true ? def : this.GetDouble(index) } then your example might be: temp.Foo = rd.GetDoubleOrDefault(0); or temp.Foo = rd.GetDoubleOrDefault(0, 0);
James Simpson Web Developer imebgo@hotmail.com P S - This is what part of the alphabet would look like if Q and R were eliminated
Mitch HedbergI like that. Keeps the code nice and readable.
Simon
-
Let's say you have this class:
class myClass
{
public double? Foo;
}and this piece of code:
myClass temp=new myClass()
SqlDataReader rd = GetDataSomeHow();if(rd.IsDBNull(0))
temp.Foo = null;
else
temp.Foo = rd.GetDouble(0);Rewrite the above statement without if. [Edit] One line solution Sorry if it's too easy for you :) Here is the solution in case you can't solve it: Solution[^]
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
modified on Thursday, October 23, 2008 11:23 AM
I wouldn't use the tertiary operator in this case either, most times I use it to avoid a local variable:
System.Console.Writeline ( rd [ 0 ] == System.DBNull.Value ? "null" : rd [ 0 ].ToString() ) ;
In this case I'd initialize Foo to null in the class (shouldn't it be anyway?) and then:if ( rd [ 0 ] != System.DBNull.Value )
{
temp.Foo = (double) rd [ 0 ] ;
}But this kind of stuff belongs encapsulated in something, perhaps pass the DataReader to the constructor of myClass and hide the details there. This is why my wrapper for ExecuteScalar requires the user to pass in a value to use in case of null.
public virtual T ExecuteScalar<T> ( T IfNull ) { lock ( this.cmd ) { **T result = IfNull ;** bool wasclosed = this.IsClosed ; try { object temp ; if ( wasclosed ) { this.Open() ; } temp = this.cmd.ExecuteScalar() ; **if ( ( temp != null ) && ( temp != System.DBNull.Value ) ) { result = (T) temp ; }** } catch ( System.Exception err ) { throw ( WrapDataException ( "ExecuteScalar" , this.cmd , err ) ) ; } finally { if ( wasclosed ) { this.Close() ; } } return ( result ) ; } }
-
What BS. A tertiary operator is just like an if. [edit] And the fact that nullable types didn't resolve the mismatch between null and DBNull pisses me off to no end.[/edit] [edit2] And yeah, you can really get rid of the if this way:
object Foo;
...
temp.Foo=rd[0];:laugh: Marc
rd[0] returns object, not double.
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
-
you could also rewrite the query
SELECT
isnul(foo, '')
FROM
whereverNeed software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.In that case how will you know if the database contained zero or it was null and isnull turned it into zero?
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
-
In that case how will you know if the database contained zero or it was null and isnull turned it into zero?
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
That is why I usually use the ternary operator. However, if you are using .NET 1.0 or 1.1 my solution is valid for your question.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
try
{
temp.Foo = rd.GetDouble(0);
}
catch (Exception e)
{
temp.Foo = null;
}that's assuming GetDouble will throw an exception on null.
What if you have several properties in your class? Code will get very ugly.
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
-
temp.Foo = rd[0] == DbNull.Value ? null : (double)rd[0]
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'double' Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
modified on Thursday, October 23, 2008 11:33 AM
-
Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'double' Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
modified on Thursday, October 23, 2008 11:33 AM
Follow the subthread I have already responded to this, it was a mistake on my part and should have been isnull(foo, 0) or more fully isnull(foo, cast(0 as float))
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
Follow the subthread I have already responded to this, it was a mistake on my part and should have been isnull(foo, 0) or more fully isnull(foo, cast(0 as float))
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.Sorry, some characters got swallowed. The code will not compile.
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion