Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
actually i have develop web application using measure conversion.i have convert the inches to feet and voice versa.but how to convert remaing conversions,meters,centermeters,kilometers,miles,millimeters,roads,furlongs.actually i know for each conversion logic but i do not know how to write way of this programming for multiple conversion only

my code like this

C#
<form id="form1" runat="server">
    <div>
    
                                                                
        <asp:TextBox ID="TextBox1" runat="server" 
            style="top: 37px; left: 213px; position: absolute; height: 22px; width: 128px; right: 889px"></asp:TextBox>
          
        <br />
        <asp:TextBox ID="TextBox2" runat="server" 
            style="top: 39px; left: 510px; position: absolute; height: 22px; width: 128px"></asp:TextBox>
        <asp:DropDownList ID="DropDownList1" runat="server" 
            style="top: 37px; left: 351px; position: absolute; height: 22px; width: 77px">
            <asp:ListItem>Feet</asp:ListItem>
            <asp:ListItem>Miles</asp:ListItem>
            <asp:ListItem>Inches</asp:ListItem>
            <asp:ListItem>Meters</asp:ListItem>
            <asp:ListItem>Centimeters</asp:ListItem>
            <asp:ListItem>Millimeters</asp:ListItem>
            <asp:ListItem>rods</asp:ListItem>
            <asp:ListItem>Furlongs</asp:ListItem>
        </asp:DropDownList>
        <br />
  
        <br />
        <br />
        <br />
        <asp:DropDownList ID="DropDownList2" runat="server" 
            style="top: 36px; left: 652px; position: absolute; height: 22px; width: 77px">
            <asp:ListItem>Feet</asp:ListItem>
            <asp:ListItem>Miles</asp:ListItem>
            <asp:ListItem>Inches</asp:ListItem>
            <asp:ListItem>Meters</asp:ListItem>
            <asp:ListItem>centimeters</asp:ListItem>
            <asp:ListItem>millimeters</asp:ListItem>
            <asp:ListItem>rods</asp:ListItem>
            <asp:ListItem>furlongs</asp:ListItem>
        </asp:DropDownList>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            style="top: 86px; left: 448px; position: absolute; height: 26px; width: 56px" 
            Text="convert" />
        <br />
        <br />
        <br />
    
    </div>
    </form>



source code like this

C#
using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {           
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (DropDownList1.Text == "Feet")
            {
                double answer = Convert.ToDouble(TextBox1.Text) * 12.0;
                TextBox2.Text = answer.ToString();
            }
            if (DropDownList1.Text == "Inches")
            {
                double answer = Convert.ToDouble(TextBox1.Text) / 12.0;
                TextBox2.Text = answer.ToString();
            }          

        }
    }
}



i want solution for multiple conversion about each measurement
Posted

1 solution

You just need to do all this job in some regular way.

It is nearly straightforward if you want to develop measure conversion in basic measures, say, length, mass, time, electric charge. It's going to be much more difficult if you are going to convert combined values, such as cm/sec or g/cm² (pressure) and the like; it will need some understanding of basic physics and good deal of thinking. For now, I'll limit my advice to the idea of the simplest case.

Basically, you need to develop some universal class representing a set of the measurement units (this is how it is correctly called) of the same physical measure, such as length. It could be some collection, or, probably, more than one collections, like dictionaries indexed by, say, name, and, another one, say, conversion factor.

What would be the element of this set? It should be a type with such properties as name ("centimeters"), abbreviated name ("cm"), and a conversion factor relative to the base measurement unit, say, 1. Each instance of the set class can have a property defining a base unit (it could be expressed just as the index of one of the elements), the one of the conversion factor of 1. Other elements of the set will have conversion factors relative to this base measurement unit.

Then you should create several instances of this set class (one for length, another for mass, etc.), populate each with measurement units. This will be the basic structure you would use for all the general cases of unit transforms, general unified approach. You can further develop this system to deal with combined physical measures.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900