1

totalPrice を計算して totalPriceOutputLabel に返す関数があります。私の問題は、出力を「1,222」などのようにフォーマットする必要があることです。私はそれをそのように変換する方法を知っています

ToString("C2")

しかし、関数呼び出し内でそれをアタッチする方法がわかりません。何か案は?

Public Class tileLimitedForm

Private enteredLength, enteredWidth As Double
Private enteredPrice As Decimal

Public Function area(ByRef enteredLength As Double, ByRef enteredWidth As Double)
    area = Val(enteredLength) * Val(enteredWidth)
End Function

Public Function totalPrice(ByRef enteredLength As Double, ByRef enteredWidth As Double)
    totalPrice = Val(area(enteredLength, enteredWidth)) * Val(enteredPrice)
End Function

Private Sub calculateButton_Click(sender As Object, e As EventArgs) Handles calculateButton.Click

totalPriceOutputLabel.Text = totalPrice(area(enteredLength, enteredWidth),enteredPrice).ToString("C2")

End Sub
4

1 に答える 1

0

ちょうどこのような:

totalPriceOutputLabel.Text = _
    totalPrice(area(enteredLength, enteredWidth), enteredPrice).ToString("C2")

totalPriceは、フォーマット パラメーターを使用Doubleした拡張子をサポートする、またはその他の数値型であると想定しています。.ToString()

編集

編集された質問を見た後:

 Public Class tileLimitedForm

        Private enteredLength, enteredWidth As Double
        Private enteredPrice As Decimal

        Public Function area(ByVal enteredLength As Double, ByVal enteredWidth As Double) As Double
            area = enteredLength * enteredWidth
        End Function

        Public Function totalPrice(ByVal enteredLength As Double, ByvalenteredWidth As Double) As Double
            totalPrice = area(enteredLength, enteredWidth) * enteredPrice
        End Function

        Private Sub calculateButton_Click(sender As Object, e As EventArgs) Handles calculateButton.Click
            totalPriceOutputLabel.Text = totalPrice(area(enteredLength, enteredWidth), enteredPrice).ToString("C2")
        End Sub
    End Class

ノート:

  • この場合、関数ByValの代わりに使用する必要がありますByRef
  • Object関数を返す型を設定していないため (Option Strict をオフにしている)、関数は現在 を返します=> を追加しましAs Doubleた。
  • Valパラメータはすでに数値型であるため、を使用する必要はありません。
于 2013-09-25T04:18:24.717 に答える