Results 1 to 5 of 5

Thread: Form load weirdness

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    43

    Form load weirdness

    I create a form with one button on it which shows another form with 3 textboxes. I send an instance of class Person to form2, which is supposed to fill the textboxes with the properties of Aperson. If I do not initialize the properties, the code in the loadboxes() routine of form2 does not get executed. I think this has something to do with the textbox.Enter routine, but I'm not sure why. If I remove the comment character in front of the lines initializine Aperson, then textbox3 contains "XXX" otherwise it is blank. I do not understand this behavior.

    Code:
    Public Class Person
        Public Property Name() As String
        Public Property Age() As Integer
    End Class
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Aform2 As New Form2
            Dim Aperson As Person = New Person
            'Aperson.Name = "Anonymous"
            'Aperson.Age = 29
            Aform2.Aperson = Aperson
            Dim result = Aform2.ShowDialog
    
        End Sub
    End Class
    
    Public Class Form2
        Public Aperson As Person
        Private SaveData As String
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            LoadBoxes()
                End Sub
        Private Sub LoadBoxes()
            TextBox1.Text = Aperson.Name.ToString
            TextBox2.Text = Aperson.Age.ToString
            TextBox3.Text = "XXX"
        End Sub
    
        Private Sub TextBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.Enter, TextBox2.Enter, TextBox1.Enter
            Dim Abox As TextBox = CType(sender, TextBox)
            SaveData = Abox.Text
        End Sub
    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Form load weirdness

    If you don't set the property values for the Person instance you create then they contain the default values for their types, which is Nothing for the String and 0 for the Integer. That means that this line:
    Code:
        Private Sub LoadBoxes()
            TextBox1.Text = Aperson.Name.ToString
            TextBox2.Text = Aperson.Age.ToString
            TextBox3.Text = "XXX"
        End Sub
    will throw a NullReferenceException, because you can't call ToString on an object that doesn't exist.

    The reason that you see the odd behaviour is that, due to a difference of opinion between the Windows and .NET teams at Microsoft, exceptions thrown in the Load event handler on 64-bit systems are simply swallowed. If you put an exception handler in there, i.e. a Try...Catch block, then you'll see the exception.

    The thing is, there's no point in calling ToString there anyway, so there's no reason for an exception to be thrown. The Age property is type Integer so it's completely appropriate for you to call ToString on it in order to display it, but it will always have a value anyway, so there's no issue. The Name property is already type String, so what benefit is there to calling ToString on it? If you simply assign the Name property to the Text property of the TextBox then it will be fine, whether there's a value or not. Assigning Nothing to the Text of a TextBox is equivalent to assigning an empty String.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    43

    Re: Form load weirdness

    If you don't set the property values for the Person instance you create then they contain the default values for their types, which is Nothing for the String and 0 for the Integer.
    So...nothing is nothing for strings but zero for integers? How does that make sense? Why wouldn't the default for strings be ""?

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Form load weirdness

    A string is a reference type (it's a class, though it doesn't always behave like a class), while an integer is a value type. Every variable must have some value. In the case of a reference type, the variable doesn't hold the actual object, it just holds a reference to the object (effectively, the memory address of the actual object). In the case of a value type, the variable does hold the actual object. So, a value type that hasn't been set to anything just holds the default value, while a reference type that hasn't been set holds a default address, which is the address of nothing, and is therefore Nothing. For the string to have a default value of "", then the string object would have to have been instantiated, even when it quite clearly has not been instantiated. In the case of a string, that's really not so bad, especially since there is a String.Empty, which all strings could start out initialized to. After all, while a String is a class, it really doesn't act like one in many ways, so some 'special' work is already done for them.

    However, no such default would make sense for any other class, so giving a string a default object when no other class gets a default object, would be a serious deviation for that one type. Furthermore, giving ALL classes default types is flat out impossible, so the only alternative that keeps them all the same would be to give NO classes default types, and that is what was done. Therefore, this was about consistency.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    43

    Re: Form load weirdness

    OK, makes sense after all. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width