Implementing Nullable types in existing objects?

UnitPrice { get; set; } private void Save() { //Datalayer calls and other props omitted SqlParameter sqlParm = new SqlParameter(); sqlParm. Value = this. UnitPrice?

DBNull. Value; } I find this absolutely ok This is how it is supposed to work.

UnitPrice { get; set; } private void Save() { //Datalayer calls and other props omitted SqlParameter sqlParm = new SqlParameter(); sqlParm. Value = this. UnitPrice?

DBNull. Value; } I find this absolutely ok. This is how it is supposed to work.

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. 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. 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.

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 > 5) Console.

WriteLine(newProduct1); Console. WriteLine("Using nullable data types is a great way to simplify code..."); Console.ReadLine(); } } } Output... 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 Using nullable data types is a great way to simplify code... Let me know if there are more specific implementation details that concern you making the switch.

The other benefit is the code is cleaner and more readable obviously. I'm sure this is part of the reason you want to make the switch in addition to performance and dependability issues as well. – Dave Jellison Jan 7 '10 at 9:27.

Generics and nullable types) and diverges from the DOM programming model with a variety of innovations to simplify programming against XML. Even without Language-Integrated Query capabilities LINQ to XML represents a significant stride forward for XML programming. The next section of this document, "Programming XML", provides more detail on the in-memory XML Programming API aspect of LINQ to XML.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions