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
指定参数。
答案 0 :(得分:3)
你只在这里传递一个论点:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define OUT_OF_MEMORY 3
#define FILE_EXTENSION ".txt"
typedef enum _Bool_ // enum _Bool_ is a non-typedef'ed enum
{
FALSE = 0, // Enum element
TRUE = 1 // Enum element
} Bool; // Bool is the typedef'ed enum
Bool cStringEndsWith(const char *sourceString, const char *suffix) {
if (!sourceString || !suffix) // Check for not null pointer
{
return FALSE;
}
size_t length_of_c_string = strlen(sourceString);
size_t length_of_suffix = strlen(suffix);
if (length_of_suffix > length_of_c_string) {
return FALSE;
}
int compare_result = strncmp(sourceString + length_of_c_string - length_of_suffix, suffix, length_of_suffix);
if (compare_result == 0) {
return TRUE;
} else {
return FALSE;
}
}
int prepareFileName(char **ptr_file_name){
int ends_with_file_extension = cStringEndsWith(*ptr_file_name, FILE_EXTENSION);
if(!ends_with_file_extension)
{
char *new_ptr_file_name = realloc(*ptr_file_name, strlen(*ptr_file_name) + strlen(FILE_EXTENSION) + 1);
if(!new_ptr_file_name)
return OUT_OF_MEMORY;
*ptr_file_name = new_ptr_file_name;
strcat(*ptr_file_name, FILE_EXTENSION);
}
}
int main()
{
char *file_name = "testFileName";
printf("Filename unprepared: \"%s\"", file_name);
prepareFileName(&file_name);
printf("Filename prepared: \"%s\"", file_name);
return 0;
}
对于期望两个的函数:
Calc(txtGrams.Text)
答案 1 :(得分: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
检查输入并一次执行转换。
答案 2 :(得分: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 Strict
或Option 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
完成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