Click here to Skip to main content
15,891,723 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

VB.NET
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
Posted
Updated 10-May-18 21:10pm
v3
Comments
[no name] 25-Sep-16 14:32pm    
What, exactly, is it that you think "Case Calculator("Add")" is supposed to do?

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!

VB.NET
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


Your function should use 3 parameters:

  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



VB.NET
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


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[^])
VB.NET
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


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[^]
 
Share this answer
 
Comments
Ralf Meier 26-Sep-16 10:16am    
Very good solution because it explains how the OP should work in future. +5 from me !!!

But ... you have a little mistake in your code (of cause - vb doesn't fail with it - but for learning it is better)
You wrote :
TextBoxResult.Text = Calculate(tot, cur, moper)
the method calculate delivers a single as result and the TextBox awaits a string - so the code should better be :
TextBoxResult.Text = Calculate(tot, cur, moper).toString
Maciej Los 26-Sep-16 10:31am    
Thank you, Ralf.
Well, this line TextBoxResult.Text = Calculate(tot, cur, moper) should work even without ToString() method, but... You're right. Good point!
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
VB.NET
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

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
VB.NET
Public Sub Output_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox3.Text = Calculator("Sub")
End Sub


Also get familiar with debugging the program. For example go through Navigating through Code with the Debugger[^]
 
Share this answer
 
v3
Comments
Member 12605293 25-Sep-16 14:45pm    
Hi Mika Wendelius
Thanks for your quick reply,yes I have used textbox3 for the result and I agree using all the variable as a parameter inside the calculator function.My code is running without any error and I wonder Why it is not giving the expected output in(texbox3)?
Wendelius 25-Sep-16 15:00pm    
It doesn't show the output in textbox3 because you're not assigning anything into it.
Have a look at the modified answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900