2017-12-02 43 views
2
Private Function Calc(ByVal a As Decimal, ByVal Grams As Decimal) As Decimal 
    ' declare variables and convert to decimal 
    Grams = txtGrams.Text 
    a = Grams 
    Grams = a * 31.1035 
    Return Grams 
End Function 

Private Function Calcul(ByVal b As Decimal, ByVal Ounces As Decimal) As Decimal 
    Ounces = txtTroyOunces.Text 
    b = Ounces 
    Ounces = b * 0.911458 
    Return Ounces 
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 
    If IsNumeric(txtGrams.Text) Then 
     txtGrams.Text = Calc(txtGrams.Text) 
    Else 
     MessageBox.Show("Please enter a number") 
    End If 
End Sub 

私はとても近いと思いますが、パラメータGramsで引数が指定されていません。 2を期待する関数にVBアプリケーションでパラメータ用の引数が指定されていません

Calc(txtGrams.Text) 

:あなたはここでしか一つの引数を渡している

+1

は、としてそれを持つ多くのポイントはおそらくありませんパラメータ。そして、あなたが 'Grams'パラメータを新しい値で壊していることを考慮すると、それをパラメータとして持つ点はそれほどありません。 – YowE3K

答えて

1

私はコンパイルコードを提供しようとするので、ここであなたのコードを持ついくつかの問題があります。私はあなたのコードを実行すると信じていることをします。

最初の関数はグラムからトロイオンスに変換するので、私はそれに応じて名前を付けました。関数の唯一の目的は、乗算を実行してビット過不足である可能性があることです。ただし、定数にDを追加することに注意してください。これはDecimalリテラルです。これを追加しないと、代わりにDoubleリテラルになります。これは、乗算が実行される前に、入力Decimalの値がDoubleの値に変換されることを意味します。結果として返されるDoubleは、関数から返されるときにDecimal値に変換されます。 Decimalの値を使用するときは、通常、この精度の低下を避ける必要があります。 (使用されていない)

Private Function ConvertGramsToTroyOunces(ByVal grams As Decimal) As Decimal 
    Return grams*31.1035 
End Function 

他の機能は、次のようになります。

Private Function ConvertOuncesToTroyOunces(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 
    Dim grams As Decimal 
    If Decimal.TryParse(txtGrams.Text, grams) 
     txtGrams.Text = ConvertGramsToTroyOunces(grams) 
    Else 
     MessageBox.Show("Please enter a number") 
    End If 
End Sub 

私は、私はあなたを実装した「レガシー」のVisual Basicの関数であると考えていIsNumeric機能を使用していません自分自身。代わりに、Decimal.TryParseを使用して入力を確認し、一度に変換を実行します。

+0

ありがとうtxttroyouncesの別のif文を追加しました。それは魅力的です。これを数日間かけて別の方法を試して私の頭を打ちのめす。私はもう一度それを考えたようです。私はそれをする習慣があります。それはあなたの助けのために – vgingin

3

Private Function Calc(ByVal a As Decimal, ByVal Grams As Decimal) As Decimal 
0

このコードを試してください。それはあなたが持っているものにいくつかのの改善を行います

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 StrictOption 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 
+0

TYVMです。 – vgingin

関連する問題