使用select case程序计算VB.NET中的加法减法 [英] Using select case program to calculate addition subtraction in VB.NET

查看:156
本文介绍了使用select case程序计算VB.NET中的加法减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是Vb的新手,请指导我如何在vb.net中使用案例获得加法和减法的结果。

先谢谢



我尝试过:



Hi All,
I am New to Vb,please guide me how to get the result of addition and subtraction using case in vb.net.
Thanks in Advance

What I have tried:

Public Class Form1

    Public Function Calculator(ByVal strMod As String)
        Dim Num1 As Integer = TextBox1.Text
        Dim Num2 As Integer = TextBox2.Text
        Dim Num3 As Single = TextBox3.Text
        Select Case strMod
            Case Calculator("Add")
                Num3 = Num1 + Num2
            Case Calculator("Sub")
                Num3 = Num1 - Num2
        End Select
        Return Num3
    End Function

    Public Sub Output_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim result As Integer = Calculator("Sub")
        MessageBox.Show(Convert.ToString(result), "Result")
    End Sub

    Public Sub Clear_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Close()
    End Sub
End Class

推荐答案

嗯,在我看来,你做错了。



至于功能体......

从编程实践的角度来看,功能应该尽可能地普及。因此,它可以采用零个,一个或多个参数,但它不应该引用表单上的控件!



Well, in my opinion you're doing it wrong way.

As to the body of function...
From programming practice perspective, function should be as much universal as possible. So, it can take zero, one or more parameters, but it shouldn't refer to the controls on the form!

Public Function Calculator(ByVal strMod As String)
    Dim Num1 As Integer = TextBox1.Text
    Dim Num2 As Integer = TextBox2.Text
    Dim Num3 As Single = TextBox3.Text
    Select Case strMod
        Case Calculator("Add")
            Num3 = Num1 + Num2
        Case Calculator("Sub")
            Num3 = Num1 - Num2
    End Select
    Return Num3
End Function





你的功能应该使用3个参数:



Your function should use 3 parameters:



  1. 一个数字,它存储实际结果(默认为零) )
  2. 一个数字,用于进一步计算
  3. 一个符号[ + ],[ - ],[ * ]或[ / ] - 又名数学运算符



  1. a number, which stores actual result (zero by default)
  2. a number, which is used for further calculation
  3. a sign [+], [-], [*] or [/] - aka mathematical operator







Public Function Calculate(ByVal totalValue As Single, ByVal currentValue As Single, ByVal mathOperator As String)
    Dim retVal as Single = 0

    'here your logic
    Select Case mathOperator
        Case "+"
            retVal = totalValue + currentValue 
        Case "-"  
            retVal = totalValue - currentValue 
        Case "*"
            retVal = totalValue * currentValue 
        Case "/"  
            retVal = totalValue / currentValue 
    End Select
 
    Return retVal
End Function





用法:

假设你有一个按钮用于加法运算[+],一个用于减法运算[ - ]等,但是所有按钮引用相同的单击事件(请参阅:如何:连接Mult Windows窗体中单个事件处理程序的iple事件 [ ^ ])



Usage:
Let's say, you have one button for addition operation [+], one for subtraction operation [-], etc, but all buttons refer to the same Click event (see: How to: Connect Multiple Events to a Single Event Handler in Windows Forms[^])

Private Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click, ButtonSub.Click, ...
Dim tot As Single = Convert.ToSingle(Me.TextBoxResult.Text)
Dim cur As Single  = Convert.ToSingle(Me.TextBoxValue.Text) 
Dim moper As String = TryCast(sender, Button).Text '
TextBoxResult.Text = Calculate(tot, cur, moper)
End Sub





如你所见,我使用了 Convert.ToSingle() [ ^ ]将字符串转换为单个字符串的方法。



以下是MSDN示例: VB.NET for Visual Studio 2008中的Windows VB.Net Calculator示例 [ ^ ]



但是,最后,你需要数学表达式求值器/解析器。请参阅:

简单数学解析器 - 主页 [ ^ ]

快速轻量级表情评估器 - 主页 [ ^ ]

< a href =http://ilcalc.codeplex.com/> ILCalc:Arithmetical Expressions Evaluator - Home [ ^ ]

NCalc - .NET的数学表达式评估器 - 主页 [ ^ ]

重新访问表达式计算器(100%托管.NET中的Eval函数) [ ^ ]

.NET的计算引擎 [ ^ ]



As you can see, i used Convert.ToSingle()[^] method to convert string into single.

Here is MSDN example: Windows VB.Net Calculator sample in VB.NET for Visual Studio 2008[^]

But, finally, you need mathematical expression evaluator/parser. See:
Simple Math Parser - Home[^]
Fast Lightweight Expression Evaluator - Home[^]
ILCalc: Arithmetical Expressions Evaluator - Home[^]
NCalc - Mathematical Expressions Evaluator for .NET - Home[^]
The expression evaluator revisited (Eval function in 100% managed .NET)[^]
A Calculation Engine for .NET[^]


你错过的东西很少

- The函数应该有一个返回类型

- 而不是检查参数,你在案例结构中递归调用计算器函数



尝试类似于以下

There are few things you have missed
- The function should have a return type
- Instead of checking the parameter you call the Calculator function recursively in the case structure

Try something like the following
Public Function Calculator(ByVal strMod As String) As Single
    Dim Num1 As Integer = TextBox1.Text
    Dim Num2 As Integer = TextBox2.Text
    Dim Num3 As Single = TextBox3.Text

    Select Case strMod
        Case "Add"
            Num3 = Num1 + Num2
        Case "Sub"
            Num3 = Num1 - Num2
    End Select
    Return Num3
End Function



另请注意:

- 不确定为什么要从textbox3为num3分配值。如果Textbox3用于结果,你应该反过来吗

- 而不是为操作传递一个字符串我建议使用枚举。请参阅枚举声明(Visual Basic) [ ^ ]

- 如果你愿意,它可能会更优雅将num1和num2作为参数传递给函数。这样,该功能不依赖于用户界面对象,可以用于不同类型的对象和情况。



[已添加]

用于将值赋给TextBox3


Also note:
- not sure why you assign a value to num3 from the textbox3. Should you do it the other way around if Textbox3 is for the result
- Instead of passing a string for the operation I'd suggest using enumeration. See Enum Statement (Visual Basic)[^]
- and it would probably be more elegant if you would pass num1 and num2 as parameters to the function. This way the function wouldn't depend on the user interface objects and could be used for different kinds of objects and situations.

[ADDED]
For assigning the value to TextBox3

Public Sub Output_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox3.Text = Calculator("Sub")
End Sub





也熟悉调试程序。例如,通过使用调试器浏览代码 [ ^ ]


这篇关于使用select case程序计算VB.NET中的加法减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆