Thursday, July 30, 2009

Visual Basics Question. is this right?

My directions are to:


Continue with the commision project you coded for the ch3 homework.





use the tryparse to check if the data the user entered can be convert. If data cannot be convert send message to user.


code the exit button so that it give the user a messagebox asking them whether or not they want to exit. If the user answers yes the program should exit; if they answer no the program should not exit.





and this is my code which i'm pretty sure I did right?





Public Class MainForm





Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load





PaperSplashScreenForm.ShowDialog()





End Sub





Private Sub CalculateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalculateButton.Click





'Declare


Dim SalesNum As Integer 'SalesNum.textbox string convert to integer


Dim CommissionRateNum As Integer 'CommissionRateNum.textbox string convert to Integer


Dim TotalResult As Integer 'from calculation


Dim isconverted1 As Boolean


Dim isconverted2 As Boolean





'Input conversion


isconverted1 = Integer.TryParse(SalesTextBox.Text, SalesNum)


isconverted2 = Integer.TryParse(CommissionRateTextBox.T... CommissionRateNum)





'If/Else statement


If isconverted1 = True AndAlso isconverted2 = True Then





'Calc()


TotalResult = SalesNum * (CommissionRateNum * 0.01)





'Display


CommissionTotalLabel.Text = TotalResult.ToString("C")


Else


MessageBox.Show("Please enter a whole number only", "Error Message", _


MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1)


End If





End Sub





Private Sub ClearButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ClearButton.Click





NameTextBox.Clear()


SalesTextBox.Clear()


CommissionRateTextBox.Clear()


CommissionTotalLabel.Text = String.Empty





End Sub





Private Sub ExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExitButton.Click





Dim button As DialogResult





button = MessageBox.Show("Do you want to exit?", "Exit Message", _


MessageBoxButtons.YesNo, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button2)


If button = Windows.Forms.DialogResult.Yes Then


Me.Close()





End If





End Sub





End Class

Visual Basics Question. is this right?
It appears right enough. I haven't checked every line, but it appears to be fine.





I will make the comment that you have a LOT of declarations that are either completely unnecessary, or more verbose than needed.





For example:





button = MessageBox.Show("Do you want to exit?", "Exit Message", _


MessageBoxButtons.YesNo, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)


If button = Windows.Forms.DialogResult.Yes Then


Me.Close()


End If





This can be trimmed down to:





If MessageBox.Show("Do you want to exit?", "Exit Message", _


MessageBoxButtons.YesNo, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then


Me.Close()


End If





Notice that button never needs to be Dimensioned, as you can simply call the MessageBox.Show method with the DialogResult.Yes check right in the If statement.





The same thing can be said of your TryParse calls.





This:





Dim isconverted1 As Boolean


Dim isconverted2 As Boolean


'Input conversion


isconverted1 = Integer.TryParse(SalesTextBox.... SalesNum)


isconverted2 = Integer.TryParse(CommissionRat... CommissionRateNum)


'If/Else statement


If isconverted1 = True AndAlso isconverted2 = True Then





Becomes:





If Integer.TryParse(SalesTextBox.... SalesNum) AndAlso Integer.TryParse(CommissionRat... CommissionRateNum) Then





Notice that your 5 lines of code has become 1 line of code, yet still functions as intended.





Also notice I did not do an "= True" check. You never need to check "= True" in .NET. Because TryParse returns a Boolean, it's a complete expression all by itself. Otherwise, you are essentially asking "If True = True", which, although it will work perfectly, is extra work and unncessary, and makes your code hard to read, which always means higher maintenance.


No comments:

Post a Comment