このコードを試してください。それはあなたが持っているものにいくつかのの改善を行います
Private Function TroyOuncesFromGrams(ByVal Grams As Decimal) As Decimal
Return Grams * 31.1034768D
End Function
Private Function TroyOuncesFromStdOunces(ByVal Ounces As Decimal) As Decimal
Return Ounces * 0.911458D
End Function
Private Sub btnConvert_Click(sender As System.Object, ByVal e As EventArgs) Handles btnConvert.Click
' determine if text boxes txtGrams and txtTroyOunces is empty
Dim input As Decimal
If Decimal.TryParse(txtGrams.Text, input)
txtGrams.Text = ConvertGrams(input).ToString()
Else
MessageBox.Show("Please enter a number")
End If
End Sub
そして、あなたはOption Strict
やOption Infer
がオンになっていることを確認してください!それはすでにあなたのためのほとんどのエラーをキャッチしていただろう。
私もこのためDecimal
タイプのシャドウModule
あるいはStruct
使用するように誘惑されるだろう:
Public Struct TroyOunce
'TODO: Implements directives for IEquatable, IConvertable, IComparable, etc to match Decimal, plus addition/subtraction operators and overloads for GetHashCode(), Equals(), CompareTo() etc
Public Property Value As Decimal
Public Sub New()
End Sub
Public Sub New(quantity As Decimal)
Value = quantity
End Sub
Public Shared Widening Operator CType(ByVal ounces As TroyOunce) As Decimal
Return ounces.Value
End Operator
Public Shared Narrowing Operator CType(ByVal ounces As TroyOunce) As Double
Return CDbl(ounces.Value)
End Operator
Public Shared Narrowing Operator CType(ByVal ounces As TroyOunce) As Integer
Return CInt(ounces.Value)
End Operator
Public Shared Widening Operator CType(ByVal ounces As Decimal) As TroyOunce
Return New TroyOunce(ounces)
End Operator
Public Shared Widening Operator CType(ByVal ounces As Double) As TroyOunce
Return New TroyOunce(CDec(ounces))
End Operator
Public Shared Widening Operator CType(ByVal ounces As Integer) As TroyOunce
Return New TroyOunce(CDec(ounces))
End Operator
Public Shared Function FromGrams(grams As Decimal) As TroyOunce
Return New TroyOunce(grams * 31.1034768D)
End Function
Public Shared Function FromStdOunces(ounces As Decimal) As TroyOunce
Return New TroyOunce(ounces* 0.911458D)
End Function
Public Overrides Function ToString()
Return Value.ToString()
End Function
Public Overrides Function ToString(provider As IFormatProvider)
Return Value.ToString(provider)
End Function
Public Overrides Function ToString(format As String)
Return Value.ToString(format)
End Function
Public Overrides Function ToString(format As String, provider As IFormatProvider)
Return Value.ToString(format, provider)
End Function
End Struct
構造体が完了したら、あなたはほとんどの部分でちょうどAのように扱うことができますが小数、そしてあなたのClickイベントは次のようになります。あなたは、パラメータとして渡されたものを使用する前に、新しい値を持つ `A`パラメータをつかうしていることを考えると
Private Sub btnConvert_Click(sender As System.Object, ByVal e As EventArgs) Handles btnConvert.Click
' determine if text boxes txtGrams and txtTroyOunces is empty
Dim input As Decimal
If Decimal.TryParse(txtGrams.Text, input)
txtGrams.Text = TroyOunce.FromGrams(input).ToString()
Else
MessageBox.Show("Please enter a number")
End If
End Sub
は、としてそれを持つ多くのポイントはおそらくありませんパラメータ。そして、あなたが 'Grams'パラメータを新しい値で壊していることを考慮すると、それをパラメータとして持つ点はそれほどありません。 – YowE3K