Exercise 4 Performing Culture Dependent Conversion

Crypto Ultimatum

How to Make Money with Bitcoin

Get Instant Access

Just as the Currency Converter displays a different interface for different cultures, it also performs different conversions based on the current culture. In this exercise, you will write the code that performs the culture-dependent conversions.

To perform the culture-dependent conversions

Add the following code to the butOK_Click event procedure to detect the current culture and perform different conversions based on that information:

Visual Basic .NET

Private Sub butOK_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles butOK.Click ' Get the amount.

Dim SrcAmount As Double = Convert.ToDouble(txtCurrency.Text) Dim DestAmount As Double

' Culture variables for currency formatting. Dim Europe As New CultureInfo("fr-FR") Dim USA As New CultureInfo("en-US") Dim Mexico As New CultureInfo("es-MX")

' Perform conversions based on current user-interface culture. Select Case Thread.CurrentThread.CurrentCulture.Name Case "en-US"

' Convert from dollars. DestAmount = SrcAmount * 1.1329

lblCurrencyl.Text = DestAmount.ToString("C", Europe) DestAmount = SrcAmount * 9.0655

lblCurrency2.Text = DestAmount.ToString("C", Mexico) Case "es-MX"

' Convert from pesos. DestAmount = SrcAmount * 0.125

lblCurrency1.Text = DestAmount.ToString("C", Europe) DestAmount = SrcAmount * 0.1103

lblCurrency2.Text = DestAmount.ToString("C", USA) Case Else

' Convert from euros. DestAmount = SrcAmount * 8.0021

lblCurrency1.Text = DestAmount.ToString("C", USA) DestAmount = SrcAmount * 0.1103

lblCurrency2.Text = DestAmount.ToString("C", Mexico)

End Select End Sub

Visual C#

private void butOK_Click(object sender, System.EventArgs e) {

double SrcAmount = Convert.ToDouble(txtCurrency.Text); double DestAmount;

// Culture variables for currency formatting. CultureInfo Europe = new CultureInfo("fr-FR"); CultureInfo USA = new CultureInfo("en-US"); CultureInfo Mexico = new CultureInfo("es-MX");

// Perform conversions based on current user-interface culture.

switch (Thread.CurrentThread.CurrentCulture.Name) {

// Convert from dollars. DestAmount = SrcAmount * 1.132 9;

lblCurrencyl.Text = DestAmount.ToString("C", Europe); DestAmount = SrcAmount * 9.0655;

lblCurrency2.Text = DestAmount.ToString("C", Mexico); break; case "es-MX":

// Convert from pesos. DestAmount = SrcAmount * 0.125;

lblCurrencyl.Text = DestAmount.ToString("C", Europe); DestAmount = SrcAmount * 0.1103;

lblCurrency2.Text = DestAmount.ToString("C", USA);

break; default:

// Convert from euros. DestAmount = SrcAmount * 8.0 021;

lblCurrency1.Text = DestAmount.ToString("C", USA); DestAmount = SrcAmount * 0.1103;

lblCurrency2.Text = DestAmount.ToString("C", Mexico); break;

The preceding code creates Culturelnfo objects (Europe, United States, and Mexico) for each cultural conversion. These objects provide the appropriate currency formatting in the ToString methods. The Europe object uses the culture code for France, because France uses the euro as currency. There isn' t a culture code for the European Union.

The preceding example also hard-codes the exchange rates into the procedure. You wouldn' t do that in a real-world situation, of course. Getting current exchange rates is an appropriate task for Web Services. See Chapter 7, "Advanced Web Forms Programming," for a lesson on how to use XML Web Services.

Was this article helpful?

0 0

Post a comment