Store value into Array & Write File
I wish that someone can help me with the following problem:
I have an integer and a button.
After clicking on the button, the integer value would be for example 10.
Then how could I store that value 10 into the memory, and when I click the button again, the new integer value now is 5. I'd like to add up the old and new value together and write it to a text file.
Thank you. Other way to achieve the same goal is appreciated. Please help.
Re: Store value into Array & Write File
where are you getting your integer values from?
user input? or constant values?
you could use a list(of integer):
vb Code:
Dim numbers As New List(Of Integer)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim number As Integer = 0
Integer.TryParse(InputBox("Enter an integer"), number)
If number > 0 Then
numbers.Add(number)
IO.File.WriteAllText("sum.txt", numbers.Sum.tostring)
End If
End Sub
Re: Store value into Array & Write File
Not knowing all the facts here is an example which shows a) storage b) calculation. The calculation is not what you specified but it gives you an idea how you might think about coding in general. There is no type checking either so for this demo only enter valid integers.
Code:
Private MyValues As New List(Of Integer)
Private Sub Button1_Click() Handles Button1.Click
MyValues.Add(CInt(TextBox1.Text))
End Sub
Private Sub Button2_Click() Handles Button2.Click
Dim Amount = (From Numbers In MyValues Select Numbers).Sum
Console.WriteLine(Amount)
End Sub
Private Sub Button3_Click() Handles Button3.Click
Dim Data As String = ""
For Each item In MyValues
Data &= item.ToString & Environment.NewLine
Next
My.Computer.FileSystem.WriteAllText("Numbers.txt", Data, False)
Process.Start("Numbers.txt")
End Sub