Small c# challenge [modified]
-
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
1. initialize temp's properties to null 2. try { do all your Get*() calls } or, write Get*Default functions which do the null test and assign either the recordset value, or a default value in case the field is null.
-
Sorry, some characters got swallowed. The code will not compile.
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
Ah, yeah, sometimes the ternary is a bear because of the typing. Cast using (double?) instead I believe. Really, however, if this is to migrate into a programming question it should not be in the lounge. The logic is inferrable from what I wrote.
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. -
Ah, yeah, sometimes the ternary is a bear because of the typing. Cast using (double?) instead I believe. Really, however, if this is to migrate into a programming question it should not be in the lounge. The logic is inferrable from what I wrote.
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.Nice solution. My solution is similar but a little bit different.
Ennis Ray Lynch, Jr. wrote:
Really, however, if this is to migrate into a programming question it should not be in the lounge.
I don't think it's a programming question as I already had solution when I posted it.
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
-
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.;-) Happens to the best of us, and even me occasionally :laugh:
-
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?
cpkilekofp wrote:
But I have to ask: have you tried it, and observed it to work?
It works because all the fields are objects, unless you use a typed data reader. Marc
-
rd[0] returns object, not double.
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
Giorgi Dalakishvili wrote:
rd[0] returns object, not double.
Yes, I know. What did you mean? I was being flippant, because you obviously have to deal with converting the object to a double at some point. But it was a way to get rid of the "if". Marc
-
Giorgi Dalakishvili wrote:
rd[0] returns object, not double.
Yes, I know. What did you mean? I was being flippant, because you obviously have to deal with converting the object to a double at some point. But it was a way to get rid of the "if". Marc
Sorry I didn't notice changes you have made :)
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
-
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
-
Giorgi Dalakishvili wrote:
rd[0] returns object, not double.
Yes, I know. What did you mean? I was being flippant, because you obviously have to deal with converting the object to a double at some point. But it was a way to get rid of the "if". Marc
I knew that flippant was a word. Yet for some reason I always had lingering self-doubt when using it.
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.It would be nice if this worked in VB.NET... Unfortunately the VB ternary operator (IIF) has a nasty habit of evaluating both the true and false statements instead of short-circuiting directly to one option or the other like a logical language does.
Imagine that you are hired to build a bridge over a river which gets slightly wider every day; sometimes it shrinks but nobody can predict when. Your client provides no concrete or steel, only timber and cut stone (but they won't tell you what kind). The coefficient of gravity changes randomly from hour to hour, as does the viscosity of air. Your only tools are a hacksaw, a chainsaw, a rubber mallet, and a length of rope. Welcome to my world. -Me explaining my job to an engineer
-
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
Somehow is one word.
Todd Smith
-
I knew that flippant was a word. Yet for some reason I always had lingering self-doubt when using it.
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.Dictionary? :~ They probably even have them on the Internet.
BDF People don't mind being mean; but they never want to be ridiculous. -- Moliere
-
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
Giorgi Dalakishvili wrote:
One line solution
What's a line?
-
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
Which is why it should be hidden.