Program not working the same when compiling in .net 4.0 rather than 4.5

DanielB33

Active member
Joined
Mar 29, 2013
Messages
40
Programming Experience
1-3
Thanks for previewing my post. If someone can tell me how to restructure the following code so that it will work with 4.0, it would be great/

I have made the start of an application with VB.Net in visual studio 2012 express. Works great with windows 7, but I need it to work with xp. To do this, I installed the updates recommended by microsoft (this alone did not work) and then tried to compile the code in vb.net 4.0. It worked...almost.

For the section of code listed below, vb.net 4.0 does not functin correctly. My goad is to send the user an error message if incorrect information is entered into a text box...If characters are entered, notify. If negative numbers are entered...notify. This works with 4.5, but not 4.0. Can anyone tell me why????
    Private Sub txtM1_Extend_TextChanged(sender As Object, e As EventArgs) Handles txtM1_Extend.TextChanged
        'Goal - Notify user if entering bad information before pressing go button
        If Not IsNumeric(Me.txtM1_Extend.Text) AndAlso Me.txtM1_Extend.Text IsNot "" Then                     'If the user entered something other than a number, send error message via error provider
            Me.ErrorProvider1.SetError(Me.txtM1_Extend, "Only enter integers!")
            GlobalVariables.m1_ExtendStatus = 0
        ElseIf 1 Then
            Try
                If (Convert.ToInt32(Me.txtM1_Extend.Text) < 0) Then                                           'If user entered negative number, inform via error provider
                    Me.ErrorProvider1.SetError(Me.txtM1_Extend, "Only enter positive values!")
                    GlobalVariables.m1_ExtendStatus = 0
                Else
                    GlobalVariables.m1_ExtendStatus = 1                                                       'Set flag so we know what what we can send to the micro when user presses "Go" button 
                End If
            Catch ex As Exception
                'Dont care, just do not interrupt program!
            End Try
        End If


        If (Me.txtM1_Extend.Text Is "") Then
            ErrorProvider1.Clear()                                                                            'Prevent error signal from staying on
            GlobalVariables.m1_ExtendStatus = 0                                                               'Ensure we know not to send micro blank info
        End If


    End Sub

*******Note: that the LAST if statement is the only code not unexciting. So either the ErrorProvider1.Clear() does not work in the 4.0 framework or I cannot use the condition in the if statement. I just don't know what to change and to what.

Thanks!
 
Last edited by a moderator:
I would change this:
VB.NET:
If (Me.txtM1_Extend.Text Is "") Then
to this:
VB.NET:
If (Me.txtM1_Extend.Text = "") Then
regardless. You may find that that will do the trick.
 
Do you understand what that did and why it worked? The 'Is' operator tests referential equality while the '=' operator tests value equality. By using 'Is' you were testing whether those two expressions were referring to the same String object in memory. If that was working in .NET 4.5 then that would suggest that .NET 4.5 optimises such that there is only one empty String object and that one object is used whenever an empty String is required. That means that two expressions the evaluate to an empty String will refer to the same String object and 'Is' will evaluate to True. If that optimisation is not applied though, you will have two empty String objects and therefore 'Is' will evaluate to False. The two Strings may have the same contents but they are not the same object. Because '=' tests value equality it compares the contents and finds them to be the same and therefore evaluates to True, whether the two objects are the same or not. If value equality is what you care about then you should be using '=', whether 'Is' works or not.
 
Back
Top