The Artima Developer Community
Sponsored Link

.NET Buzz Forum
C# Teaser - The Tricky Ternary Teaser?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jonathan Cogley

Posts: 56
Nickname: thycotic
Registered: Dec, 2004

Jonathan Cogley is the founder of Thycotic Software Ltd, a C# MVP and TDD evangelist.
C# Teaser - The Tricky Ternary Teaser? Posted: Dec 26, 2004 7:01 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Jonathan Cogley.
Original Post: C# Teaser - The Tricky Ternary Teaser?
Feed Title: Jonathan Cogley's Blog
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/jcogley/Rss.aspx
Feed Description: C#, Test Driven Development, Remote Scripting, CShark! :)
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Jonathan Cogley
Latest Posts From Jonathan Cogley's Blog

Advertisement

Today we came across a little quirk that is worth sharing. Big thanks to Bob Flanders and Jeff Schoolcraft for help in figuring out the quirk.

The Rules:

  1. Prize (a Thycotic keyring light) to be mailed to the first comment with the correct answer(s) on this blog post with valid contact information.
  2. My definition of the problem is the correct one. :)  Other answers might be right but won't win the prize.
  3. I will post the answer within a few days.

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace Teasers

{

    public class TernaryTeaser

    {

        private object GetValue(Control o)

        {

            return (o is TextBox && ((TextBox)o).Text.Trim() != "") ?

                 ((TextBox) o).Text : System.DBNull.Value;

        }

    }

}

  1. What is the problem and what is the easiest way to fix it?
  2. Why do ternaries work this way - is there something at the compiler or IL level?

The first person to successfully answer part (1) gets a hearty pat on the back, the first person to explain part (2) gets a Thycotic keyring light.

UPDATE: Thanks to Kirk and Joseph for spotting the logic error - not what I was looking for, just my silly oversight (where's a test when you need one!) - I have updated the code to make more sense.

Winner: Jonathan de Halleux

Answer & Discussion (click and drag your mouse to see the answer)

Answer:
The simplest fix I was looking for is casting the System.DBNull.Value to an object:

 return (o is TextBox && ((TextBox)o).Text.Trim() != "") ? 
((TextBox) o).Text : (object) System.DBNull.Value;
For a full explanation of the ternary/conditional operator, read the docs.
For the answer to part (2), see Jonathan de Halleux's awesome detailed comment below.

Read: C# Teaser - The Tricky Ternary Teaser?

Topic: Neue Extremsportart aus USA: extreme conferencing... Previous Topic   Next Topic Topic: Good News !

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use