4 Replies - 682 Views - Last Post: 24 September 2013 - 07:38 AM Rate Topic: -----

#1 sirnick41  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-September 13

Visual Basic Program Doesn't 100% Work

Posted 24 September 2013 - 07:09 AM

So I'm making this program that a user can choose an academic rank, which multiplies a given salary by an amount (depending on which radio button they click). There are also check boxes that a user can click that will raise the original salary depending on which one(s) are clicked. My program works if no check boxes are checked, if the Doctorate check box is checked alone, and if the Doctorate and Masters buttons are checked at the same time. For some reason, when the Masters button is checked by itself, if you uncheck it and switch ranks then check it again it works but if you keep it checked and change ranks the program gets messed up. I've looked it over a dozen times and can't figure out what's wrong, any help would be greatly appreciated.

Here's my code:

Option Explicit On
Option Strict On

Public Class frmAcademic

    Dim salary As Integer
    Dim bonusM As Integer
    Dim bonusMc As Integer
    Dim bonusD As Integer
    Dim bonusDc As Integer
    Dim newsalary As Integer

    Private Sub frmAcademic_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ControlBox = False
        salary = 50000
        bonusM = 3000
        bonusD = 6000
        bonusMc = bonusM
        bonusDc = bonusD
        chkBach.Checked = True
        newsalary = CInt(salary.ToString())
        lblSalaryAmt.Text = Format(newsalary, "$##,##0.00")
    End Sub


    Private Sub rdLecturer_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optLecturer.CheckedChanged
        bonusM = bonusMc * 1
        bonusD = bonusDc * 1
        newsalary = CInt(salary.ToString())
        If chkMast.Checked = True Then
            newsalary = newsalary + bonusM
        ElseIf chkMast.Checked = False Then
            newsalary = CInt(salary.ToString())
        End If
        If chkDoct.Checked = True Then
            newsalary = newsalary + bonusD
        ElseIf chkDoct.Checked = False Then
            newsalary = CInt(salary.ToString())
        End If
        lblSalaryAmt.Text = Format(newsalary, "$##,##0.00")
    End Sub

    Private Sub rdInstructor_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optInstructor.CheckedChanged
        bonusM = CInt(bonusMc * 1.2)
        bonusD = CInt(bonusDc * 1.2)
        newsalary = CInt(CDbl(salary.ToString()) * 1.2) + 11
        If chkMast.Checked = True Then
            newsalary = newsalary + bonusM
        ElseIf chkMast.Checked = False Then
            newsalary = CInt(CDbl(salary.ToString()) * 1.2)
        End If
        If chkDoct.Checked = True Then
            newsalary = newsalary + bonusD
        ElseIf chkDoct.Checked = False Then
            newsalary = CInt(CDbl(salary.ToString()) * 1.2)
        End If
        lblSalaryAmt.Text = Format(newsalary, "$##,##0.00")
    End Sub

    Private Sub rdAsstProf_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optAsstProf.CheckedChanged
        bonusM = CInt(bonusMc * 1.4)
        bonusD = CInt(bonusDc * 1.4)
        newsalary = CInt(CDbl(salary.ToString()) * 1.4)
        If chkMast.Checked = True Then
            newsalary = newsalary + bonusM
        ElseIf chkMast.Checked = False Then
            newsalary = CInt(CDbl(salary.ToString()) * 1.4)
        End If
        If chkDoct.Checked = True Then
            newsalary = newsalary + bonusD
        ElseIf chkDoct.Checked = False Then
            newsalary = CInt(CDbl(salary.ToString()) * 1.4)
        End If
        lblSalaryAmt.Text = Format(newsalary, "$##,##0.00")
    End Sub

    Private Sub rdAssoProf_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optAssoProf.CheckedChanged
        bonusM = CInt(bonusMc * 1.6)
        bonusD = CInt(bonusDc * 1.6)
        newsalary = CInt(CDbl(salary.ToString()) * 1.6)
        If chkMast.Checked = True Then
            newsalary = newsalary + bonusM
        ElseIf chkMast.Checked = False Then
            newsalary = CInt(CDbl(salary.ToString()) * 1.6)
        End If
        If chkDoct.Checked = True Then
            newsalary = newsalary + bonusD
        ElseIf chkDoct.Checked = False Then
            newsalary = CInt(CDbl(salary.ToString()) * 1.6)
        End If
        lblSalaryAmt.Text = Format(newsalary, "$##,##0.00")
    End Sub

    Private Sub rdProf_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optProf.CheckedChanged

        bonusM = bonusMc * 2
        bonusD = bonusDc * 2
        newsalary = CInt(CDbl(salary.ToString()) * 2)
        If chkMast.Checked = True Then
            newsalary = newsalary + bonusM
        ElseIf chkMast.Checked = False Then
            newsalary = CInt(CDbl(salary.ToString()) * 2)
        End If
        If chkDoct.Checked = True Then
            newsalary = newsalary + bonusD
        ElseIf chkDoct.Checked = False Then
            newsalary = CInt(CDbl(salary.ToString()) * 2)
        End If
        lblSalaryAmt.Text = Format(newsalary, "$##,##0.00")

    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click
        Me.Close()
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileAbout.Click
        MsgBox("This program was written by Nicholas Konst")
    End Sub

    Private Sub chM_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkMast.CheckedChanged
        If chkMast.Checked = True Then
            newsalary = newsalary + bonusM
            lblSalaryAmt.Text = newsalary.ToString()
            lblSalaryAmt.Text = Format(newsalary, "$##,##0.00")

        ElseIf chkMast.Checked = False Then
            newsalary = newsalary - bonusM
            lblSalaryAmt.Text = newsalary.ToString()
            lblSalaryAmt.Text = Format(newsalary, "$##,##0.00")
        End If

    End Sub

    Private Sub chD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDoct.CheckedChanged
        If chkDoct.Checked = True Then
            newsalary = newsalary + bonusD
            lblSalaryAmt.Text = newsalary.ToString()
            lblSalaryAmt.Text = Format(newsalary, "$##,##0.00")

        ElseIf chkDoct.Checked = False Then
            newsalary = newsalary - bonusD
            lblSalaryAmt.Text = newsalary.ToString()
            lblSalaryAmt.Text = Format(newsalary, "$##,##0.00")
        End If

    End Sub

    Private Sub chkBach_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBach.CheckedChanged
        chkBach.Checked = True
    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Visual Basic Program Doesn't 100% Work

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Visual Basic Program Doesn't 100% Work

Posted 24 September 2013 - 07:16 AM

It'll also help if which version of Visual Basic you're using when asking question about it.
Did you know that this isn't VB6 code but VB.Net?
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9205
  • View blog
  • Posts: 34,582
  • Joined: 12-June 08

Re: Visual Basic Program Doesn't 100% Work

Posted 24 September 2013 - 07:18 AM

... I don't remember a 'handles' for the event clause in VB6.
Was This Post Helpful? 0
  • +
  • -

#4 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 204
  • View blog
  • Posts: 1,261
  • Joined: 11-May 12

Re: Visual Basic Program Doesn't 100% Work

Posted 24 September 2013 - 07:23 AM

Saying "the program gets messed up" isn't really telling us much. How does it mess up? Does it error out and crash? Does it just not do what you think should be done? If you aren't sure how to explain something in a different way, don't be afraid to use Screen Shots to show us what is happening :) What do you use to "change ranks"?
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Visual Basic Program Doesn't 100% Work

Posted 24 September 2013 - 07:38 AM

Why are check for false? Just think about it for moment.

  If chkMast.Checked = True Then
     newsalary = newsalary + bonusM
  ElseIf chkMast.Checked = False Then
      newsalary = CInt(salary.ToString())
  End If



Look at the basic construction of an If ... Then ... Else ... End If
If {boolean condition } Then
  ' condition is true
Else
  ' condition is false
End If



So using the documentation and Intellisense you see that chkMast.Checked returns a boolean already, so there's no need to compare it with True.
If chkMast.Checked Then
  newsalay = newsalary + bonusM
Else
  ' It not checked.
  newsalary = salary ' Salary is already an Integer.
End If


See also there no need to do the comparison for the false part, as that the Else clause does.

Strings containing numbers are not numbers but strings, since not every possible variation of the contents are a number. So don't do casting to a string and back again.
Use .TryParse to valid input string before using the converted value in further expressions.
Dim output As Integer
If Integer.Try(input, output) Then
  ' input was a valid integer
Else
  ' input wasn't valid
End If



A little bit of further jiggery-pokery reduces it down to 1 line.
 newsalary = salary + If(chkMast.Checked, bonusM, 0)



Also think about some of the basic maths. What is the result of multiplying by the constant 1? Is the multiplication needed?

There is also a lot a repetition of similar code, maybe think implementing a method which parameters can be passed to.

This post has been edited by AdamSpeight2008: 24 September 2013 - 07:41 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1