Home > Net >  Q. Accept two numbers using an InputBox in two different variables and print the sum of two in a mes
Q. Accept two numbers using an InputBox in two different variables and print the sum of two in a mes

Time:11-24

I have to answer this in Visual Basic.I actually don't have any idea how to solve this, our teacher barely teaches us practical stuff. I have to submit this assignment by today too.

I have tried to do solve it and searched the internet for it, but I could barely understand it.

CodePudding user response:

Here's an example using Integer.TryParse():

Dim value1, value2 As Integer
Dim response As String = InputBox("Enter first Integer:")
If Integer.TryParse(response, value1) Then
    response = InputBox("Enter second Integer:")
    If Integer.TryParse(response, value2) Then
        Dim sum As Integer = value1   value2
        MessageBox.Show("The sum of " & value1 & " and " & value2 & " is " & sum)
    Else
        MessageBox.Show(response & " is not a valid Integer!")
    End If
Else
    MessageBox.Show(response & " is not a valid Integer!")
End If

CodePudding user response:

Here is some code you can build upon:

Public Sub AddTwoNumbers()
     Dim FirstNumber As String = Convert.toInt32(InputBox("Enter the first number.")) 'Get the first number
     Dim SecondNumber As String = Convert.toInt32InputBox("Enter the second number.")) 'Get the second number
     Dim Result As Integer = 0 'Used to store the result in
     'Now perform the calculation.
     Result = FirstNumber   SecondNumber
     'Then show the result in a MessageBox
     MessageBox.Show("The result is: " & Result.ToString())
End Sub
  • Related