Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm a little unclear as to the implementation of your above property set method as a decimal can never be null so this test is redundant I think. I'm including some sample code that can be dropped into a Console application that should clear things up for you. </p> <p>You will experience very little refactoring of your code due to switching over to the nullable data types. This will be a good move on your part in cleaning up your code and avoiding the potential pitfalls of your current implementation.</p> <p>If nothing else, the performance gains you'll receive would likely make the effort worth your while. Nothing will be completely plug-n-play so to speak but if you look at the below code you'll see there's very little impact in the scenarios you've provided. </p> <h2>Example</h2> <pre><code>using System; using System.Data.SqlClient; namespace NullableTypes { class Program { static class Constants { public static decimal NullDecimal { get { return decimal.MinValue; } } } public class ProductTheOldWay { public string Name { get; set; } public decimal UnitPrice { get; set; } public ProductTheOldWay() { Name = string.Empty; UnitPrice = Constants.NullDecimal; } public override string ToString() { return "Product: " + Name + " Price: " + ((UnitPrice == Constants.NullDecimal) ? "Out of stock" : UnitPrice.ToString()); } public void Save() { //Datalayer calls and other props omitted var sqlParm = new SqlParameter { Value = (UnitPrice == Constants.NullDecimal) ? DBNull.Value : (object)UnitPrice }; //save to the database... Console.WriteLine("Value written to the database: " + sqlParm.Value); } } public class ProductTheNewWay { public string Name { get; set; } public decimal? UnitPrice { get; set; } public ProductTheNewWay() { Name = string.Empty; } public override string ToString() { return "Product: " + Name + " Price: " + ((UnitPrice.HasValue) ? UnitPrice.ToString() : "Out of stock"); } public void Save() { //Datalayer calls and other props omitted var sqlParm = new SqlParameter { Value = UnitPrice }; //save to the database... Console.WriteLine("Value written to the database: " + sqlParm.Value); } } static void Main() { var oldProduct1 = new ProductTheOldWay { Name = "Widget", UnitPrice = 5.99M }; var oldProduct2 = new ProductTheOldWay { Name = "Rare Widget", UnitPrice = Constants.NullDecimal // out of stock }; Console.WriteLine(oldProduct1); Console.WriteLine(oldProduct2); Console.WriteLine("Saving..."); oldProduct1.Save(); oldProduct2.Save(); Console.ReadLine(); var newProduct1 = new ProductTheNewWay { Name = "Widget", UnitPrice = 5.99M }; var newProduct2 = new ProductTheNewWay { Name = "Rare Widget" /* UnitPrice = null by default */ }; Console.WriteLine(newProduct1); Console.WriteLine(newProduct2); Console.WriteLine("Saving..."); newProduct1.Save(); newProduct2.Save(); Console.ReadLine(); // as a further example of the new property usage.. if (newProduct1.UnitPrice &gt; 5) Console.WriteLine(newProduct1); Console.WriteLine("Using nullable types is a great way to simplify code..."); Console.ReadLine(); } } } </code></pre> <h2>Output</h2> <pre><code>Product: Widget Price: 5.99 Product: Rare Widget Price: Out of stock Saving... Value written to the database: 5.99 Value written to the database: Product: Widget Price: 5.99 Product: Rare Widget Price: Out of stock Saving... Value written to the database: 5.99 Value written to the database: Product: Widget Price: 5.99 </code></pre> <hr> <p>Using nullable data types is a great way to simplify code...</p> <p>Let me know if there are more specific implementation details that concern you making the switch.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload