Norisor Marius
Norisor Marius

Reputation: 65

Operation with two string textboxes separated by a comma vb net

I have 2 strings of numbers: like this:

Textbox1.Text = 1,2,3,4,5,6,7,8,9,11
Textbox2.Text = 11,9,8,7,6,5,4,3,2,1

I want to calculate, display in textbox3, their sum, + or - or * or /

all value from textbox1.text + all values from textbox2.text

Textbox3.Text = 12,11,11,11,11,11,11,11,11,12

I would like, though, and a specification where I can change the sign, instead of + being -, or / or * depending on my choice.

this code is a bit cumbersome, I would like to improve it with something simpler.

    Dim str1 As String
    'Set your string value
    str1 = TxtBoxLstDrawsPlus.Text
    Dim str2 As String
    'Set your string value
    str2 = TxtBoxLstDrawsMinus.Text
    Dim strWords As String() = str1.Split(",")
    Dim strWordsAAA As String() = str2.Split(",")
    TextBox6.Text &= Val(strWords(0)) + Val(strWordsAAA(0)) & "," & Val(strWords(1)) + Val(strWordsAAA(1)) & "," & Val(strWords(2)) + Val(strWordsAAA(2)) & "," & Val(strWords(3)) + Val(strWordsAAA(3)) & "," & Val(strWords(4)) + Val(strWordsAAA(4)) & "," & Val(strWords(5)) + Val(strWordsAAA(5)) & "," & Val(strWords(6)) + Val(strWordsAAA(6)) & "," & Val(strWords(7)) + Val(strWordsAAA(7)) & "," & Val(strWords(8)) + Val(strWordsAAA(8)) & "," & Val(strWords(9)) + Val(strWordsAAA(9)) & "," & Val(strWords(10)) + Val(strWordsAAA(10)) & "," & Val(strWords(11)) + Val(strWordsAAA(11)) & "," & Val(strWords(12)) + Val(strWordsAAA(12)) & "," & Val(strWords(13)) + Val(strWordsAAA(13)) & "," & Val(strWords(14)) + Val(strWordsAAA(14)) & "," & Val(strWords(15)) + Val(strWordsAAA(15)) & "," & Val(strWords(16)) + Val(strWordsAAA(16)) & "," & Val(strWords(17)) + Val(strWordsAAA(17)) & "," & Val(strWords(18)) + Val(strWordsAAA(18)) & "," & Val(strWords(19)) + Val(strWordsAAA(19))

Upvotes: 1

Views: 640

Answers (1)

user10216583
user10216583

Reputation:

You can split the input strings (TextBox1.Text and TextBox2.Text), Zip the string arrays to produce a result array, and finally String.Join it in TextBox3:

Here's in one line:

TextBox3.Text = String.Join(",", TextBox1.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries).
    Zip(TextBox2.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries),
        Function(x, y) CInt(x) + CInt(y)))

You can use the same approach to do different arithmetic operations:

Dim opr As Char = "+"c '<- From your arithmetic operation selector like ComboBox.

TextBox3.Text = String.Join(",", TextBox1.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries).
    Select(Function(x) CInt(x)).
    Zip(TextBox2.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries).
    Select(Function(y) CInt(y)),
        Function(x, y)
            Select Case opr
                Case "-"c
                    Return x - y
                Case "*"c
                    Return x * y
                Case "/"c
                    Return x \ y
                Case Else
                    Return x + y
            End Select
        End Function))

It'll be a good idea if you'd validate your inputs first:

Dim Arr1() = TextBox1.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
Dim Arr2() = TextBox2.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
Dim opr As Char = OprTextBox.Text.ElementAtOrDefault(0) 'A TextBox this time as another example.
Dim ValidOpr() As Char = {"+"c, "-"c, "*"c, "/"c}

If Arr1.Length <> Arr2.Length OrElse
    Arr1.Any(Function(x) Not Integer.TryParse(x, Nothing)) OrElse
    Arr2.Any(Function(x) Not Integer.TryParse(x, Nothing)) OrElse
    Not ValidOpr.Contains(Opr) Then
    MessageBox.Show("Excuse me ... !?!?")
    Return
End If

'You have valid inputs. Proceed...

TextBox3.Text = String.Join(",", Arr1.Select(Function(x) CInt(x)).
    Zip(Arr2.Select(Function(x) CInt(x)),
        Function(x, y)
            Select Case opr
                Case "-"c
                    Return x - y
                Case "*"c
                    Return x * y
                Case "/"c
                    Return x \ y
                Case Else
                    Return x + y
            End Select
        End Function))

Better yet, enum the arithmetic operations:

Public Enum ArithmeticOperations
    Add
    Subtract
    Multiply
    Divide
End Enum

.. and create a parameterized Function that returns the joined string.

'You name it...
Public Function GetCalcString(input1 As String,
                                input2 As String,
                                opr As ArithmeticOperations) As String

    Dim Arr1() = input1.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
    Dim Arr2() = input2.Split({","c}, StringSplitOptions.RemoveEmptyEntries)

    If Arr1.Length <> Arr2.Length OrElse
        Arr1.Any(Function(x) Not Integer.TryParse(x, Nothing)) OrElse
        Arr2.Any(Function(x) Not Integer.TryParse(x, Nothing)) Then
        Return Nothing         
    End If

    Return String.Join(",", Arr1.Select(Function(x) CInt(x)).
                        Zip(Arr2.Select(Function(x) CInt(x)),
                            Function(x, y)
                                Select Case opr
                                    Case ArithmeticOperation.Subtract
                                        Return x - y
                                    Case ArithmeticOperation.Multiply
                                        Return x * y
                                    Case ArithmeticOperation.Divide
                                        Return x \ y
                                    Case Else
                                        Return x + y
                                End Select
                            End Function))
End Function

Assuming that, the arithmetic operations are listed in a ComboBox in the same order:

Private Sub TheCaller()
    TextBox3.Text = GetCalcString(TextBox1.Text,
                                  TextBox12.Text,
                                  CType(ComboBox1.SelectedIndex, ArithmeticOperations))
End Sub

Upvotes: 1

Related Questions