dimanche 3 février 2019

How to use values stated in the if-else statement?

I am recently started studying C# and I wanted to make a length converter with different units. However, it seems that I cannot use values stated inside the if-else statement. Can anyone please help me?

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace converter
{
    public partial class MainForm : Form
    {
    public MainForm()
    {
        InitializeComponent();
    }
    void ClearBClick(object sender, EventArgs e)
    {
        convIn.Clear();
        convOut.Clear();
        cmbConv.Refresh();
    }
    void ExitBClick(object sender, EventArgs e)
    {
        this.Close();
    }
    void ConvBClick(object sender, EventArgs e)
    { double exchangeRate;

        if(cmbConv.SelectedItem.ToString() == "Miles to Kilometers")
        {
            exchangeRate = 1.60934;
        }
        else if(cmbConv.SelectedItem.ToString() == "Kilometers to Miles")
        {
            exchangeRate = 0.621371;
        }
        else if(cmbConv.SelectedItem.ToString() == "Inches to Centimeters")
        {
            exchangeRate = 0.393701;
        }
        else if(cmbConv.SelectedItem.ToString() == "Centimeters to Inches")
        {
            exchangeRate = 2.54;
        }

        double conv = Convert.ToDouble(convIn.Text);
        var conversion = conv * exchangeRate;

        var result = Convert.ToString(conversion);

        convOut.Text = result;
        }

    }
}

Before I used this, I used a different structure which also seem to not work, in which I separated the if-else in a different method.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace converter
{
    public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }
    void ClearBClick(object sender, EventArgs e)
    {
        convIn.Clear();
        convOut.Clear();
        cmbConv.Refresh();
    }
    void ExitBClick(object sender, EventArgs e)
    {
        this.Close();
    }
    void ConvBClick(object sender, EventArgs e)
    {           double conv = Convert.ToDouble(convIn.Text);
        var conversion = conv * exchangeRate;

        var result = Convert.ToString(conversion);

        convOut.Text = result;
    }
    void CmbConvSelectedIndexChanged(object sender, EventArgs e)
    {
         double exchangeRate;

        if(cmbConv.SelectedItem.ToString() == "Miles to Kilometers")
        {
            exchangeRate = 1.60934;
        }
        else if(cmbConv.SelectedItem.ToString() == "Kilometers to Miles")
        {
            exchangeRate = 0.621371;
        }
        else if(cmbConv.SelectedItem.ToString() == "Inches to Centimeters")
        {
            exchangeRate = 0.393701;
        }
        else if(cmbConv.SelectedItem.ToString() == "Centimeters to Inches")
        {
            exchangeRate = 2.54;
        }

     }
 }

Aucun commentaire:

Enregistrer un commentaire