想要在VB.NET中声明一个字符串的值

时间:2014-02-15 11:09:18

标签: vb.net string

我目前正在VB.NET中制作货币转换器,但存在一些障碍。我要转换的货币在组合框中被选中。

现在看起来像这样:

    If Combobox1.Text = "USD" Then
        USD1.Text = Amount.Text * (USD / USD)
        EUR1.Text = Amount.Text * (USD / EUR)
        GBP1.Text = Amount.Text * (USD / GBP)
        NOK1.Text = Amount.Text * (USD / NOK)
    End If
    If Combobox1.Text = "EUR" Then
        USD1.Text = Amount.Text * (EUR / USD)
        EUR1.Text = Amount.Text * (EUR / EUR)
        GBP1.Text = Amount.Text * (EUR / GBP)
        NOK1.Text = Amount.Text * (EUR / NOK)
    End If

但我希望它看起来更像这样:

    USD1.Text = Amount * (SelectedExch / USD)
    EUR1.Text = Amount * (SelectedExch / EUR)

那么有什么建议吗?

编辑:我做了一些研究和实验,目前这是我的代码:

Private Sub Textbox1_TextChanged(sender As Object, e As EventArgs) Handles Amount.TextChanged
    Dim USD = My.Settings.USD
    Dim EUR = My.Settings.EUR
    Dim GBP = My.Settings.GBP
    Dim NOK = My.Settings.NOK
    Dim value, value2 As Decimal
    If Not Decimal.TryParse(Amount.Text, value) Then
        USD1.Text = ""
        EUR1.Text = ""
        GBP1.Text = ""
        NOK1.Text = ""
        Return
    End If
    Select Case Currency.Text
        Case "USD"
            value2 = USD
        Case "EUR"
            value2 = EUR
        Case "GBP"
            value2 = GBP
        Case "NOK"
            value2 = NOK
    End Select
    value2 *= value
    USD1.Text = Math.Round((value2 / USD), 1)
    EUR1.Text = Math.Round((value2 / EUR), 1)
    GBP1.Text = Math.Round((value2 / GBP), 1)
    NOK1.Text = Math.Round((value2 / NOK), 1)
End Sub

我不能说我理解代码中的所有内容。你觉得还有改进的余地吗?我后来想给用户添加货币的选项

3 个答案:

答案 0 :(得分:0)

这可能是一种更优雅的方式来做到这一点

Public Class Form1

   Private Enum CurrencyTypes
       USD
       EUR
       GBP
       NOK
   End Enum

   Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) _
                                                            Handles MyBase.Load
       ComboBox1.DataSource = [Enum].GetValues(GetType(CurrencyTypes))
   End Sub

   Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _
                                           Handles ComboBox1.SelectedIndexChanged
       Dim source As CurrencyTypes = [Enum].Parse(GetType(CurrencyTypes), _
                                                  ComboBox1.SelectedValue)
       UpdateConversion(source)
   End Sub

   Private Sub UpdateConversion(ByVal sourceCurrency As CurrencyTypes)
       USD1.Text = ConvertCurrency(sourceCurrency, CurrencyTypes.USD).ToString()
       EUR1.Text = ConvertCurrency(sourceCurrency, CurrencyTypes.EUR).ToString()
       GBP1.Text = ConvertCurrency(sourceCurrency, CurrencyTypes.GBP).ToString()
       NOK1.Text = ConvertCurrency(sourceCurrency, CurrencyTypes.NOK).ToString()
   End Sub

   Private Function ConvertCurrency(ByVal fromType As CurrencyTypes, _
                                          toType As CurrencyTypes) As Decimal
        'do conversion
   End Function

End Class

您尚未显示有关如何获取转化价值的任何实施细节。你似乎在字符串上显示数学,这没有任何意义 - 如果你已经关闭了Option Strict,那么为了对所有事物的热爱,请将其打开。在任何情况下,我都没有填写ConvertCurrency函数,但可能是你从某个地方获取交换价值。所有该功能需要将转换值与CurrencyTypes枚举类型相关联。如果你不知道如何做到这一点,它可能是另一个问题的好主题。

答案 1 :(得分:0)

怎么样......

Public Class Form1

    Dim lstExchangeRates As New List(Of ExchangeRate)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim excExchangeRate As ExchangeRate

        excExchangeRate = New ExchangeRate With {.RateName = "USD", .Rate = 1}
        lstExchangeRates.Add(excExchangeRate)

        excExchangeRate = New ExchangeRate With {.RateName = "EUR", .Rate = 1.2}
        lstExchangeRates.Add(excExchangeRate)

        excExchangeRate = New ExchangeRate With {.RateName = "GBP", .Rate = 0.5}
        lstExchangeRates.Add(excExchangeRate)

        excExchangeRate = New ExchangeRate With {.RateName = "NOK", .Rate = 1.5}
        lstExchangeRates.Add(excExchangeRate)

        ComboBox1.DataSource = lstExchangeRates
        ComboBox1.DisplayMember = "RateName"
        ComboBox1.ValueMember = "Rate"

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        CalculateValues()

    End Sub

    Private Sub Amount_TextChanged(sender As Object, e As EventArgs) Handles Amount.TextChanged
        CalculateValues()

    End Sub

    Private Sub CalculateValues()

        If Amount.Text = "" Then Exit Sub

        For Each ExchangeRateItem As ExchangeRate In lstExchangeRates

            Dim txtExchangeRate As TextBox = CType(Me.Controls(ExchangeRateItem.RateName & "1"), TextBox)

            txtExchangeRate.Text = CDbl(Amount.Text) * (ComboBox1.SelectedValue / (From ExchangeRates As ExchangeRate In lstExchangeRates
                                            Where ExchangeRates.RateName = ExchangeRateItem.RateName
                                            Select ExchangeRates.Rate).FirstOrDefault)

        Next

    End Sub

End Class

Public Class ExchangeRate

    Public Property RateName As String
    Public Property Rate As Double

End Class

答案 2 :(得分:0)

为什么不是最简单的方法,在Form Load中构造Dictionary:

Dim Currencies as New Dictionary(Of String, Double)
Currencies.Add("USD", [Value of USD])
Currencies.Add("EUR", [Value of EUR])
...

并且呼叫就像:

Dim SelectedExch as Double = Currencies(Combobox1.Text)
USD1.Text = Amount.Text * (SelectedExch / USD)
EUR1.Text = Amount.Text * (SelectedExch / EUR)
GBP1.Text = Amount.Text * (SelectedExch / GBP)
NOK1.Text = Amount.Text * (SelectedExch / NOK)