Results 1 to 11 of 11

Thread: Need help with calculator

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    9

    Exclamation Need help with calculator

    Hello!!

    First of all, I am not asking you guys to do my homework for me but i am stuck at arithmetics so I would appreciate any guidance.

    We are asked to design a rent calculator for Shoreline and Wavecrest communities. below are the specifications:

    - Shoreline adds $140
    - Fireplace adds $27
    - Smoking subtracts $11

    I wrote the code for these basic features but I am stuck on how to add fireplace to Shoreline price or subtract smoking. I can write a statement for each operation but our teacher wants the computer to do all the calculations. He says the fewer, the better.

    here's what i've got so far and I would appreciate any hints.
    Code:
        Private Sub btnRent_Click(sender As Object, e As EventArgs) Handles btnRent.Click
            If cboBuilding.Text = "Shoreline" Then
                lblDisplay.Text = txtBaseRent.Text + 140
            End If
    
            If chkFireplace.Checked = True Then
                lblDisplay.Text = txtBaseRent.Text + 27
            End If
    
            If chkNonSmoking.Checked = True Then
                lblDisplay.Text = txtBaseRent.Text - 11
            End If
    
        End Sub

    many thanks in advance!!!
    Last edited by Shaggy Hiker; Nov 18th, 2017 at 09:31 AM. Reason: Removed Italics in favor of CODE tags.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help with calculator

    Firstly, if you're doing mathematics then do it on numbers, not Strings. Convert the base rent value from the TextBox into a number. The simplest way to do that is with CInt if it's a whole number. That will fail if the text isn't a valid Integer so you'll need to look for something more robust if you can't make that assumption. You would then use your three If statements and, in each case, add to your numeric variable. That way, your variable will increase in value one, two or three times as required. As you have it, you can never actually add more than one additional value because you ignore what went before. When you've done all that, THEN you display the final numeric value in the Label. To be most correct, you should convert it to a String first. That will happen implicitly by default, but anyone with experience will tell you that it's a good habit to get into to make all your conversions explicit. If you turn Option Strict On then you'll be forced to do that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    9

    Re: Need help with calculator

    The teacher said not to worry about numbers and assume that it would always be a whole number. Would I still have to convert it then? Or leave it as it is?

    How do i make it so that it adds $27 to the Shoreline value? I wrote the statement but I was wondering if there was a simpler, shorter way to do it? He said "let the computer do the math" which is why I am confused..

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help with calculator

    That is often the way with beginner assignments because they want you to concentrate and what you're trying to do rather than validating data. In that case, you can safely assign the Text property of your TextBox, which is a String to an Integer variable and also assign that Integer variable to the Text property of a Label. The conversion from String to Integer and back to String will happen implicitly.

    You should still be doing your arithmetic on an Integer variable though and, once you have that variable, how you got the value is irrelevant. The arithmetic is still the same and I've already told you what to do. DO NOT display the result until AFTER ALL the logic and arithmetic is complete. Check one condition and update the variable if appropriate. Check the second condition and update the variable if appropriate. Do the same for the third condition. The variable will be updated zero, one, two or three times as required. Once you've made all the changes you need to to that one variable, THEN you display its value.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Member
    Join Date
    Oct 2016
    Posts
    32

    Re: Need help with calculator

    Like others have mentioned I would parse the text to an integer at least.
    Hint: Integer.Parse() or Integer.TryParse().

    if its short code you want I guess its doable although short code doesn't always make for better code.
    I changed some of the names and values so its not exactly what you want, modify as needed.

    txtBaseRent.Text = if(cbo.Text = "PentHouse", 4000, 0) + if(FP.Checked = True, 440, 0) + if(NS.Checked = True, -50, 0)

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help with calculator

    Quote Originally Posted by TizzyT View Post
    Like others have mentioned I would parse the text to an integer at least.
    Hint: Integer.Parse() or Integer.TryParse().
    In "proper" code I would agree but if a teacher sets you an assignment and tells you to assume that all input will be valid then that's what you should do.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Need help with calculator

    I edited your original post to remove the italics and replace those tags with [CODE][/CODE] tags. The CODE tags nicely format the code (or at least a bit nicer format). Since you used Italics, you already know about the buttons. The CODE tags are the # button. The VB button (looks like VE on some browsers) adds color and numbering, so that's an option, as well.


    I'm not sure that I agree with that last post. Normally, I'd be all for proper conversion, but in this case, why not just use a NumericUpDown control and get the Value property? Sure, you'd still have to do a conversion, since the value from a NUD is a Decimal, but CInt would be totally safe for that. It seems like a change such as that might show initiative, even though the teacher is allowing you to be a bit sloppy. Unless your teacher REQUIRES you to use a Textbox, you should really consider using a NUD. Textboxes are for entering strings. Those strings can look like numbers, but they are still strings. A NUD is for entering numbers. You can set a min, a max, and whether or not you have decimal places. It's much better for numbers than a textbox.

    One thing about working with strings is in this line:
    Code:
    lblDisplay.Text = txtBaseRent.Text + 27
    A string + a string is the two strings concatenated together. The + operator combines strings. If you are getting a number out of that, then the compiler is smart enough to convert the string to a number, add the 27, then convert the result back to a string. That would be fortunate, because I've always felt that what it SHOULD do is convert the 27 to a string and concatenate the two strings together. That would give you interesting results.

    Also, the Checked property of a radiobutton is already True or False. You don't need to compare it to True, because that would just be saying If True = True, or If False = True. Either way, the answer is on the left side of the = sign, so you don't need to bother with the "= True" part.
    My usual boring signature: Nothing

  8. #8
    Member
    Join Date
    Oct 2016
    Posts
    32

    Re: Need help with calculator

    Wow I just didn't think about it since I CnP the condition from OP lol. Also to note String concatenation in VB.Net unlike in other languages is & (which I'm sure everyone already knows) instead of + (although it works, it has rules for concatenating string with non strings like numbers).

    Second attempt:
    txtBaseRent.Text = if(cbo.Text = "PentHouse", 4000, 0) + if(FP.Checked, 440, 0) + if(NS.Checked, -50, 0)

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    9

    Re: Need help with calculator

    Thank you for all your guidance!!! i have decided to write each statement instead of a few short statements. the shorter my statements are, the messier the program gets )

    Can anyone help me write the statement where it adds $27 to the shoreline value? for example when i test the program for $1000 and select shoreline it displays $1140 but when i add $27, it adds to $1000 not $1140. my attempt is below. any input or correction is greatly appreciated.

    i am surprised that the teacher assigned this homework where he did not utter a single word of strings or conversion etc..

    Code:
            If cboBuilding.Text = "Shoreline" And chkFireplace.Checked = True Then
                lblDisplay.Text = txtBaseRent.Text + 140 + 27
            End If
    
            If cboBuilding.Text = "Shoreline" And chkNonSmoking.Checked = True Then
                lblDisplay.Text = txtBaseRent.Text + 140 - 11
            End If

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help with calculator

    Quote Originally Posted by hoeleeschitt View Post
    Thank you for all your guidance!!! i have decided to write each statement instead of a few short statements. the shorter my statements are, the messier the program gets )

    Can anyone help me write the statement where it adds $27 to the shoreline value? for example when i test the program for $1000 and select shoreline it displays $1140 but when i add $27, it adds to $1000 not $1140. my attempt is below. any input or correction is greatly appreciated.

    i am surprised that the teacher assigned this homework where he did not utter a single word of strings or conversion etc..

    Code:
            If cboBuilding.Text = "Shoreline" And chkFireplace.Checked = True Then
                lblDisplay.Text = txtBaseRent.Text + 140 + 27
            End If
    
            If cboBuilding.Text = "Shoreline" And chkNonSmoking.Checked = True Then
                lblDisplay.Text = txtBaseRent.Text + 140 - 11
            End If
    That's bad code. Why not do it properly? It's not messy at all. E.g.
    vb.net Code:
    1. Dim variable As SomeType
    2.  
    3. If firstCondition Then
    4.     'Modify variable accordingly.
    5. End If
    6.  
    7. If secondCondition Then
    8.     'Modify variable accordingly.
    9. End If
    10.  
    11. If thirdCondition Then
    12.     'Modify variable accordingly.
    13. End If
    14.  
    15. 'Display final value of variable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Need help with calculator

    Code:
    lblDisplay.Text = txtBaseRent.Text + 140
    That code above is a no-no. As jmc said, you should be doing all your math using numeric types, not Strings. You need to stop writing code like the above ASAP.

    This is the proper way to do something like this:-
    Code:
            Dim baseRent As Integer = CInt(txtBaseRent.Text)
    
            lblDisplay.Text = (baseRent + 140).ToString
    Or a more concise way:-
    Code:
    lblDisplay.Text = (CInt(txtBaseRent.Text) + 140).ToString
    The idea is to tell VB exactly what you want it to do and not have it guess because it could guess wrong. The "+" symbol means addition when it's between two numbers but it means concatenate when it's between two Strings. The guessing happens when it's between a String and a number. This is why we use CInt to convert the String to an Integer, so VB knows and does exactly what you want and adds the two values.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Tags for this Thread

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