|
-
Nov 17th, 2017, 07:16 PM
#1
Thread Starter
New Member
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.
-
Nov 17th, 2017, 07:52 PM
#2
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.
-
Nov 18th, 2017, 12:31 AM
#3
Thread Starter
New Member
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..
-
Nov 18th, 2017, 02:16 AM
#4
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.
-
Nov 18th, 2017, 04:39 AM
#5
Member
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)
-
Nov 18th, 2017, 06:13 AM
#6
Re: Need help with calculator
 Originally Posted by TizzyT
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.
-
Nov 18th, 2017, 09:42 AM
#7
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
 
-
Nov 18th, 2017, 03:27 PM
#8
Member
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)
-
Nov 18th, 2017, 11:20 PM
#9
Thread Starter
New Member
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
-
Nov 19th, 2017, 06:14 PM
#10
Re: Need help with calculator
 Originally Posted by hoeleeschitt
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:
Dim variable As SomeType If firstCondition Then 'Modify variable accordingly. End If If secondCondition Then 'Modify variable accordingly. End If If thirdCondition Then 'Modify variable accordingly. End If 'Display final value of variable.
-
Nov 22nd, 2017, 02:14 AM
#11
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|