Alright, so I'm trying to code a program to where I can plug in two numbers and get a commission cost out of them.
I'm having a hard time setting up the code, and I'm really looking for some guidance. I have this as the code so far:
Public Class Form1
'Declare module-lvel variables and constants
Private SellingPriceInteger, CostValueInteger As Integer
Const COMMISSION_RATE_Decimal As Decimal = 0.2D
Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintButton.Click
'Prints the Form.
PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
PrintForm1.Print()
End Sub
Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
'Calculates the Comission
Dim SalesPriceInteger As Integer, CostValueInteger As Integer
Dim COMMISSION_RATE_Decimal As Decimal = 0.2D
Try
'Convert quantity to numeric variable.
SalesPriceInteger = Integer.Parse(SPTextBox.Text)
Catch ex As Exception
End Try
Try
'Convert price if quantity was successful.
Catch ex As Exception
End Try
End Sub
End Class
Basically, I'm in need of some guidance when it comes to Try/Catch, along with figuring out if what I'm typing would work, or if I'm missing a code that defines Integer.
4 answers
It is good practice to use try/catch blocks for user entered values. You need to elaborate on that, i.e. error messages, try again, etc.
There is one little point you can improve on: you have defined the commission rate in two separate places. In normal programming, this is not a good idea. If tomorrow the rate changes, you will need to change it in two different places. What if your assistant programmer forgot or did not know that it is defined in two places?
So try to define "global" constants in one place, and make it accessible to everyone.
I also have a question: what is the commission structure? Most of the time, the commission depends only on the selling price. Here you enter the cost AND the selling price. Is the cost used at all?
As for it being incomplete, I'm in the process of writing it, I've just run into a wall, really.
The commission structure is Commission = Commission rate * (Sales price - Cost Value)
I was told by the professor to use one of the hands-on exercises as a loose guideline, and I'm not one for thinking too freely when it comes to code, so I ended up copying quite a bit of it over, tweaking it for the names of my TextBoxes. I've been trying to find a guideline for this specific problem, but have had no luck.
This is an online class, and I'm really not learning much from just reading the book, so any advice would be helpful.
One of the few advantages of a learner-programmer is you have unlimited number of tries, and eventually you'll get somewhere. Evidently you cannot try blindly, but much of the learning is trying things out. You try, the VB compiler catches!
If you have your hands on the VB compiler, you will find that much of the mundane stuff is automated, like the routines for the text-boxes, etc. You only have to fill in the "meat", which is what you're after.
In any case, I suppose you will need two textboxes to enter the selling price and the cost, a button to calculate, and a textbox to display the results. All this can be done by drag-and-drop on the GUI (graphics user interface) of VB. You only have to tweak the error detecting procedures.
Your first priority is to make something that compiles, as simple as you wish, even if it does nothing.
Then you can test your own code, incrementally, so that if there is an error, you know exactly where it happens.
If it does not compile, and you have difficulty finding the problem, post.