Results 1 to 4 of 4

Thread: System.OutOfMemoryException was unhandled help

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    4

    System.OutOfMemoryException was unhandled help

    i am getting a System.OutOfMemoryException was unhandled on my listbox. i dont know how to fix it please help here is the code its saying that to.

    Form1.ListBox1.Items.Add(New listItem(TextBox1.Text, TextBox2.Text, MaskedTextBox1.Text, TextBox4.Text))

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: System.OutOfMemoryException was unhandled help

    what is a listitem? The only one I am familiar with is in ASP.NET, not winforms and it doesn't take 4 parameters.

    So is that your own custom class or something?

    Also, where are you calling this code from that you are qualifying with "Form1" ? If this code is in form1 then you should not have that part on there. If you are calling it from somewhere else, then you need to make sure you have the right instance of the form.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    4

    Re: System.OutOfMemoryException was unhandled help

    This is what I am trying to do: I have two forms one form has a list box, 4 labels, and two buttons on it. One of the buttons on the main form is an add new button which brings you to my second form, which has 4 textboxes: First name, last name, phone, and address and a done button. I want the first name to be displayed in the listbox on the main form when the done button is clicked and when the first name is selected in the listbox I want the more detailed information (names phone and address) to display in the labels. Here is what I got so far(where its underlined and bold is where its giving me System.OutOfMemoryException was unhandled :

    Code:
    Public Class Form1
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            If ListBox1.SelectedIndex <> -1 Then
                firstLabel.Text = CType(ListBox1.SelectedItem, listItem).firstName
                lastLabel.Text = CType(ListBox1.SelectedItem, listItem).lastName
                phoneLabel.Text = CType(ListBox1.SelectedItem, listItem).phone
                addressLabel.Text = CType(ListBox1.SelectedItem, listItem).address
            Else
                firstLabel.Text = ""
                lastLabel.Text = ""
                phoneLabel.Text = ""
                addressLabel.Text = ""
            End If
        End Sub
    
        Private Sub addnewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addnewButton.Click
            'Show Add New Form.
            Form2.Show()
        End Sub
        Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deleteButton.Click
            'Delete Selected Item in Listbox.
            For newitem As Integer = (ListBox1.SelectedItems.Count - 1) To 0 Step 1
                ListBox1.Items.Remove(ListBox1.SelectedItems(newitem))
            Next
        End Sub
        Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
            'Exits the Program
            Me.Close()
        End Sub
    
        Private Sub AddNewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Form2.Show()
        End Sub
    
        Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Me.Close()
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    End Class
    Public Class listItem
        Public firstName As String
        Public lastName As String
        Public phone As String
        Public address As String
        Public Sub New(ByVal firstName As String, ByVal lastName As String, ByVal phone As String, ByVal address As String)
            firstName = firstName
            lastName = lastName
            phone = phone
            address = address
        End Sub
        Public Overrides Function ToString() As String
            Return firstName
        End Function
    End Class
    
    
    
    
    Public Class Form2
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Handle Data Entry Error.
            If Me.TextBox1.Text = "" OrElse TextBox2.Text = "" OrElse MaskedTextBox1.Text = "" OrElse TextBox4.Text = "" Then
                MessageBox.Show("You should fill all the textboxes.", "Data Entry Error", _
                 MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                Form1.ListBox1.Items.Add(New listItem(TextBox1.Text, TextBox2.Text, MaskedTextBox1.Text, TextBox4.Text))            
    
    Me.Close()
            End If
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            'Exits the Program.
            Me.Close()
        End Sub
    End Class

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: System.OutOfMemoryException was unhandled help

    ok, change your class for ListItem to this:

    Code:
    Public Class listItem
    
        'PRIVATE VARIABLES
        Private _firstName As String = String.Empty
        Private _lastName As String = String.Empty
        Private _phone As String = String.Empty
        Private _address As String = String.Empty
    
        'PUBLIC PROPERTIES
        Public Property FirstName() As String
            Get
                Return _firstName
            End Get
            Set(ByVal value As String)
                _firstName = value
            End Set
        End Property
    
        Public Property LastName() As String
            Get
                Return _lastName
            End Get
            Set(ByVal value As String)
                _lastName = value
            End Set
        End Property
        Public Property Phone() As String
            Get
                Return _phone
            End Get
            Set(ByVal value As String)
                _phone = value
            End Set
        End Property
        Public Property Address() As String
            Get
                Return _address
            End Get
            Set(ByVal value As String)
                _address = value
            End Set
        End Property
    
        'CONSTRUCTOR
        Public Sub New(ByVal firstName As String, ByVal lastName As String, ByVal phone As String, ByVal address As String)
            _firstName = firstName
            _lastName = lastName
            _phone = phone
            _address = address
        End Sub
    
        'TOSTRING OVERRIDE
        Public Overrides Function ToString() As String
            Return _firstName
        End Function
    End Class
    it is bad practice to name your variables the same thing that you name the parameters of your constructor, because what was happening in YOUR original code is that you were just assiging variables to themselves.

    In your code:
    Code:
        Public firstName As String
        Public lastName As String
        Public phone As String
        Public address As String
        Public Sub New(ByVal firstName As String, ByVal lastName As String, ByVal phone As String, ByVal address As String)
            firstName = firstName <- this is assinging firstName to itself, not to the public variable you made above
            lastName = lastName
            phone = phone
            address = address
        End Sub
    so you were never actually assinging values to your variables (which should really be private not public, and exposed through properties, which my code above does)


    As for WHY you were getting the error, it seems that if you try to add an object to a listbox, and the objects ToString() method produces a null value (nothing) then it craps out with that error. It isn't really a great error for the issue, so likely it could be considered a small bug. The simple fix is to make sure your strings are at least initialized with string.empty, so they will be an empty string, which is not the same as a string that equals nothing. You were never assigning values to the variable you were using in your ToString override, so it was always nothing when you tried to add it to the listbox.

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