werck
werck

Reputation: 75

Calculating downloading/uploading speed error

I have a problem with my download/upload speed calculator. I write down the numbers to the "inputBox", and I think it can't parse it. Here's the source code:

!UPDATED!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net.NetworkInformation;

namespace Tomco_DownloadTime {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            timeLabel.Text = DateTime.Now.ToString("HH:mm tt");
        }

        IPv4InterfaceStatistics stat = NetworkInterface.GetAllNetworkInterfaces() [0].GetIPv4Statistics();

        private double getKBDownloadSpeed() {
            return (stat.BytesReceived)/1024;
        }

        private double getMBDownloadSpeed() {
            return ((stat.BytesReceived)/1024)/1024;
        }

        private double getGBDownloadSpeed() {
            return (((stat.BytesReceived)/1024)/1024)/1024;
        }

        private double getKBUploadSpeed() {
             return (stat.BytesSent)/1024;
        }

        private double getMBUploadSpeed() {
             return ((stat.BytesSent)/1024)/1024;
        }

        private double getGBUploadSpeed() {
            return (((stat.BytesSent)/1024)/1024)/1024;
        }

        private void startButton_Click(object sender,EventArgs e) {
            this.Width = 335;
            this.Height = 154;
            double size = double.Parse(inputBox.Text);

            if(roundingCheckBox.Checked == true) {
                if(downloadCheckBox.Checked == true) {
                    // download
                    if(kbCheckBox.Checked) {
                        sizeTypeLabel.Text = "Size (KB):";
                        double kbSpeed = size / getKBDownloadSpeed();
                        outputLabel.Text = kbSpeed.ToString();
                    } 
                    if(mbCheckBox.Checked) {
                        sizeTypeLabel.Text = "Size (MB):";
                    }
                    if(gbCheckBox.Checked) {
                        sizeTypeLabel.Text = "Size (GB):";
                    }
                }
                if(uploadCheckBox.Checked) {
                        //upload
                    if(kbCheckBox.Checked) {
                        sizeTypeLabel.Text = "Size (KB):";
                    }
                    if(mbCheckBox.Checked) {
                        sizeTypeLabel.Text = "Size (MB):";
                    }
                    if(gbCheckBox.Checked) {
                        sizeTypeLabel.Text = "Size (GB):";
                    }
                }
            } else {

            }
        }

        private void optionsButton_Click(object sender,EventArgs e) {
            this.Width = 335;
            this.Height = 241;
        }
     }
  }

Why do I think there's a problem with the parsing? Because if I add the numbers to the inputBox, and press the "Calculate" button, my MessageBox appears, and displays the error.

Upvotes: 1

Views: 375

Answers (1)

nrodic
nrodic

Reputation: 3021

It seems this is caused by unrecognized decimal separator. I guess the code is not run under en-US culture.

You can try:

double size = 
  double.Parse(inputBox.Text, System.Globalization.CultureInfo.InvariantCulture)

Upvotes: 1

Related Questions