Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Trying to learn VB.Net, problem with my simple calculator

Master Disaster
 Share

I learned Visual Basic 6 back in college (so over 10 years ago) and I really enjoyed it but I haven't really bothered with it since I left. Now I want to get back into it so I joined an online course to learn Visual Basic 2015 (my understanding is that 13, 15 & 17 are all almost identical anyway) and I have worked through the first 4 chapters however I decided to go off piste and try to create a calculator as I rememer this was the first project we did at college.

 

So I got a basic calculator working within a few hours using Integer as not to complicate things, there wasn't really any problems at all. Now I have decided to upgrade it to support decimal points and I cannot get it working properly at all.

 

Before we go any furthur can I say that I am aware my code is probably VERY unoptimised and I am writing absolute code for things which probably already have built it methods. As of now I am still very unfamiliar with .Net methods, remember I learned on VB6 when absolute code was required and the course I am doing hasn't touched the subject yet. If you are going to help please try and keep it within my level, I have googled this problem and found an almost identical situation on Microsofts help forums however the fix uses methods I simply do not understand yet so I cannot incorporate them in to my project.

 

Heres the issue.

 

I have all calculation variables defined as Doubles because I am not sure of the difference between a double and a decimal yet.

 

This is the code for when a numerical button is pushed

Private Sub btnCalc2_Click(sender As Object, e As EventArgs) Handles btnCalc2.Click
        'check if text box is 0, if so replace 0 with value otherwise concatenate value
        If txtCalc.Text = 0 Then
            txtCalc.Text = 2
        Else
            txtCalc.Text = txtCalc.Text & 2
        End If
    End Sub

I have do it this way to check if the textbox contains zero or not and either replace or concatenate based on the result.

 

Heres the code for my decimal point button

Quote

    Private Sub txtPoint_Click(sender As Object, e As EventArgs) Handles txtPoint.Click
        txtCalc.Text = txtCalc.Text & "."
    End Sub

I am sure you've probably already seen the problem here. When I run the calc I can add points to any number that doesn't start with zero (so for example 1.2 works perfectly) but if I try to add a point to a number starting with zero (so 0.2 for example) then as soon as I press 2 the 0. disappears and it replaced by 2.

 

My solution to this issue was to use an ElseIf then use the Text Box Contains function to check for the presence of a . and if one is present add it to the concatenation, as follows

Private Sub btnCalc1_Click(sender As Object, e As EventArgs) Handles btnCalc1.Click
        'check if text box is 0, if so replace 0 with value otherwise concatenate value
        If txtCalc.Text = 0 Then
            txtCalc.Text = 1
        ElseIf txtCalc.Text.Contains(".") Then
            txtCalc.Text = txtCalc.Text & "." & 1
        End If
    End Sub

Unfortunately this doesn't work and I cant for the life of me figure out why.

 

Please help a learner out. Thanks all.

Main Rig:-

Ryzen 7 3800X | Asus ROG Strix X570-F Gaming | 16GB Team Group Dark Pro 3600Mhz | Corsair MP600 1TB PCIe Gen 4 | Sapphire 5700 XT Pulse | Corsair H115i Platinum | WD Black 1TB | WD Green 4TB | EVGA SuperNOVA G3 650W | Asus TUF GT501 | Samsung C27HG70 1440p 144hz HDR FreeSync 2 | Ubuntu 20.04.2 LTS |

 

Server:-

Intel NUC running Server 2019 + Synology DSM218+ with 2 x 4TB Toshiba NAS Ready HDDs (RAID0)

Link to comment
Share on other sites

Link to post
Share on other sites

I've never user VB, let alone VB.net, but just guessing from my experience with other languages, the problem stems from the comparison at:

29 minutes ago, Master Disaster said:

If txtCalc.Text = 0 Then

You're comparing the String "0." to integer value 0 -- well, "0." when converted to an integer is the same thing as 0.0, which is the same thing as 0, so the comparison is always going to be true. A string-value of e.g. "1." would be converted to int-value of 1.0, which obviously isn't 0.

 

Moving the check for the period first should allow you to get the result you want:

Private Sub btnCalc1_Click(sender As Object, e As EventArgs) Handles btnCalc1.Click
        'check if text box is 0, if so replace 0 with value otherwise concatenate value
        If txtCalc.Text.Contains(".") Then
            txtCalc.Text = txtCalc.Text & "." & 1
        ElseIf txtCalc.Text = 0 Then
            txtCalc.Text = 1
        End If
    End Sub

Another option would be to add a comparison like e.g.:

29 minutes ago, Master Disaster said:

If txtCalc.Text.Equals("0.") Then

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

Do say, if I wasn't clear enough or if you'd like more help. Like I said, never used VB.net, but I may still be able to offer some insight.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, WereCatf said:

I've never user VB, let alone VB.net, but just guessing from my experience with other languages, the problem stems from the comparison at:

You're comparing the String "0." to integer value 0 -- well, "0." when converted to an integer is the same thing as 0.0, which is the same thing as 0, so the comparison is always going to be true. A string-value of e.g. "1." would be converted to int-value of 1.0, which obviously isn't 0.

 

Moving the check for the period first should allow you to get the result you want:


Private Sub btnCalc1_Click(sender As Object, e As EventArgs) Handles btnCalc1.Click
        'check if text box is 0, if so replace 0 with value otherwise concatenate value
        If txtCalc.Text.Contains(".") Then
            txtCalc.Text = txtCalc.Text & "." & 1
        ElseIf txtCalc.Text = 0 Then
            txtCalc.Text = 1
        End If
    End Sub

Another option would be to add a comparison like e.g.:

Thank you so much. So my code was good but the order was bad.

Main Rig:-

Ryzen 7 3800X | Asus ROG Strix X570-F Gaming | 16GB Team Group Dark Pro 3600Mhz | Corsair MP600 1TB PCIe Gen 4 | Sapphire 5700 XT Pulse | Corsair H115i Platinum | WD Black 1TB | WD Green 4TB | EVGA SuperNOVA G3 650W | Asus TUF GT501 | Samsung C27HG70 1440p 144hz HDR FreeSync 2 | Ubuntu 20.04.2 LTS |

 

Server:-

Intel NUC running Server 2019 + Synology DSM218+ with 2 x 4TB Toshiba NAS Ready HDDs (RAID0)

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Master Disaster said:

 

 

I have all calculation variables defined as Doubles because I am not sure of the difference between a double and a decimal yet.

 

For simplicity of things at your stage of learning all you need to remember are these specific points as for the difference.

The decimal type is 128 bit memory and use decimal representation for all numbers so you will never lose accuracy like using floating points

The double type is 64 bit and is double floating point therefore is subject to floating point error as the number are stored in memory as scientific notation

The single type is 32 bit and is single floating point, much smaller than double but same issues with precisions.

Link to comment
Share on other sites

Link to post
Share on other sites

55 minutes ago, Franck said:

The decimal type is 128 bit memory and use decimal representation for all numbers so you will never lose accuracy like using floating points

The fact it's limited to 128-bits means it's still prone to rounding errors. However, the values are so much smaller this time that for many practical purposes it doesn't matter.

 

It's likely any serious calculator sort of application these days actually employ some sort of arbitrary precision format to avoid this.

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, M.Yurizaki said:

The fact it's limited to 128-bits means it's still prone to rounding errors. However, the values are so much smaller this time that for many practical purposes it doesn't matter.

 

It's likely any serious calculator sort of application these days actually employ some sort of arbitrary precision format to avoid this.

What is serious calculator to you ? to me it's MathLab and it still have the floating point errors.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Franck said:

What is serious calculator to you ? to me it's MathLab and it still have the floating point errors.

Any application used widely in professional settings.

 

MatLab probably has rounding errors because it likely uses the IEEE floating point standard by default. Most hardware that crunches numbers demands a standardized format. If accuracy is of utmost importance, MatLab supports arbitrary precision: https://www.mathworks.com/help/symbolic/vpa.html

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×