7 Replies - 651 Views - Last Post: 08 May 2011 - 07:34 PM Rate Topic: -----

#1 mwharyjr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-March 11

I get errors when I run the code, the program starts when I debug.

Posted 08 May 2011 - 10:46 AM

I am not exactly sure what I am doing wrong. I need to convert
Me.xCommissionLabel.Text = Format(Me.xCommissionLabel.Text, "currency") to a toString, so it displays a dollar sign, 2 decimal places. so I did this.

Me.xCommissionLabel.Text = commissionLabel.ToString(CType("C", IFormatProvider))

When I run the program and input the data I get:

A first chance exception of type 'System.InvalidCastException' occurred in Commission Project.exe

What am I doing wrong?? Or what am I missing.

heres the rest of my code


Option Strict On
Option Explicit On

Public Class MainForm


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

Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click
' calculates and displays a commission

Dim commissionLabel As String
commissionLabel = CStr(CInt("0.1"))


Me.xCommissionLabel.Text = CStr(Val(Me.xSalesTextBox.Text) * 0.1)
Me.xCommissionLabel.Text = commissionLabel.ToString(CType("C", IFormatProvider))


End Sub

Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click

End Sub
End Class

This post has been edited by AdamSpeight2008: 08 May 2011 - 12:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: I get errors when I run the code, the program starts when I debug.

#2 kojima100  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 66
  • Joined: 10-November 10

Re: I get errors when I run the code, the program starts when I debug.

Posted 08 May 2011 - 03:30 PM

Use this instead of the weird format thing:

 xCommissionLabel.Text = "$" & math.round(xCommission.Text, 2)


Try not to use Option Strict and Option Explicit because they tend to cause a lot of problems I find when it comes to conversion.

This post has been edited by kojima100: 08 May 2011 - 03:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: I get errors when I run the code, the program starts when I debug.

Posted 08 May 2011 - 03:38 PM

By learning with them both turned on, will make you into a better programmer.

For me it's a standard of coding to aim for. My code compiles with Option Explicit and Option Strict turn on.
Was This Post Helpful? 0
  • +
  • -

#4 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 308
  • Joined: 30-January 11

Re: I get errors when I run the code, the program starts when I debug.

Posted 08 May 2011 - 03:50 PM

View Postkojima100, on 08 May 2011 - 03:30 PM, said:

Try not to use Option Strict and Option Explicit because they tend to cause a lot of problems I find when it comes to conversion.

Could you post a example of how they cause problems or do you have times where late binding is absolutley neccessary?
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: I get errors when I run the code, the program starts when I debug.

Posted 08 May 2011 - 03:53 PM

I'll give you a common one. Thinking that a text containing a number, can be explicitly treated as number without convesion.
Was This Post Helpful? 0
  • +
  • -

#6 mwharyjr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-March 11

Re: I get errors when I run the code, the program starts when I debug.

Posted 08 May 2011 - 05:30 PM

View Postmwharyjr, on 08 May 2011 - 10:46 AM, said:

I am not exactly sure what I am doing wrong. I need to convert
Me.xCommissionLabel.Text = Format(Me.xCommissionLabel.Text, "currency") to a toString, so it displays a dollar sign, 2 decimal places. so I did this.

Me.xCommissionLabel.Text = commissionLabel.ToString(CType("C", IFormatProvider))

When I run the program and input the data I get:

A first chance exception of type 'System.InvalidCastException' occurred in Commission Project.exe

What am I doing wrong?? Or what am I missing.

heres the rest of my code


Option Strict On
Option Explicit On

Public Class MainForm


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

Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click
' calculates and displays a commission

Dim commissionLabel As String
commissionLabel = CStr(CInt("0.1"))


Me.xCommissionLabel.Text = CStr(Val(Me.xSalesTextBox.Text) * 0.1)
Me.xCommissionLabel.Text = commissionLabel.ToString(CType("C", IFormatProvider))


End Sub

Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click

End Sub
End Class


I actually figured it out, I forgot to put in the

sales *.1 = commission
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: I get errors when I run the code, the program starts when I debug.

Posted 08 May 2011 - 06:07 PM

Huh? Where? How's that related to your errors?
You're code is overly complex and has lot redundant code, which strongly suggest you have no understand to what your code is doing.
Was This Post Helpful? 0
  • +
  • -

#8 mwharyjr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-March 11

Re: I get errors when I run the code, the program starts when I debug.

Posted 08 May 2011 - 07:34 PM

View PostAdamSpeight2008, on 08 May 2011 - 06:07 PM, said:

Huh? Where? How's that related to your errors?
You're code is overly complex and has lot redundant code, which strongly suggest you have no understand to what your code is doing.


Oh I understand what its doing, as long as it works. I just had to make a commission calculater to calculate commission on 10% of sales, it had to display a $ and a thousands decimal. everything works now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1