Results 1 to 3 of 3

Thread: Store value into Array & Write File

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    5

    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.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    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:
    1. Dim numbers As New List(Of Integer)
    2.  
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.     Dim number As Integer = 0
    5.     Integer.TryParse(InputBox("Enter an integer"), number)
    6.     If number > 0 Then
    7.        numbers.Add(number)
    8.        IO.File.WriteAllText("sum.txt", numbers.Sum.tostring)
    9.     End If
    10. End Sub

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width