How do I create a currency convertor in .NET?

I only need a method with a set rate that can be whatever (I don’t want to use an api) to convert let’s say dollars to euros.

I’ve never used a backend language before and so I’m not sure how to do this but I assume the method would look a bit like this?

public void Convert(int rate, string fromDollars, string toEuros)
{

}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Well, you didn’t specify webforms or mvc.

So, lets open up a web form. We drag + drop in two combo boxes (drop downs).

A text box, a button, and we have this:

How do I create a currency convertor in .NET?

Ok, we now assume we have a table in sql server like this:

How do I create a currency convertor in .NET?

Ok, now comes our code.

on page load “event”, we fill the two combo boxes (our from to currency).

The code looks like this on page load (we fill the two combo boxes).

' on first page load only, load up combo boxes
If IsPostBack = False Then
   Dim strSQL = "SELECT ToUsDollars, Country FROM tblCurrency ORDER BY Country"
   Dim MyData As DataTable = Myrst(strSQL)
   cFrom.DataSource = MyData
   cFrom.DataBind()
   cTo.DataSource = MyData
   cTo.DataBind()
End If

And the code behind our button is thus this:

Dim curAmount As Decimal = txtFromAmount.Text
Dim curResult As Decimal = curAmount / cFrom.SelectedValue
' we have amount in usa dollars, now conver to target amount
Me.txtResultAmount.Text = curResult * cTo.SelectedValue

So we take any currency, convert it to one known currency (USA) and then simply convert it to the target currency.

The resulting page looks like this:

How do I create a currency convertor in .NET?

So, it not much code. The handy dandy “MyRst” is simply a routine that takes our sql string and returns a data table.

it looks like this:

Public Function Myrst(strSQL As String) As DataTable

    Dim strCon As String = ConfigurationManager.ConnectionStrings("Test4").ConnectionString
    Dim rstData As New DataTable

    Using mycon As New SqlConnection(strCon)
        Dim cmdSQL As New SqlCommand(strSQL, mycon)
        mycon.Open()
        rstData.Load(cmdSQL.ExecuteReader)
    End Using

    Return rstData

End Function

So above shows:

Code to load up the combo (dropDownList)
And the button code (server side .net code) that runs when you click a button.

So, this was “mostly” a drag + drop design approach, and then code to load the combo’s, and the code for the one button to calculate the results.

So, above should give you an idea of how code works for asp.net

From the drag + drop form desinger?

the resulting markup does look like this:

(I did add two “divs” to force the two combo boxes beside each other).
Nothing pretty – just done in a few minutes of time.

<body>
  <form id="form1" runat="server">
    <div>
        <div style="float:left">
            <asp:Label ID="Label1" runat="server" Text="From"></asp:Label>
            <br />
            <asp:DropDownList ID="cFrom" runat="server" Width="131px" DataTextField="Country" DataValueField="ToUsDollars" Height="17px"></asp:DropDownList>
       </div>

        <div style="float:left;padding-left:30px">
            <asp:Label ID="Label2" runat="server" Text="To"></asp:Label>
            <br />
            <asp:DropDownList ID="cTo" runat="server" Width="135px" DataTextField="Country" DataValueField="ToUsDollars" Height="20px"></asp:DropDownList>
            <br />
       </div>

        <div style="clear:both;float:left;text-align:center">
            <br />
            <h2>enter amount</h2>
            <asp:TextBox ID="txtFromAmount" runat="server"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Calulate" Width="111px" />
            <h2>Result</h2>
            <asp:TextBox ID="txtResultAmount" runat="server" Width="183px" Font-Size="Large"></asp:TextBox>
        </div>
    </div>
  </form>
</body>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x