Generic Type Conversion snippet for C# and VB.NET

Here you can find a generic Type conversion snippet for C# and VB.NET.

Sample C#

public static T GenericConvert<T, X>(X value) where X : IConvertible
{
	return (T)Convert.ChangeType(value, typeof(T));
}

Sample VB.NET

Public Shared Function GenericConvert(Of T, X As IConvertible)(value As X) As T
	Return DirectCast(Convert.ChangeType(value, GetType(T)), T)
End Function

Please rate this snippet

How to convert string to int using Objective-C

To convert string to int using Objective-C you can use the snippet below.

Sample Objective-C

[myString intValue];

or you can do it like this.

myString.intValue;

Please rate this snippet

How to convert a string to enum in Java

To convert a string to enum in Java you can use the following snippet.

Considering the following enum.

Sample Java

public enum Testenum {
  One,Two,Three,Four
}

you can cast an string to enum like this:

Sample Java

Testenum.valueOf("Two");

Please rate this snippet

How to convert a boolean to Json Boolean in C# and VB.NET

To convert a boolean to Json Boolean in C# and VB.NET you can use the following snippet.

Sample C#

public static string ToJson(bool inputVal)
{
	return inputVal ? "true" : "false";
}

Sample VB.NET

Public Shared Function ToJson(inputVal As Boolean) As String
	Return If(inputVal, "true", "false")
End Function

Please rate this snippet