Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Handling DB Nulls
Message
Information générale
Forum:
ASP.NET
Catégorie:
Conception classe
Divers
Thread ID:
01440804
Message ID:
01440896
Vues:
40
>>>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?



some thoughts ...

(1) make sure you are 'using' the namespace of the ExtensionMethods class
(2) The methods I wrote only work for value types and string type. Don't know what is the type is of ds.Tables[0].Rows[0]["ProjectName"].
[ haven't used them yet]

(3) ExtensionMethods class must be public
(4) extension methods must be static and public
(5) the first parameter of an extension method defines the type it operates on and is preceeded by 'this'
(6) write
_ProjectName = ds.Tables[0].Rows[0]["ProjectName"].        // - after the dot GetNonNull() should show up in intellisense
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform