Make Encapsulation Easy with dotNetTips.Utility

Encapsulation is the first pillar of Object Oriented Programming (OOP), yet most code that I see does not implement encapsulation correctly or not all. Like I say in many of my conference sessions "If you do not implement encapsulation, you aren't doing OOP!" I also say "Bad data in, bad data out!". Several years ago, … Continue reading Make Encapsulation Easy with dotNetTips.Utility

Using Generic Constraints & Default

Here is a great video from one of my buddies, Jeremy Clark, on generic constraints and default: Here is an extension method I made from Jeremy's second example: public static T Max<T>(this T obj1, T obj2) where T : IComparable {     if obj1.CompareTo(obj2) >= 0 ? return obj1 : return obj2; }

Los Angeles Code Camp 2009

I hope everyone in southern California is planning to attend this years Los Angeles Camp on 11/21 - 11/22. It's always agreat time and lots of free training! I will be presenting the following sessions and I hope you will attend. Also, check out my new .NET discussion site called DotNet Army! Building nTier Applications … Continue reading Los Angeles Code Camp 2009

Serialize and Deserialize Xml to Disk Using Generics

If you need to serialize and deserialize your objects and persist them to disk, the the code below is an easy way to accomplish this. The GetObjectTypes function correctly detects types so that the serializer will do it's job correctly. Without this you could run into problems (as I did).VB     ''' <summary>     ''' … Continue reading Serialize and Deserialize Xml to Disk Using Generics

Serialize Your Objects to and from JSON

Here is some easy, generic code to serialize your objects to and from JSON:Public Shared Function JsonEncode(ByVal input As Object) As String Dim serilizer = New DataContractJsonSerializer(input.GetType) Using ms = New MemoryStream() serilizer.WriteObject(ms, input) Return Encoding.Default.GetString(ms.ToArray()) End UsingEnd FunctionPublic Shared Function JsonDecode(Of T)(ByVal input As String) As T Using ms = New MemoryStream(Encoding.Unicode.GetBytes(input)) Dim serilizer … Continue reading Serialize Your Objects to and from JSON

How to Serialize and Deserialize Your Objects

I came up with some generic methods to do the job:Public Shared Function DeserializeXMLToObject(ByVal input As String, ByVal type As System.Type) As Object  Dim result As Object = Nothing  Dim serializer As New XmlSerializer(type)  Try    result = serializer.Deserialize(New XmlTextReader(New StringReader(input)))  Catch ex As Exception    Debug.WriteLine(ex.Message)  End Try   Return resultEnd FunctionPublic Shared Function SerializeObjectToXML(ByVal input As … Continue reading How to Serialize and Deserialize Your Objects