Results 1 to 11 of 11

Thread: Basic Calculator

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    6

    Question Basic Calculator

    Hi All,
    I'm very new to VB and hope you wont mind me asking what may seem a silly question to most of you.

    I am trying to create a form that adds two text boxes together, I have achieved this however when running the program if you forget to enter numbers in both boxes the program crashes!

    I dont want to display an error message i just want the program to ignore empty boxes.

    Is this easily achieved?

    Regards
    Chris

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    6

    Re: Basic Calculator

    This is the 'button code' I have written so far


    Dim x As Integer = Val(TextBox1.Text)
    Dim y As Integer = Val(TextBox2.Text)


    Mortage = TextBox1.Text
    Insurance = TextBox2.Text

    Label4.Text = Mortage + Insurance

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Basic Calculator

    Why are you converting the strings in the textbox in the first 2 lines of code when you don't bother to use the x and y again to do your math?

    Also, val returns a type double, so you are trying to do a narrowing conversion by converting the result of the VAL(textbox.text) method and squeezing a double type into an integer. Integers are only whole numbers, so if your insurance was 100.50, you would lose the decimal information.

    Try this:

    Code:
             Dim Mortage As Double = Val(TextBox1.Text)
            Dim Insurance As Double = Val(TextBox2.Text)
    
            Label4.Text = (Mortage + Insurance).ToString("0.00")
    This also does look like it is school work right? Tell your teacher you should be coding with option strict on, which wouldn't let you try to force doubles into integers in the first place.

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    6

    Re: Basic Calculator

    Hi kleinma,

    I can understand you assuming it is school work, however it isnt, im just a real Newbie and i am trying to learn.

    You reply is excellent and works really well,

    I really feel like I have learnt something.

    Regards
    Chris

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Basic Calculator

    well in that case tell yourself you should be coding with option strict on

    Seriously though, by turning on this feature, the IDE will force you to make sure conversions between types are done explicitly, so there is less chance of obscure bugs (like dropping of decimal places) or runtime errors (like you were seeing).

    Glad to hear it is working for you now.

  6. #6
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Basic Calculator

    A little more info:

    If you turned option strict on, you no doubt immediately became the target of an intelligent IDE giving you messages that seemed to be quite a pain.

    As Kleinma pointed out, with option strict on you have to overtly make the conversion. The IDE won't stop bothering you until you do. Why?

    The reason is that this: The IDE is basically pointing out the possible loss of accuracy (or other problems) to you. This gives you the chance to basically "reply" with "yes, I know I'm doing that. I've considered the ramifications of doing so, and I'm certain this is what I want to do. Thank you, IDE, for telling me about that potential problem."

    There may be other reasons that I'm not at the moment considering.

  7. #7
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Basic Calculator

    also think about using lowercase variable names as well. Don't think it's set in stone anywhere, but seems to be "rule of thumb".

    Constants all uppercase
    variables use "camel casing"
    Functions/Procedures and Classes use Uppercase first letter

    this is from what I've seen with working with OOP so far.

  8. #8
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Basic Calculator

    Actually he had the casing standard correct.

    It's parameters that are lower case:

    Code:
    Public Sub SomeSub(Byval parameter1 as Integer, Byval parameter2 as String)
    
    Dim LocalInteger as Integer = parameter1
    Dim LocalString as String = parameter2
    'Although you'd probably not set param1 and 2 to the local CamelCase variables
    'like that because they were passed by Value.
    
    End Sub
    Code:
    Public Function SomeFunction(Byval first as Textbox, Byval second as MyType) As Thing
    End Function
    In this case Mortgage and Insurance are correctly written.

  9. #9
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Basic Calculator

    However the OP's variables x and y should have been X and Y.

    Another standard for lower case is in looping variables.

    For i as integer...
    For each i as integer...

  10. #10

  11. #11
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Basic Calculator

    Honestly?

    Anyone can do as they please.
    This only comes into play when you anticipate that one day someone else may have to read your code. That's when a standard comes in.

    Hmmm...

    If the name, whatever it is, can be used "stand alone" then capitalize and make camel case.

    If the name, whatever it is, is found as a parameter then the first word would be lowercase and the second Proper; or if the name was just one word then lowercase.

    If the name is part of any counter, then lower if one word; else lower then upper camel if two words.

    In a class:
    If the name is a backing field then lower.
    If the name is a property, then upper and camel.
    If the name is anything else in a class then upper and camel.


    Whether it is Local or Class level does not matter.

    Some examples:

    Dim PeopleCounter as Integer...

    For Each person as Person in People...
    For Each fastCar as Car in Vehicles...

    ...(Byval one as Integer)...
    ...(Byval moreThanOne as String)...
    Private Function NameOfFunction (...) as Boolean
    Public Sub NameOfSub(...

    If NameOfFunction Then
    End If

    You can probably find the recommendations on MSDN

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