CompareVersions Method (.NET)

 

This static method compares version numbers in a string format to one another.

Syntax:

 result = bhObj.CompareVersions(string1, string2)

Returns:

 -1: The version number represented by string 1 is less than string 2

 0: The version numbers of string 1 and string 2 are the same

 1: The version number represented by string 1 is greater than string 2

The CompareVersions method is extremely convenient for making numeric comparisons between version numbers represented by strings. This is because normal string comparisons often return the wrong result for version numbers because standard greater than and less than comparisons are lexicographic.

For example, with a normal lexicographic string comparison the following would return true:

"1.10" < "1.9" (evaluates to True, incorrect for version comparisons)

The above statement returns true because on a lexicographic basis the 1.10 value is less than 1.9. On a numeric basis, however, certainly 1.10 is a greater version number than 1.9 making standard comparison operators ineffective for numeric comparisons. The CompareVersions method therefore provides an easy and effective way to make such numeric comparisons accurately:

BrowserObj.CompareVersions("1.10", "1.9") (evaluates to 1 since the first is greater; correct) 

In addition to its ability to make numeric comparisons with strings, CompareVersions is also very convenient because it handles various version number formats automatically. For example the version number can use periods ("6.0.15.0"), commas ("6,0,15,0"), or even the "r" format used by the Flash plugin on non IE Windows browsers, such as "6.0 r15".

For example, if you wanted to make sure the Flash plugin version is at least 6.0.15.0, you can simply do this:

<%

ExtendedOptions options = new ExtendedOptions();

options.AddProperties("Plugin_FlashVerEx");

ExtendedBrowserObj extBrow = BrowserObj.GetExtendedBrowser(options);

if (BrowserObj.CompareVersions(extBrow.Plugin_FlashVerEx, "6.0.15.0") < 0) {

Response.Redirect("/higher_flash_required.asp"); // player is not installed or version is prior to 6.0.15.0

}

%>

<html>… </html>

In the above example CompareVersions takes care of automatically adjusting for whatever version format the Plugin_FlashVerEx property may return depending on the browser (such as 7.0.19.0 under IE Windows and 7.0 r19 under Mozilla).

Likewise CompareVersions can be used to check to see if the version number is greater than a major version. For example: bhObj.CompareVersions(bhObj.Plugin_FlashVerEx, "6") will return -1 if Flash is not installed or the version is prior to 6.0, 0 if it is Flash 6.0, and 1 if it’s a version of Flash greater than 6.0 (such as 6.01 or 7.0).

In the event letters are contained within the version number, these are considered as well. For example "6.10a1" is considered less than "6.10b1". Letter comparisons are not case sensitive.

Any trailing zeros do not affect the results of the comparison. For example "6" is considered equal to "6.0" and "6.0.0" etc.

Here are some examples to help further demonstrate how the comparisons work:

CompareVersions("9.1", "9.1.0.0") returns 0

CompareVersions("x.y.z", "x.y.z.a") returns -1

CompareVersions("8.1", "9.x") returns -1

CompareVersions("5.71a", "5.8f") returns 1 (71a is considered greater than 8f)

CompareVersions("9.1 r9", "9.1.10) returns -1 (9.1 r9 converts to 9.1.9.0)

CompareVersions("2.5.9.1", "2,5,9,1") returns 0

CompareVersions("7.1", "7.1.0.00") returns 0

CompareVersions("1.2.3.f", "1.2.3.f12") returns -1