Results 1 to 15 of 15

Thread: [RESOLVED] Need more help understanding Classes

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    132

    Resolved [RESOLVED] Need more help understanding Classes

    Hi Guys

    As I said in my prior post I am trying to get to grips with Classes and how to use them "efficiently", So bearing this in mind I decided to create a small class driven application.

    I am struggling to understand how I can "use" this new instance of this user somewhere else.


    Test Data was
    Name : Joe
    username : JoeBloggs
    Password : JoesPassword

    Then

    Name : John
    Username : JohnDoe
    Password : JohnsPassword


    Now this updates the listbox with Joe and John in each row.

    The Form Code is. 3 Textboxes, 1 Button , 1 Listbox, 2Labels.

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If (txtName.Text = "") Then
                MsgBox("ERROR ENTER CLASS NAME")
            Else
                'Create a new user object. I tried to create a "unique" object name by passing the txtName.text value but it throws errors getName is not member of string..
                'Dim txtName.text As New user
     
                Dim newObject As New user
                newObject.setDetails(txtUsername.Text, txtPassword.Text, txtName.Text)
                lstUsers.Items.Add(newObject.getName())
            End If
        End Sub
    
        Private Sub lstUsers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstUsers.SelectedIndexChanged
            Dim users As user
            users.getLogins(lstUsers.SelectedItem)
        End Sub
    End Class
    Which is handled by this class.
    Code:
    Public Class user
        Dim name As String
        Dim username As String
        Dim password As String
        Public Sub setDetails(ByVal argusername As String, ByVal argpassword As String, ByVal argName As String)
            name = argName
            username = argusername
            password = argpassword
        End Sub
    
        Public Function getName()
            Return name
        End Function
    
        Public Function getLogins(ByVal userObject As Object)
            Form1.Label1.Text = username
            Form1.Label2.Text = password
            Return Nothing
    
        End Function
    
    End Class
    Now this works and updates the listbox with the new instance of the user but how can I use that instance from the listBox control to retrieve that users details??

    Many Thanks Barra.

    PS If you needed clearer clarification on what i am trying to do, Please ask
    Last edited by Barrabutus; Mar 10th, 2011 at 05:54 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