1

I'm just curious, really.

There was some code that wasn't handling empty strings before converting, essentially calling

Dim StringToConvert As String = ""
Dim a As Integer = CInt(StringToConvert)

Rather then something like

Dim StringToConvert As String = ""
Dim a As Integer = CInt("0" & StringToConvert)

and so it makes sense that it would throw an error...

What I don't understand is that this wasn't throwing an error on my machine when I was debugging. But it does throw an error when compiled!

Here is what calls the CInt function and only sometimes throws an error:

Public NotInheritable Class SomeForm
    Inherits Windows.Forms.Form

    Private Sub TextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles TextBox.LostFocus
        StaticHolderClass.StaticMethod(DirectCast(sender,TextBoxBase))
    End Sub

End Class

Public NotInheritable Class StaticHolderClass

    Public Shared Sub StaticMethod(ByVal sender As Windows.Forms.TextBoxBase)
        sender.Text = Format(CInt(sender.Text),"#,#")
    End Sub

End Class

Does anyone know why this would happen?

Joseph Nields
  • 5,527
  • 2
  • 32
  • 48
  • 2
    doing `Console.WriteLine(CInt(""))` throws an exception on my machine. You'll need to go through your debug settings to find out why yours isn't working. It could also be that somewhere the code is being called from within a `Try` block and the exception is getting eaten. – вʀaᴎᴅᴏƞ вєнᴎєƞ Mar 12 '15 at 14:20
  • It is not the case that it is in a try block because on another machine it caused a fatal error. – Joseph Nields Mar 12 '15 at 15:14

1 Answers1

2

In VB, occasionally when an error is encountered during form initialization, it doesn't get reported properly. Sometimes the error is not flagged at all, and sometimes it appears to be from a different location. For example, in the following code (on my system), an error is thrown with the button click and not the form load:

Public Class Form1
Dim k As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
k = CInt("")
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
k = CInt("")
End Sub

End Class

It seems to depend on the environment, so "your results may vary". You can work around this sometimes by using the shown event rather than load.

xpda
  • 15,585
  • 8
  • 51
  • 82
  • 1
    Its not just VB, but C# as well. Exceptions in the constructor, initialization or Form_Load can get eaten when running as x86. The C# compiler does catch a few more cases than VB though. See http://stackoverflow.com/q/4933958/1070452 – Ňɏssa Pøngjǣrdenlarp Mar 12 '15 at 14:29
  • @xpda this isn't in a Form_Load event though.... Any idea why it would be happening? I put where it is being called in my question. – Joseph Nields Mar 12 '15 at 15:28
  • Try separating ' = CInt("")' from the Dim statement, and then set a breakpoint on 'a = CInt("")'. Single-step from there (or check the call stack) to see what called the Lostfocus handler. It is possible it fired during some initialization. – xpda Mar 13 '15 at 00:31