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:
The Select Case structure is meant to only take 1 value and compare it to a list of possible values, so it'd work great if you were only needing to pick from 1 combo box

although come to think of it, this might work:
VB.NET:
Select Case True
  Case combobox1.text = "British pound" AndAlso combobox2.text = "Euro"
    Label1.text = "0.5"
End Select
 
VB.NET:
Select Case True
            Case cmbCurrency1.Text = "British Pound (GBP)" AndAlso cmbCurrency2.Text = "Euro (EUR)"
                lblrate = [U]"1.5"[/U]

The above underlined.
 
You need neither lots of If-Then or a Select structure, you need to lookup the exchange rate for both given currencies in some kind of table and just divide these values. There are many options for storing data, the most basic example here is a Dictionary where you use the currency string as key and an exchange rate as value. For example you have entered some rates in a Dictionary variable called "Rates", to lookup and calculate you would do this:
Dim buy As Double = Rates(CStr(ComboBox1.SelectedValue))
Dim sell As Double = Rates(CStr(ComboBox2.SelectedValue))
Me.Label1.Text = (buy / sell).ToString("n2")
This shortens your 50+ or 500+ lines of code or what it was to 3.

In real world currency has a little more depth, there is bid and offer rates which means different rates for selling and buying the same currency, and there is currency rates measured in different units, most common is 1 or 100. You also have banks and other services offering currency rate tables for download, which you can import dynamically.
 
Did you check with help documentation? It has explanations for all kinds of stuff and is loaded with code samples, it is well organized too.
 
I think your right Invisionsoft, I did check the documentation. Its ok, but I cannot ask it questions.

I'm still looking for a answer to this problem, the IF statements are ok, but its looks messy and very daunting (Less to a proffesional, but sharing with fellow students).

Any other suggestions apart from using Cases?

//
Ashcan
 
Looping through a multidimensional array. Well I know what one of those is, just no idea how to use it. But I made you this! (Had a go at arrays and *failed*):

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
 
I have only just started programming, one of my college assignments was to make a currency conversion program between the british pound, and the euro. I took it one step further and added about 6 currencys. To calculate and select the currencys i used combo boxes and lots and lots of IF statements,

so,

VB.NET:
IF comboxbox1 = euro and combobox2 = pound then lblrate = 1.5

and the same thing for each possible currency combination, which takes alot of space.

I will read up on Arrays and do some testing


//
Ashcan
 
Last edited:
Back
Top