Results 1 to 15 of 15

Thread: [RESOLVED] Need more help understanding Classes

Threaded View

  1. #2
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Need more help understanding Classes

    After reading it over a few times, I get what you are trying to accomplish.
    Correct me if I am wrong, but you want to have:
    - a button with textboxes to add new names with password and username
    - a listbox displaying the names
    - a way of determining what instance is selected from the listbox selection changed

    A problem with your current code is that you are not actually storing your classes.

    You only know the selected name, but no instance of the previously added item is there to evaluate with.

    To do this properly, make a single list variable in your Form1 class:
    Code:
    Private users As New List(Of user)
    Then, to "sync" it with your listbox, you add a new item to both your listbox AND your user list:
    Code:
                Dim newObject As New user()
                newObject.setDetails(txtUsername.Text, txtPassword.Text, txtName.Text)
                lstUsers.Items.Add(newObject.getName())
                users.Add(newObject)
    Now you can access your "already added info" from the selected index:
    Code:
    Private Sub lstUsers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstUsers.SelectedIndexChanged
          Dim selecteduser As user = users(lstUsers.SelectedIndex)
          'do some stuff with this class, like setting your textboxes or labels
    End Sub
    Important is to keep some sort of "back buffer" with your data in the background, and not to store your info in a control. I made the same mistake before, adding all my information to a listview control. But it makes it hard to use your stored data.

    EDIT

    In case you want some sort of global storage, you can add a shared list member to your user class:
    Code:
        Public Class user
            Private Shared users As New List(Of user)
            Public Shared Function GetUser(ByVal name As String) As user
                For Each u As user In users
                    If u.getName = name Then Return u
                Next
                Return Nothing
            End Function
    
            Public Shared Sub AddUser(ByVal u As user)
                users.Add(u)
            End Sub
    Last edited by bergerkiller; Mar 10th, 2011 at 06:08 PM.

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