I have a program here that will solve an expression...
First I need to input the expression in a textbox. That will be stored in a CharArray and then substitute the variables to integer by using input boxes...
My problem is: How can I store the integer to an array and also store the operation?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim arr() As Char = TextBox1.Text.ToCharArray
Dim aChar As Char
Dim a As String
Dim calc() as String
'Me.Height = Me.Height + 90
Me.Button1.Enabled = False
Me.TextBox1.Enabled = False
For i = 0 To TextBox1.Text.Length() - 1
aChar = arr.ElementAt(i)
If Char.IsLetter(aChar) Then
a =开发者_运维百科 InputBox("Enter value for " & aChar, "Expression")
'Here what code?
' Try
' calc(i) = a
' Catch ex As Exception
' MsgBox("eee")
' End Try
'Else
End If
Next i
End Sub
I generally prefer to work with generic Lists, they make adding and removing items much easier. The code below should do what you need:
Dim arr() As Char = TextBox1.Text.ToCharArray
Dim aChar As Char
Dim a As String
Dim calc As New List(Of String)
Me.Button1.Enabled = False
Me.TextBox1.Enabled = False
For i = 0 To TextBox1.Text.Length() - 1
aChar = arr.ElementAt(i)
If Char.IsLetter(aChar) Then
a = InputBox("Enter value for " & aChar, "Expression")
''//Add the result to the list
calc.Add(a)
Else
''//Add the operator to the list
calc.Add(aChar)
End If
Next i
''//If you want to convert to list to an array you can use this
Dim CalcArray = calc.ToArray()
I haven't tested this or anything, but I think refactoring this a tad to use the For Each loop and either scoping or get rid of the a and aChar variables would be a little nicer approach:
Dim arr() As Char = TextBox1.Text.ToCharArray
Dim calc As New List(Of String)
Me.Button1.Enabled = False
Me.TextBox1.Enabled = False
For Each aChar As Char In arr
If Char.IsLetter(aChar) Then
''//Add the result to the list
calc.Add(InputBox(String.Format("Enter value for {0} Expression", aChar.ToString)))
Else
''//Add the operator to the list
calc.Add(aChar.ToString)
End If
Next
''//If you want to convert to list to an array you can use this
Dim CalcArray = calc.ToArray()
精彩评论