Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Calculator Help filter_list
Author
Message
Calculator Help #1
Hey guys! I'm in the process of creating a calculator with added functionality of figuring out the area, perimeter, and length of a square (and hopefully more polygons) given a single one of the above.

It works fine if I give it the length, but if I give it the area or perimeter it gives me 0 for each of my answers. I'm relatively new to VB.NET (about a week ago), and I'm looking for criticism. Here's my code:
Code:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim area As Single
        Dim peri As Single
        Dim length As Single
        If sqarea.Text = "" Then
            sqarea.Text = "0"
        End If
        If sqper.Text = "" Then
            sqper.Text = "0"
        End If
        If sqlth.Text = "" Then
            sqlth.Text = "0"
        End If
        area = sqarea.Text
        peri = sqper.Text
        length = sqlth.Text
        area = length * length
        sqarea.Text = (area & " units²")
        peri = length * 4
        sqper.Text = (peri & " units")
        If sqarea.Text = "" Then
            length = peri / 4
            sqlth.Text = (length & " units")
        End If
        If sqper.Text = "" Then
            length = Math.Sqrt(area)
            sqlth.Text = (length & " units")
        End If
    End Sub

Reply

RE: Calculator Help #2
I've never coded in VB, but I think the problem can be in data types. As I can see you're doing math operations with strings, try casting them into integers.
If VB is automatically casting strings to integers in math operations then all I wrote has no point.

You should also make sure your program won't crash when user types some text in input area. For example, what happens if user inputs "-string"?

Reply

RE: Calculator Help #3
(05-25-2014, 06:40 PM)Souvarine Wrote: I've never coded in VB, but I think the problem can be in data types. As I can see you're doing math operations with strings, try casting them into integers.
If VB is automatically casting strings to integers in math operations then all I wrote has no point.

You should also make sure your program won't crash when user types some text in input area. For example, what happens if user inputs "-string"?

From what I understand, since I defined length/area/peri as singles, VB automatically casts the strings from the textbox into singles.


And also, I don't know if you were being rhetoric or not, but yeah, it crashes if you enter a string it crashes. I'm trying to use
Code:
If textbox.text = string Then
textbox.text = "0"

But Visual Studio is giving me an error that "String is a class type and not an expression" and that I think it's a period that is expected.

Reply

RE: Calculator Help #4
I've been staring at this for the past 45 minutes, and I still have no idea what's causing the exception.

Code:
If ListBox1.SelectedItem.Equals("for Area") Then
            area = Convert.ToSingle(sqarea.Text)
            length = Convert.ToSingle(sqlth.Text)
            area = length * length
            sqarea.Text = (area & " units²")
        End If

VS is telling me that the exception is being caused in line three of my "if" statement, but I'm too big a newb to see the error.

Reply

RE: Calculator Help #5
Code:
If textbox.text = string Then
textbox.text = "0"
That code cant be right Smile
Even if that would compile and run, sequence '3423' is also a valid string.

You can check if entered string is valid number by making your own function. In that function you should check if every character is digit (with integrated function IsDigit) and is there only one period at the right place.
You can also choose to accept scientific notation or not (for example 6.02e+34).

There is third option to use regular expressions. You can find tons of these for checking valid numbers online, just use Google.

Reply

RE: Calculator Help #6
(05-25-2014, 06:40 PM)Souvarine Wrote: I've never coded in VB, but I think the problem can be in data types. As I can see you're doing math operations with strings, try casting them into integers.
If VB is automatically casting strings to integers in math operations then all I wrote has no point.

You should also make sure your program won't crash when user types some text in input area. For example, what happens if user inputs "-string"?

No, it has a point; Implicit casts are BAD.

(05-25-2014, 07:02 PM)Fish Wrote:
(05-25-2014, 06:40 PM)Souvarine Wrote: I've never coded in VB, but I think the problem can be in data types. As I can see you're doing math operations with strings, try casting them into integers.
If VB is automatically casting strings to integers in math operations then all I wrote has no point.

You should also make sure your program won't crash when user types some text in input area. For example, what happens if user inputs "-string"?

From what I understand, since I defined length/area/peri as singles, VB automatically casts the strings from the textbox into singles.


And also, I don't know if you were being rhetoric or not, but yeah, it crashes if you enter a string it crashes. I'm trying to use
Code:
If textbox.text = string Then
textbox.text = "0"

Yes, but that doesn't mean you should be doing that. You should not rely on implicit casts. And if you enter something that cannot be interpretted as the datatype you're trying to populate, then yes it will throw an exception. You should be using a TryParse() function for the validation.

Quote:But Visual Studio is giving me an error that "String is a class type and not an expression" and that I think it's a period that is expected.

Still entirely wrong, you don't compare types like this, you're trying to compare a value of a string and the class object here... That won't work.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Calculator Help #7
(05-25-2014, 07:50 PM)Souvarine Wrote: You can check if entered string is valid number by making your own function. In that function you should check if every character is digit (with integrated function IsDigit) and is there only one period at the right place.
You can also choose to accept scientific notation or not (6.02e+34).

There is third option to use regular expressions. You can find tons of these for checking valid numbers online, just use Google.

Wrong, although you could do this, functions already exist that are a million times better. Also, if you check every character to see if it's a digit, what about the decimal for floating point? What about data type limits? What about different numeric formats like hexadecimal input?
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Calculator Help #8
(05-25-2014, 07:51 PM)ArkPhaze Wrote:
(05-25-2014, 06:40 PM)Souvarine Wrote: I've never coded in VB, but I think the problem can be in data types. As I can see you're doing math operations with strings, try casting them into integers.
If VB is automatically casting strings to integers in math operations then all I wrote has no point.

You should also make sure your program won't crash when user types some text in input area. For example, what happens if user inputs "-string"?

No, it has a point; Implicit casts are BAD.

(05-25-2014, 07:02 PM)Fish Wrote:
(05-25-2014, 06:40 PM)Souvarine Wrote: I've never coded in VB, but I think the problem can be in data types. As I can see you're doing math operations with strings, try casting them into integers.
If VB is automatically casting strings to integers in math operations then all I wrote has no point.

You should also make sure your program won't crash when user types some text in input area. For example, what happens if user inputs "-string"?

From what I understand, since I defined length/area/peri as singles, VB automatically casts the strings from the textbox into singles.


And also, I don't know if you were being rhetoric or not, but yeah, it crashes if you enter a string it crashes. I'm trying to use
Code:
If textbox.text = string Then
textbox.text = "0"

Yes, but that doesn't mean you should be doing that. You should not rely on implicit casts. And if you enter something that cannot be interpretted as the datatype you're trying to populate, then yes it will throw an exception. You should be using a TryParse() function for the validation.

Quote:But Visual Studio is giving me an error that "String is a class type and not an expression" and that I think it's a period that is expected.

Still entirely wrong, you don't compare types like this, you're trying to compare a value of a string and the class object here... That won't work.

(05-25-2014, 07:58 PM)ArkPhaze Wrote:
(05-25-2014, 07:50 PM)Souvarine Wrote: You can check if entered string is valid number by making your own function. In that function you should check if every character is digit (with integrated function IsDigit) and is there only one period at the right place.
You can also choose to accept scientific notation or not (6.02e+34).

There is third option to use regular expressions. You can find tons of these for checking valid numbers online, just use Google.

Wrong, although you could do this, functions already exist that are a million times better. Also, if you check every character to see if it's a digit, what about the decimal for floating point? What about data type limits? What about different numeric formats like hexadecimal input?

Hey @ArkPhaze,
I think you're trying to help, which is really nice of you, but you are also coming off as really vicious.

Also, like I said in the beginning, I think I did mention that I am very new to Visual Basic.NET (and programming in general), so instead of just telling me that I'm doing most of it wrong, it would be really awesome if you could try to point me a little bit closer in the right direction.



I'm trying to use the TryParse() now, but it's still crashing. I understand that this is most likely wrong and not following the correct syntax, but how could I fix this?
Code:
TryToParse(textbox.text)


Thanks in advance

And now, it crashes no matter what.

Reply

RE: Calculator Help #9
(05-25-2014, 07:58 PM)ArkPhaze Wrote:
(05-25-2014, 07:50 PM)Souvarine Wrote: You can check if entered string is valid number by making your own function. In that function you should check if every character is digit (with integrated function IsDigit) and is there only one period at the right place.
You can also choose to accept scientific notation or not (6.02e+34).

There is third option to use regular expressions. You can find tons of these for checking valid numbers online, just use Google.

Wrong, although you could do this, functions already exist that are a million times better. Also, if you check every character to see if it's a digit, what about the decimal for floating point? What about data type limits? What about different numeric formats like hexadecimal input?

No! That is NOT wrong. There always can be better or worse way, but what I said is also working.

Of course the best way would be to use integrated functions that are almost perfect, but I don't know if they exist because I have never used Visual Basic!

So, chill out and give some advice if you wanna help, don't attack.

Reply

RE: Calculator Help #10
Code:
Public Class geometry

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Form1.Show()
        Me.Hide()

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        RichTextBox1.Text = ("A square is a four-sided (quadrilateral) polygon of congruent side and angle measure.  This means that each side length is the same and each angle is 90°.  A Square is a type of parallelogram.  a = s²  p = 4(s)")
    End Sub

    Private Sub sqarea_TextChanged(sender As Object, e As EventArgs) Handles sqarea.TextChanged

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim area As Single
        Dim length As Single
        If sqlth.Text = "" Then
            sqlth.Text = "0"
        End If
        If Not sqarea.Text Is DBNull.Value Then
            sqarea.Text = "0"
        End If
        area = (Single.Parse(sqarea.Text))
        length = (Single.Parse(sqlth.Text))
        area = length * length
        sqarea.Text = (area & " units²")
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        sqlth.Text = ("")
        sqarea.Text = ("")
        sqper.Text = ("")
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Dim length As Single
        Dim peri As Single
        If Not sqper.Text Is DBNull.Value Then
            sqper.Text = "0"
        End If
        If sqlth.Text = "" Then
            sqlth.Text = "0"
        End If
        peri = length * 4
        sqper.Text = (peri & " units")
    End Sub
End Class

I've separated the subs into different buttons (solve area, perimeter, length) and now perimeter is always giving me 0. If I do manage to find a solution, I'll post back, just documenting right now.

Edit: I'm a total moron, I forgot to define length as being equal to sqlth.Text.

Reply







Users browsing this thread: 1 Guest(s)