Results 1 to 9 of 9

Thread: Memory buttons in calculator

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    45

    Memory buttons in calculator

    I'm making a basic calculator, so far I have programmed the addition, subtraction, multiplication and division without any problems. I want to add a memory, memory recall and memory clear button but I'm not sure how to go about them.

    Here is the code I have for the other buttons so far.

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim num1, num2, ans
            Try
                num1 = Val(TextBox1.Text)
                num2 = Val(TextBox2.Text)
                ans = num1 + num2
                MessageBox.Show(ans)
            Catch ex As Exception
                MessageBox.Show(Err.Description)
            End Try
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim num1, num2, ans
            Try
                num1 = Val(TextBox1.Text)
                num2 = Val(TextBox2.Text)
                ans = num1 - num2
                MessageBox.Show(ans)
            Catch ex As Exception
                MessageBox.Show(Err.Description)
            End Try
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim num1, num2, ans
            Try
                num1 = Val(TextBox1.Text)
                num2 = Val(TextBox2.Text)
                ans = num1 * num2
                MessageBox.Show(ans)
            Catch ex As Exception
                MessageBox.Show(Err.Description)
            End Try
        End Sub
    
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            Dim num1, num2, ans
            Try
                num1 = Val(TextBox1.Text)
                num2 = Val(TextBox2.Text)
                ans = num1 / num2
                MessageBox.Show(ans)
            Catch ex As Exception
                MessageBox.Show(Err.Description)
            End Try
        End Sub
    End Class
    Could somebody give me the code for the memory buttons?

    (Thanks)

  2. #2
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Re: Memory buttons in calculator

    Just think about what you're trying to do. What exactly do you want to happen when you click the memory button? You want the answer to be stored in the memory? If so then what would happen?
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    45

    Re: Memory buttons in calculator

    What would happen is the answer would be saved somehow. Sorry, my mind is a little blank.

  4. #4
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Re: Memory buttons in calculator

    So have a variable for memory. You should have all your variables declared as something specific. In your code snippet num1, num2 and ans don't have an as clause. You should set Option Strict On, then it wouldn't allow what you are doing there.

    Code:
    Option Strict On
    Public Class Form1
       Dim num1, num2, ans, memory As Integer ' or if not Integer then whatever you want. Double, Single, etc. 
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    You don't need to keep on declaring the variables for every different button. You can just declare them once at the top and then set the values of the variables within the different button click events.
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    45

    Re: Memory buttons in calculator

    Thanks for pointing that out, never noticed that.

    I understand I need a variable for memory but I just don't have a clue on how to code it.

  6. #6
    Hyperactive Member
    Join Date
    Apr 2011
    Posts
    268

    Re: Memory buttons in calculator

    well i believe u need to store it in a file, is that what u want?

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    45

    Re: Memory buttons in calculator

    Is there anyway to store it without putting it in a file?

  8. #8
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Memory buttons in calculator

    The data has to go somewhere, whether it is a file or database. Even the built in settings for Visual Studio (which is what I recommend for this) use an XML file in the Users folder. With that said, you should start here.

  9. #9
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Memory buttons in calculator

    If the memory function needs to remember the textbox values until the next time your calculator is run then you will need to use a file/application setting. Otherwise you can use a Global Variable. Something like this should do the job:

    VB Code:
    1. Dim Memory As New List(Of Integer) 'Global Variable
    2.  
    3. Sub RestoreMemory() 'Use your button click events (these subs are just for the example)
    4.     If Memory.Count = 0 Then Exit Sub
    5.     textbox1.text = Memory(0)
    6.     textbox2.text = Memory(1)
    7. End Sub
    8.  
    9. Sub ClearMemory()
    10.     Memory.Clear()
    11. End Sub
    12.  
    13. Sub SaveMemory()
    14.     Memory.Add(textbox1.text)
    15.     Memory.Add(textbox2.text)
    16. 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