Login Register






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


Calculator Help filter_list
Author
Message
RE: Calculator Help #11
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
        If Not IsNumeric(sqlth.Text) Then
            sqlth.Text = "0"
            RichTextBox1.Text = ("Error 1:  Input was not a number.  Strings such as 'apple', 'banana', 'x', or 'y' will trigger this error.  Instead try and use a postive real number like '20', '310', or '20.5'.")

        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 = ("")
        RichTextBox1.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 Not IsNumeric(sqlth.Text) Then
            sqlth.Text = "0"
            RichTextBox1.Text = ("Error 1:  Input was not a number.  Strings such as 'apple', 'banana', 'x', or '2y' will trigger this error.  Instead try and use a postive real number like '20', '310', or '20.5'.")
        End If
        length = sqlth.Text
        peri = length * 4
        sqper.Text = (peri & " units")
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        RichTextBox1.Text = ("This figure will help you to solve for the side length(s), area, and perimeter of a square.  Fill in the length box with your length and click the 'Solve for Area' or 'Solve for Perimeter' to find the area or perimeter, respectively.  If you are solving for the length, fill in for the area or perimeter and click the 'Solve for Length, Given Area' or 'Solve for Length, Given Perimeter'.")
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim area As Single
        Dim length As Single
        If sqarea.Text = "" Then
            sqarea.Text = "0"
        End If
        If Not sqlth.Text Is DBNull.Value Then
            sqlth.Text = "0"
        End If
        If Not IsNumeric(sqarea.Text) Then
            sqarea.Text = "0"
            RichTextBox1.Text = ("Error 1:  Input was not a number.  Strings such as 'apple', 'banana', 'x', or 'y' will trigger this error.  Instead try and use a postive real number like '20', '310', or '20.5'.")

        End If
        area = (Single.Parse(sqarea.Text))
        length = (Single.Parse(sqlth.Text))
        length = Math.Sqrt(area)
        sqlth.Text = (length & " units")
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        Dim peri As Single
        Dim length As Single
        If sqper.Text = "" Then
            sqper.Text = "0"
        End If
        If Not sqlth.Text Is DBNull.Value Then
            sqlth.Text = "0"
        End If
        If Not IsNumeric(sqper.Text) Then
            sqper.Text = "0"
            RichTextBox1.Text = ("Error 1:  Input was not a number.  Strings such as 'apple', 'banana', 'x', or 'y' will trigger this error.  Instead try and use a postive real number like '20', '310', or '20.5'.")

        End If
        peri = (Single.Parse(sqper.Text))
        length = (Single.Parse(sqlth.Text))
        length = peri / 4
        sqlth.Text = (length & " units")
    End Sub
End Class

It works (as of now), including decimals. Biggrin

Reply

RE: Calculator Help #12
I did point you in the right direction, and there is no TryToParse() function within the .NET BCL. 'TryParse()', and it exists for lots of datatypes. I'm straight to the point, I don't add any icing to what I'm trying to get across, but I don't know how you got 'TryToParse' out of my suggestion 'TryParse'. I was waiting for you to look it up, as it's one of the most common methods for validation in .NET (meaning easy to find through a Google search), but here it is anyways: http://msdn.microsoft.com/en-us/library/26sxas5t.aspx

The IsNumeric() function and Parse() aren't going to guarantee that an exception will be thrown, which is why I've already mentioned TryParse() in my last post. None of the other suggestions above will be as good as TryParse(), and I can give you a few ways that most of these other methods will fail...

And in addition to the fact that TryParse() achieves what IsNumeric() and Parse() does, plus a little more, and is guaranteed to not throw an exception for invalid input (the whole point of this function), it is the way to go here. It accounts for the datatype size limits, invalid characters, and allows for multi-format input.

@"Souvarine" - It's nice that you're trying to help, but if you've never used VB.NET before then how can you really help? Moral support? No offense, but if you don't know the language, your suggestions may not always be the best is my point. This thread is an example of where it is not the greatest suggestion that you've posted.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Calculator Help #13
I'll try out TryParse() now, and I'm really sorry for using TryToParse(). I actually did Google TryParse, but I guess my Google-fu is a little rusty and I ended up using TryToParse without realizing.

Thanks for the help!

@ArkPhaze

Is this the correct syntax of TryParse()?

Code:
If Not Single.TryParse(trb.Text, base) Then
            trb.Text = "0"
            RichTextBox2.Text = ("Error 1:  Input was not a number.  Strings such as 'apple', 'banana', 'x', or 'y' will trigger this error.  Instead try and use a postive real number like '20', '310', or '20.5'.")

        End If

I just used Single.TryParse(string, single) in place of my Parse's and IsNumeric's and it gave me totally wrong answers.

Reply

RE: Calculator Help #14
(05-25-2014, 07:47 PM)Fish Wrote: 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.
I know next to nothing about VB but that looks like a flawless section.
Have you tried breaking the code down and compiling it in smaller parts? Might not work but it's worth a shot.
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.

Reply

RE: Calculator Help #15
@"Jinxed" - It has the potential to throw an exception (that code you quoted).

Since you guys are struggling with this, I'll provide a bit more assistance... @"Fish" -- If the variable [base] is the Single datatype variable that you want the result to be stored in, and trb.Text is the input that you want to parse, then yes that's the correct usage.

The first parameter to the TryParse() function is passed by value, the second one is passed in by reference (ByRef), meaning it gets changed after the function call. If TryParse() returns True, the variable passed in as the second parameter will contain the parsed value from the input string (the first parameter). Therefore, it works like the IsNumeric() function (almost (it is better)), but it also does what your Parse() call does at the same time. MUCH more convenient, and MUCH more reliable.

Code:
Dim result As Single
      If Not Single.TryParse(stringInput, result) Then
         ' Parsing was a FAILURE...
         ' The variable [result] won't contain the parsed value
      Else
         ' Parsing was a SUCCESS...
         ' The variable [result] now contains the parsed value
      End If

How did it give you a wrong answer though? Let's see what your code looked like when you had used TryParse()... You're doing something wrong, but I can't help until I see.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Calculator Help #16
Try this:
[
Code:
vbnet]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 Not Single.TryParse(sqarea.Text, area) Then
      sqarea.Text = "0"
      Exit Sub
   End If
   If Not Single.TryParse(sqper.Text, peri) Then
      sqper.Text = "0"
      Exit Sub
   End If
   If Not Single.TryParse(sqlth.Text, length) Then
      sqlth.Text = "0"
      Exit Sub
   End If

   area = length * length
   sqarea.Text = area & " units²"
   peri = length * 4
   sqper.Text = peri & " units"
   If sqarea.Text = String.Empty Then
      length = peri / 4
      sqlth.Text = length & " units"
   End If
   If sqper.Text = String.Empty Then
      length = CSng(Math.Sqrt(area))
      sqlth.Text = length & " units"
   End If
End Sub

Didn't really change anything else, just showed you the syntax for the TryParse() calls. And if you want a lower precision, you can stick with Single, but functions within the Math class typically return Double, so you needed a cast in there as well for the Sqrt() function.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Calculator Help #17
(05-26-2014, 12:05 AM)ArkPhaze Wrote: ...

@"Souvarine" - It's nice that you're trying to help, but if you've never used VB.NET before then how can you really help? Moral support? No offense, but if you don't know the language, your suggestions may not always be the best is my point. This thread is an example of where it is not the greatest suggestion that you've posted.

I know my suggestions aren't the best, but not knowing the language doesn't need to be a barrier.
I'm also aware that my suggestions are not the greatest, but some programming concepts can be used in almost every programming language.

In this case, my suggestions obviously are not good, but my personal opinion is they can be good practice for better understanding of programming in general.

To close this case, I was just trying to help even tho I'm not familiar with programming language.
No hard feelings Smile

Reply

RE: Calculator Help #18
(05-26-2014, 10:03 AM)Souvarine Wrote: I know my suggestions aren't the best, but not knowing the language doesn't need to be a barrier.
I'm also aware that my suggestions are not the greatest, but some programming concepts can be used in almost every programming language.

In this case, my suggestions obviously are not good, but my personal opinion is they can be good practice for better understanding of programming in general.

To close this case, I was just trying to help even tho I'm not familiar with programming language.
No hard feelings Smile

And it isn't a barrier, but it's the reason why bad suggestions exist truthfully. If you're a master at Haskell, then stick with what you know, but even myself, I wouldn't go around posting help for languages that I've never even used. People that don't know C++ shouldn't be trying to teach it for instance, just like people that aren't electricians shouldn't be fooling around with high voltage, it applies everywhere. C and C++ are alike to people that don't know the languages, but for people that truly understand their differences, they are not the same at all. This question isn't related to the fundamentals that apply to all programming languages IMO. Conditional logic and control statements, sure, but this isn't, which is ALL that I'm getting at.

He doesn't want to understand programming in general, if he did he would've posted a theoretical question. Instead, he's having a particular issue with C# validation. Might just be me, but if I were OP, I wouldn't want all of the philosophical and theoretical BS and I'd rather know how to properly validate input in the language I wanted help in. Validation just from the languages I know, is not all the same, and it's probably the worst in a language like C++. There are things in certain languages that are good, but in terms of another language, are really bad, and this is the main reason why I don't try to consolidate programming into a general concept. The way bits work and what happens with the processor doesn't really change. Conditions and control statements just the same, yet the way conditions work from some languages to another still varies slightly.

I am fairly direct with my posts, as I've always been, but this is my main point in a nutshell.

To make this post somewhat helpful, I'll explain why checking the characters of the string is a bad idea.

1. let a 32 bit signed integer be the datatype in this case...
2. Checking the input string for digits only, assume an input of '9999999999999999'
3. Character validation was successful
4. During the cast, this value obviously exceeds the datatype limits and an OverflowException occurs
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply







Users browsing this thread: 1 Guest(s)