Question Question about the Case Structure.

Ashcan

Member
Joined
Jul 23, 2008
Messages
11
Location
Ramsgate, Kent.
Programming Experience
Beginner
Hello,

I have made a currency conversion program at college, but now developing it some more.

I have 2 ComboBox's each one has the same list of Currencys, I have 50+ lines off IF statements that decide the currency rates for the calculation.

IF combobox1.text = "British pound" And combobox2.text = "Euro" then _
Label1.text = "0.5"

Instead of doing this, Can I use the Case decision structure to give each case a number value and make the calculation based upon them?

Thanks in advance.

//
Ashley
 
Last edited:
DONE! Tell your mentor you used some PHP and the Yahoo! API. Anyways here's your code:

VB.NET:
 If (ComboBox1.Text = ComboBox2.Text) Then
            MsgBox("Whats the point in converting a " + ComboBox1.Text + " to a " + ComboBox1.Text + "?")
            Exit Sub
        End If

        Dim wc As New Net.WebClient
        Dim incur As String = ""
        Dim outcur As String = ""

        Select Case ComboBox1.Text
            Case "Pound"
                incur = "GBP"
            Case "Dollar"
                incur = "USD"
            Case "Euro"
                incur = "EUR"
        End Select

        Select Case ComboBox2.Text
            Case "Pound"
                outcur = "GBP"
            Case "Dollar"
                outcur = "USD"
            Case "Euro"
                outcur = "EUR"
        End Select

        Dim rate As Double = Val(wc.DownloadString("http://www.dsgamemaker.com/currency.php?in=" + incur + "&out=" + outcur))

        MsgBox("The rate between " + ComboBox1.Text + " and " + ComboBox2.Text + " is " + rate.ToString)

And also, here's the PHP I used to get the rates:

VB.NET:
<?php
function convert($from,$to){
 $url= 'http://finance.yahoo.com/currency/convert?amt=1&from='.$from.'&to='.$to.'&submit=Convert';
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $feed = curl_exec($ch);
 curl_close($ch);
 preg_match_all("/tabledata1\">([^<]+)<\/td>/",$feed,$cells);
 return $cells[1][1];
}
echo convert($_GET['in'],$_GET['out']);
?>
 
VB.NET:
 If (ComboBox1.Text = ComboBox2.Text) Then
            MsgBox("Whats the point in converting a " + ComboBox1.Text + " to a " + ComboBox1.Text + "?")
            Exit Sub
        End If

^^ Checks to see if user is converting a £ to a £ or $ or $ or <euro> to a <euro>. If so, tells them a message then exits the sub (stops execution of that procedure).

VB.NET:
Dim wc As New Net.WebClient
        Dim incur As String = ""
        Dim outcur As String = ""

^^ Makes 3 variables. wc is a WebClient which allows us to send/receive data from the interweb. incur and outcur are regular strings we will use next.


VB.NET:
   Select Case ComboBox1.Text
            Case "Pound"
                incur = "GBP"
            Case "Dollar"
                incur = "USD"
            Case "Euro"
                incur = "EUR"
        End Select

Does a select case on the value of the first combobox. Because: The Yahoo! API does not accept "Pound" or "Dollar". It accepts country codes, e.g. Pound = GBP and Dollar = USD. So all we are doing is setting the currency codes in incur and outcur variables.

VB.NET:
Dim rate As Double = Val(wc.DownloadString("http://www.dsgamemaker.com/currency.php?in=" + incur + "&out=" + outcur))

        MsgBox("The rate between " + ComboBox1.Text + " and " + ComboBox2.Text + " is " + rate.ToString)

^^ 1)Make a variable called rate as a double - decimal number. Then, we make it equal the response of the Yahoo! API. Note use of VAL - the DownloadString function returns a String - so we have to convert back to a number.

The rest is obvious right?
 
Back
Top