The Artima Developer Community
Sponsored Link

.NET Buzz Forum
The C# ?: operator

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
Peter van Ooijen

Posts: 284
Nickname: petergekko
Registered: Sep, 2003

Peter van Ooijen is a .NET devloper/architect for Gekko Software
The C# ?: operator Posted: Jul 15, 2005 9:13 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Peter van Ooijen.
Original Post: The C# ?: operator
Feed Title: Peter's Gekko
Feed URL: /error.htm?aspxerrorpath=/blogs/peter.van.ooijen/rss.aspx
Feed Description: My weblog cotains tips tricks and opinions on ASP.NET, tablet PC's and tech in general.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Peter van Ooijen
Latest Posts From Peter's Gekko

Advertisement

Often I work for people with a Delphi background to introduce them to C#. No problem until we reach the conditional operator ?: The definition in the docs is cond-expr ? expr1 : expr2  but the examples are not that inspiring. The most common reaction is "we're not going to use that". Recently I found a very good way to use and demonstrate it. Let me share this.

I'm working on an application which works with an existing database. Jeremy Miller has a lovely story on those kind of animals. Mine is not to bad, I have an occasional WTF but not on a daily base. To keep the DB as far away as possible I designed a  couple of strongly typed datasets and built a set of components to fill these. Instead of an alien database I now have my dataset. But I'm still not sure what's inside. That's where the ?: comes to my rescue. Take this code

Indato.Data.Schemas.School.OPLOVKRow dr = OPLOVK.OPLOVK[0];
TextBoxOmschrijving.Text = dr.IsOPMERKINGNull() ? "" : dr.OPMERKING;
 

dr is a strong typed row. The problem is that I can never be sure if it doesn't contain a null value. Trying to set the Text property of a Textbox to null will throw an exception. The typed dataset has an IsxxxNull method for every field. This is a boolean test, if it evaluates to true the empty string after the ? is used, if it evaluated to false the contents of the field, that's the expression after the :, is used .

Fits the docs and very worthwhile using.

Read: The C# ?: operator

Topic: [INETA] Community Launch Visual Studio 2005 + SQL Server 2005 Previous Topic   Next Topic Topic: Telerik r.a.d.Dock

Sponsored Links



Google
  Web Artima.com   

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