The input character string was not in an incorrect format. Operators

Asked

Viewed 25,519 times

11

Hello, I’m beginner in programming and I’m trying to show the user how much RAM is being consumed at the moment, I’m using the following code that is not pointing nor an error when compiling only when I give Start it says:

An unhandled Exception of type 'System.Formatexception' occurred in mscorlib.dll

Additional information: Input string was not in an incorrect format.

and break on this line:

double ram = Convert.ToInt32(Program.HardwareInfo.RAM());

Form1.Cs

    PerformanceCounter ram = new PerformanceCounter("Memory", "Available MBytes", null);

public string RAM_TIE()
        {
            float ran = ram.NextValue();
            int run = (int)ran;
            return run.ToString();

        }

    public string RAMU_TIE()
    {

        int ramu = Convert.ToInt32(RAM_TIE());
        double ram = Convert.ToInt32(Program.HardwareInfo.RAM());
        double sub = ram - ramu;
        return sub.ToString();

    }

Program.Cs

    public static string RAM()
    {
        ManagementScope oMs = new ManagementScope();
        ObjectQuery oQuery = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
        ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
        ManagementObjectCollection oCollection = oSearcher.Get();

        long MemSize = 0;
        long mCap = 0;

        // 
        foreach (ManagementObject obj in oCollection)
        {
            mCap = Convert.ToInt64(obj["Capacity"]);
            MemSize += mCap;
        }
        MemSize = (MemSize / 1024) / 1024;
        return MemSize.ToString() + "MB";
    }
  • I decided to answer because I think this code is much simpler than it is.

4 answers

11


The RAM() method returns a string that is not convertible to an integer.

Notice this line of this method:

return MemSize.ToString() + "MB";

change to:

return MemSize.ToString();

should work.

  • 1

    Thank you so much ramaral! you solved my problem, it’s a shame I don’t have repudiation 15 to give the answer as useful!

  • 1

    In the case of answers to your questions there is no such limitation, you can vote on the answers you find useful. and accept one of them

  • 1

    Thanks for the @ramaral warning!

4

if you do not want to change your RAM(), you can use an TrimEnd which receives an array of char to take from the end, which would be your "MB" like this:

var ram = Convert.ToInt32(Program.HardwareInfo.RAM().TrimEnd("MB".ToCharArray()));

3

You’re catching something that is long turning into string and then turn it into int and play in a variable double. This is crazy and it doesn’t make sense.

Change the return of RAM() for:

return MemSize;

Obviously the method will return a long and not one more string.

And in the call of this method use:

public string RAMU_TIE() {
    return (Program.HardwareInfo.RAM() - RAM_TIE()).ToString();
}

A lot simpler, right?

Depending on where you’re using this, ToString() is unnecessary (changing the type of method, of course.

I would do the same in RAM_TIE(). And still change some other things. Among them the way to take these values, as I have shown in another response to the AP. Actually I do not understand why time takes one way and time takes another. I strongly advise to use the way used in RAMU_TIE().

public int RAM_TIE() {
    return ram.NextValue();
}

It’s gotten so simple, you might not even need the method. I would if it had an important semantic meaning, but it doesn’t seem to be the case, the name doesn’t even say what this method is for. He’s out of his league.

If you do the same scheme in the method RAM(), it is probably also no longer necessary, there the RAMU_TIE() would only need this:

(ram.NextValue() - ramTie.NextValue()).ToString();

I put in the Github for future reference.

I leave it to the AP to exercise this. The code may be absurdly simpler than it is today.

1

Just not to exclude the answer, what I answered is wrong, it is possible yes store a int in double.

I think the mistake is exactly in the line you pointed out

double ram = Convert.ToInt32(Program.HardwareInfo.RAM());

You are assigning a int in a variable double

Browser other questions tagged

You are not signed in. Login or sign up in order to post.