VB应用说参数没有指定参数

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未指定参数。VB应用说参数没有指定参数

回答:

有你的代码所以这里的几个问题我尽量提供代码编译也做我相信你打算你的代码做的事情。

第一个函数从克转换为金衡盎司,所以我已经相应地命名了它。该函数的唯一目的是执行乘法,所以它可能有点矫枉过正。但是,请注意,我将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

我不使用IsNumeric功能,我相信这是一个“传统”的Visual Basic,你已经实现了您的功能rself。相反,我使用Decimal.TryParse来检查输入并一次执行转换。

回答:

你只是路过一个参数的位置:

Calc(txtGrams.Text) 

为了这需要两个功能:

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

回答:

试试这个代码。它使几个改善您所拥有的一切:

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开启!这将已经为您抓住了大部分错误。

我也是会忍不住使用Module甚至Struct是阴影的Decimal类型是:

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

当结构完成后,就可以在很大程度上把它就像一个小数,你的Click事件看起来就像这样:

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

以上是 VB应用说参数没有指定参数 的全部内容, 来源链接: utcz.com/qa/258432.html

回到顶部