Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Handling DB Nulls
Message
General information
Forum:
ASP.NET
Category:
Class design
Miscellaneous
Thread ID:
01440804
Message ID:
01440893
Views:
31
>>>>Kevin,
>>>>
>>>>I'm sure I've posted my GetNonNull set of static methods here a gazillion times. <g> Here it is again in the form of a link to my blog post about it:
>>>>
>>>>http://geek-goddess-bonnie.blogspot.com/2009/11/getting-non-null-data.html
>>>>
>>>>~~Bonnie
>>>>
>>>
>>>Bonnie,
>>>
>>>Is it fair to say that all types - except string - will be value types ?
>>>
>>>If so - consider this for the next update of the blog
>>>
>>>
>>>	public static class ExtensionMethods
>>>	{
>>>		//______________________________________________________________________
>>>		public static T GetNonNull<T>(this  T? value) where T : struct
>>>		{
>>>			return value == null ? default(T) : (T)value;
>>>		}
>>>		//______________________________________________________________________
>>>		public static string GetNonNull(this string value)
>>>		{
>>>			return value == null ? "" : value;
>>>		}
>>>		//______________________________________________________________________
>>>	}
>>>
>>>
>>>// test
>>>public class test2
>>>	{
>>>		
>>>
>>>		//______________________________________________________________________
>>>		public static void Main( string[] args )
>>>		{
>>>			int? i = null;
>>>			bool? b = null;
>>>			string x = null;
>>>			DateTime? dt = null;
>>>
>>>			Console.WriteLine("i: {0}", i.GetNonNull());
>>>			Console.WriteLine("b: {0}", b.GetNonNull());
>>>			Console.WriteLine("x: {0}", x.GetNonNull());
>>>			Console.WriteLine("dt: {0}", dt.GetNonNull());
>>>
>>>			Console.ReadLine();
>>>		}
>>>	}
>>>
>>>
>>
>>
>>Ok, I created a static class called ExtensionMethods in my MiscFunctions solution. I referenced it in this project and added the using statement. When I type this:
>>
>>
>>_ProjectName = ds.Tables[0].Rows[0]["ProjectName"].ToString();
>>
>>
>>I don't see GetNonNull in intellisense.
>>
>>What am I doing wrong?
>
>string _ProjectName = ds.Tables[0].Rows[0]["ProjectName"].ToString();
>
>_ProjectName = _ProjectName.GetNonNull(); // would this work?

Not for non-string values. If the data type of the column is int and it's null, it would error before it got to the GetNonNull line.
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform