0

I know that I'm able to convert things to string with .ToString() but I'm not sure how I would go about converting something to an integer.

        int version = webBrowser1.Document.Body.InnerText;
        if (version > 0.0.1)
        {

        }

Thats what I have so far

I'm trying to convert innertext to an integer, not trying to convert general things to integer.

DanMossa
  • 994
  • 2
  • 17
  • 48

2 Answers2

2

You can do the following, based upon the format you expect, i.e. a version number consisting of integer parts separated by a dot ('.'):

var version = webBrowser.Document.Body.InnerText
   .Split('.') // this splits the string into number parts
   .Select(p => Int32.Parse(p)) // this converts each number part to an int
   .ToArray() // this returns them as an array of integer values.

// This prints the version components
if (version.Length == 1)
    Console.WriteLine("Version: {0}", version[0]);
else if (version.Length == 2) 
    Console.WriteLine("Version: Major = {0}, Minor = {1}", version[0], version[1]);
else if (version.Length == 3)
    Console.WriteLine("Version: Major = {0}, Minor = {1}, Increment = {2}", version[0], version[1], version[2]);
else if (version.Length == 3)
    Console.WriteLine("Version: Major = {0}, Minor = {1}, Increment = {2}, BuildNumber = {3}", version[0], version[1], version[2], version[4]);
else
    Console.WriteLine("Version contains more than 4 ({0}) parts", version.Length);

Note, that this is not a single integer that you will get returned. You have to access the components individually if you want to do a comparison, I.e. compare version[0] with the major version that you are expecting, version[1] with the minor version component, etc.

A standard class that is already available specifically for version numbers formatted in this way is the Version class. You can create one from your text by doing this:

var version = new Version(webBrowser.Document.Body.InnerText);
// or alternatively:
//var version = Version.Parse(webBrowser.Document.Body.InnerText);

Console.WriteLine(version);

If you want to compare this with another version (let's say "2.3.1"), you can do that like this:

var otherVersion = new Version("2.3.1");
// Or alternatively:
// var otherVersion = new Version(2, 3, 1);

// this compares two versions
var compareResult = version.CompareTo(otherVersion);
if (compareResult < 0)
    Console.WriteLine("version < otherVersion");
else if (compareResult > 0)
    Console.WriteLine("version > otherVersion");
else
    Console.WriteLine("version == otherVersion");

When applied to the code snippet in your question, you can use it like this:

var currentVersion = new Version("0.0.1");
var version = Version.Parse(webBrowser.Document.Body.InnerText);
if (version.CompareTo(currentVersion) > 0)
{
     // version is newer (higher) than our current version of "0.0.1"
}
Alex
  • 13,024
  • 33
  • 62
  • I was following up until the last part. I'm not sure how I would go about comparing the version number to another integer with this code? – DanMossa Apr 18 '15 at 05:18
  • @pshyoulost can you follow it now? It looks to me like your easiest option is to use the `Version` class. What exactly is it that you are trying to compare the version from `webBrowser.Document.Body.InnerText` with? – Alex Apr 18 '15 at 05:49
  • It makes a lot more sense now. Thanks. I'm trying to compare it to the current version number of the program – DanMossa Apr 18 '15 at 05:52
  • @pshyoulost ok, sample usage attached at the end of the answer – Alex Apr 18 '15 at 05:56
1

A number with two decimal points is not a number.

Here is an example:

using System;

namespace so29713053
{
    class Program
    {
        static void Main(string[] args)
        {
            string version = "0.0.2";

            Console.WriteLine("Checking if version match 0.0.1");
            if (version == "0.0.1")
            {
                Console.WriteLine("  version match");
            }
            else
            {
                Console.WriteLine("  version doesn't match");
            }


            Console.WriteLine("\nChecking if version match 0.0.2");
            if (version == "0.0.2")
            {
                Console.WriteLine("  version match");
            }
            else
            {
                Console.WriteLine("  version doesn't match");
            }

            Console.WriteLine("\nCheck version using string.compare...");
            switch (string.Compare(version, "0.0.1"))
            {
                case 0:
                    Console.WriteLine("   version match");
                    break;

                case 1:
                    Console.WriteLine("   version is newer");
                    break;

                case -1:
                    Console.WriteLine("   version is older");
                    break;
            }

            // or you can go deep - code need error checking
            string[] parts = version.Split('.');
            string major = parts[0];
            string minor = parts[1];
            string revision = parts[2];

            // compare each parts of the version number
            Console.ReadLine();
        }
    }
}

Output:

Output from Console Code Sample above

Black Frog
  • 11,595
  • 1
  • 35
  • 66