whats the quickest way to get this program done

eclass

Active member
Joined
Jul 6, 2011
Messages
27
Programming Experience
Beginner
ok heres my problem that Im working on I know its supposed to be parallel arrays. Im creating a directory that shows employees names in a list box on the main form. When the user selects a name from the list box, the only thing is the app has to display the employee ID number, dept name, and telepjhone number on another form. The app should store all employee ID numbers in seperate arrays.


VB.NET:
ok cool so how does this look so far [COLOR=black][FONT=inherit]
[COLOR=blue][FONT=inherit]Public[/FONT][/COLOR] [COLOR=blue][FONT=inherit]Class[/FONT][/COLOR] frmEmployeeDirectory [COLOR=blue][FONT=inherit]Dim[/FONT][/COLOR] mstrEmployeeNames() [COLOR=blue][FONT=inherit]As[/FONT][/COLOR] [COLOR=blue][FONT=inherit]String[/FONT][/COLOR] = {[COLOR=#A31515][FONT=inherit]"Anthony"[/FONT][/COLOR], [COLOR=#A31515][FONT=inherit]"Billy"[/FONT][/COLOR], [COLOR=#A31515][FONT=inherit]"Ashley"[/FONT][/COLOR], [COLOR=#A31515][FONT=inherit]"David"[/FONT][/COLOR], [COLOR=#A31515][FONT=inherit]"Brittany"[/FONT][/COLOR], [COLOR=#A31515][FONT=inherit]"Vanessa"[/FONT][/COLOR], [COLOR=#A31515][FONT=inherit]"Craig"[/FONT][/COLOR], [COLOR=#A31515][FONT=inherit]"Bria"[/FONT][/COLOR]} [COLOR=blue][FONT=inherit]Dim[/FONT][/COLOR] mintEmployeeID() [COLOR=blue][FONT=inherit]As[/FONT][/COLOR] [COLOR=blue][FONT=inherit]Integer[/FONT][/COLOR] = {[COLOR=blue][FONT=inherit]CInt[/FONT][/COLOR]([COLOR=#A31515][FONT=inherit]"2562"[/FONT][/COLOR]), [COLOR=blue][FONT=inherit]CInt[/FONT][/COLOR]([COLOR=#A31515][FONT=inherit]"2172"[/FONT][/COLOR]), [COLOR=blue][FONT=inherit]CInt[/FONT][/COLOR]([COLOR=#A31515][FONT=inherit]"2679"[/FONT][/COLOR]), [COLOR=blue][FONT=inherit]CInt[/FONT][/COLOR]([COLOR=#A31515][FONT=inherit]"2204"[/FONT][/COLOR]), [COLOR=blue][FONT=inherit]CInt[/FONT][/COLOR]([COLOR=#A31515][FONT=inherit]"2901"[/FONT][/COLOR]), [COLOR=blue][FONT=inherit]CInt[/FONT][/COLOR]([COLOR=#A31515][FONT=inherit]"2331"[/FONT][/COLOR]), [COLOR=blue][FONT=inherit]CInt[/FONT][/COLOR]([COLOR=#A31515][FONT=inherit]"2463"[/FONT][/COLOR]), [COLOR=blue][FONT=inherit]CInt[/FONT][/COLOR]([COLOR=#A31515][FONT=inherit]"27072"[/FONT][/COLOR])} [COLOR=blue][FONT=inherit]Dim[/FONT][/COLOR] mstrDeparmentName() [COLOR=blue][FONT=inherit]As[/FONT][/COLOR] [COLOR=blue][FONT=inherit]String[/FONT][/COLOR] = {[COLOR=#A31515][FONT=inherit]"Legal"[/FONT][/COLOR], [COLOR=#A31515][FONT=inherit]"Engineering"[/FONT][/COLOR], [COLOR=#A31515][FONT=inherit]"Accounting"[/FONT][/COLOR], [COLOR=#A31515][FONT=inherit]"R&D"[/FONT][/COLOR], [COLOR=#A31515][FONT=inherit]" Manager"[/FONT][/COLOR], [COLOR=#A31515][FONT=inherit]"Help Desk"[/FONT][/COLOR], [COLOR=#A31515][FONT=inherit]"Programmer"[/FONT][/COLOR], [COLOR=#A31515][FONT=inherit]"Legal"[/FONT][/COLOR]} [COLOR=blue][FONT=inherit]Dim[/FONT][/COLOR] mintPhoneNumber() [COLOR=blue][FONT=inherit]As[/FONT][/COLOR] [COLOR=blue][FONT=inherit]Integer[/FONT][/COLOR] = {[COLOR=blue][FONT=inherit]CInt[/FONT][/COLOR]([COLOR=#A31515][FONT=inherit]"773-651-9087"[/FONT][/COLOR]), [COLOR=blue][FONT=inherit]CInt[/FONT][/COLOR]([COLOR=#A31515][FONT=inherit]"773-342-3997"[/FONT][/COLOR]), [COLOR=blue][FONT=inherit]CInt[/FONT][/COLOR]([COLOR=#A31515][FONT=inherit]"312-892-5420"[/FONT][/COLOR]), [COLOR=blue][FONT=inherit]CInt[/FONT][/COLOR]([COLOR=#A31515][FONT=inherit]"708-430-3877"[/FONT][/COLOR]), [COLOR=blue][FONT=inherit]CInt[/FONT][/COLOR]([COLOR=#A31515][FONT=inherit]"773-543-7943"[/FONT][/COLOR]), [COLOR=blue][FONT=inherit]CInt[/FONT][/COLOR]([COLOR=#A31515][FONT=inherit]"312-692-1288"[/FONT][/COLOR]), [COLOR=blue][FONT=inherit]CInt[/FONT][/COLOR]([COLOR=#A31515][FONT=inherit]"2463"[/FONT][/COLOR]), [COLOR=blue][FONT=inherit]CInt[/FONT][/COLOR]([COLOR=#A31515][FONT=inherit]"708-389-9090"[/FONT][/COLOR])} [COLOR=blue][FONT=inherit]Private[/FONT][/COLOR] Names [COLOR=blue][FONT=inherit]As[/FONT][/COLOR] [COLOR=blue][FONT=inherit]Integer[/FONT][/COLOR] [COLOR=blue][FONT=inherit]Private[/FONT][/COLOR] [COLOR=blue][FONT=inherit]Sub[/FONT][/COLOR] frmEmployeeDirectory_Load([COLOR=blue][FONT=inherit]ByVal[/FONT][/COLOR] sender [COLOR=blue][FONT=inherit]As[/FONT][/COLOR] System.Object, [COLOR=blue][FONT=inherit]ByVal[/FONT][/COLOR] e [COLOR=blue][FONT=inherit]As[/FONT][/COLOR] System.EventArgs) [COLOR=blue][FONT=inherit]Handles[/FONT][/COLOR] [COLOR=blue][FONT=inherit]MyBase[/FONT][/COLOR].Load [COLOR=blue][FONT=inherit]End[/FONT][/COLOR] [COLOR=blue][FONT=inherit]Sub[/FONT][/COLOR] [COLOR=blue][FONT=inherit]Private[/FONT][/COLOR] [COLOR=blue][FONT=inherit]Sub[/FONT][/COLOR] lstEmployeeNames_SelectedIndexChanged([COLOR=blue][FONT=inherit]ByVal[/FONT][/COLOR] sender [COLOR=blue][FONT=inherit]As[/FONT][/COLOR] [COLOR=blue][FONT=inherit]Object[/FONT][/COLOR], [COLOR=blue][FONT=inherit]ByVal[/FONT][/COLOR] e [COLOR=blue][FONT=inherit]As[/FONT][/COLOR] System.EventArgs) [COLOR=blue][FONT=inherit]Handles[/FONT][/COLOR] lstEmployeeNames.SelectedIndexChanged Names = [COLOR=blue][FONT=inherit]CType[/FONT][/COLOR](lstEmployeeNames.SelectedItem, Employees).Names [COLOR=blue][FONT=inherit]Me[/FONT][/COLOR].Text = Names.ToString [COLOR=blue][FONT=inherit]End[/FONT][/COLOR] [COLOR=blue][FONT=inherit]Sub[/FONT][/COLOR][COLOR=blue][FONT=inherit]End[/FONT][/COLOR] [COLOR=blue][FONT=inherit]Class[/FONT][/COLOR][COLOR=green][FONT=inherit]'Private Sub btnChooseName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChooseName.Click[/FONT][/COLOR][COLOR=green][FONT=inherit]'user chooses name to get their employee ID, Dept Name and Phone number[/FONT][/COLOR][COLOR=green][FONT=inherit]'End Sub[/FONT][/COLOR][COLOR=green][FONT=inherit]'End Class[/FONT][/COLOR][/FONT][/COLOR]
 
Last edited:
oh ok so I should stick with the array method i had and use your select index method would I write one for each name I have?
 
oh ok so I should stick with the array method i had and use your select index method would I write one for each name I have?
Well it's up to you, i would recommend the structure/class over the separate arrays in this case. But if you stick with the arrays, you just need to pass the element from the arrays using the selected index, like i showed in post #13
        Dim id As Integer = mintEmployeeID(selectedIndex)
        Dim dept As String = mstrDeparmentName(selectedIndex)
        'repeat for the number and etc.

        'you do not have to declare variables like above, you can just pass it using the array and index like
        'frm2.id = mintEmployeeID(selectedIndex)
        'frm2.dept = mstrDeparmentName(selectedIndex)
        'however using the variables can make it easier to read in some cases
 
ok thank you Im only sticking with the array because the following chapter is on File I/O and structures and classes. so I kinda want to learn the concepts slowly but do them as fast as possible if that makes sense....
 
yea np, sounds cool. Have fun and good luck with it. :)
 
lol thanks for all your help, I just have to get more accustom to programming in general once that happens I believe ill be on my way.

heres what I have so far how is this...
VB.NET:
Option Strict On
Option Explicit On


Public Class frmEmployeeDirectory
    Private Employees(7) As Employee
    Private Sub EmployeeDirectory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Employees(0) = New Employee("Anthony", 2562, "Legal", "773-651-9087")
        Employees(1) = New Employee("Billy", 2172, "Engineering", "773-342-3997")
        Employees(2) = New Employee("Ashley", 2679, "Accounting", "312-892-5420")
        Employees(3) = New Employee("David", 2204, "R&D", "708-430-3877")
        Employees(4) = New Employee("Brittany", 2901, "Manager", "773-543-7943")
        Employees(5) = New Employee("Vanessa", 2331, "Help Desk", "312-692-1288")
        Employees(6) = New Employee("Craig", 2463, "Programmer", "708-389-9090")
        Employees(7) = New Employee("Bria", 27072, "Legal", "708-430-3877")
        Array.Sort(Employees)
        lstEmployeeNames.DataSource = Employees
    End Sub
    Private Sub lstEmployeeNames_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstEmployeeNames.SelectedIndexChanged
        Dim Emp As Employee = CType(lstEmployeeNames.SelectedItem, Employee)
        lstEmployeeNames.Text = Emp.Details
    End Sub
End Class


Public Class Employee
    Implements IComparable
    Public Name As String
    Public ID As Integer
    Public Department As String
    Public PhoneNumber As String
    Public Sub New(ByVal Name As String, ByVal ID As Integer, ByVal Dept As String, ByVal Phone As String)
        Me.Name = Name
        Me.ID = ID
        Department = Dept
        PhoneNumber = Phone
    End Sub
    Public Overrides Function ToString() As String
        Return Name
    End Function
    Public Function Details() As String
        Return Name & ", " & ID.ToString & ", " & Department & ", " & PhoneNumber.ToCharArray
    End Function
    Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
        Return String.Compare(Name, CType(obj, Employee).Name)
    End Function
End Class
 
i think it looks fine... the one thing i noticed is that in your details method of the Employee class, i don't think calling ToCharArray for the PhoneNumber is necessary since PhoneNumber is a string already and your Details method is Returning a String.. ?
 
ohhh ok so should I remove it and how can I get it to display on another form because the book says the info is suppose to display on a seperate form
 
ohhh ok so should I remove it and how can I get it to display on another form because the book says the info is suppose to display on a seperate form
Yeah you can remove it.

To display the info on another form just create a new form and pass the data to it.

For example, Create a new form and add a Label to it, let's say this form is called Form2 and the label is called Label1. Then in your current form create and show the form.

    Private frm2 As New Form2 'declare in class scope

    'say you want this second form to show when the form is loaded, add to your load method otherwise put this in a button event or something..
    Private Sub EmployeeDirectory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        frm2 = New Form2
        frm2.Show()

        'the rest of your code
        Employees(0) = New Employee("Anthony", 2562, "Legal", "773-651-9087")
        '...
    End Sub
    Private Sub lstEmployeeNames_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstEmployeeNames.SelectedIndexChanged
        Dim Emp As Employee = CType(lstEmployeeNames.SelectedItem, Employee)

        frm2.Label1.Text = Emp.Details
    End Sub


you can do this differently and use a variable/property that you set on the second form, or set the info on the second form using the constructor. If you do it like i showed above you will be changing text on the second form whenever the selected index changes in the listbox, which may/may not be what you want to do.

and say you wanted to make it show up as a dialog only when a selected index has changed, then you can do something like this
    Private Sub lstEmployeeNames_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstEmployeeNames.SelectedIndexChanged
        Dim Emp As Employee = CType(lstEmployeeNames.SelectedItem, Employee)

        Dim frm2 As New Form2
        frm2.Label1.Text = Emp.Details

        frm2.ShowDialog()
    End Sub


Note that when you set the DataSource of a Listbox the SelectedIndexChanged event is fired.
 
oh wow so thats i huh.....so I only need one label?

Well that was just an example, it's up to you. If you are only passing the string from the Details method, i think one label is enough?
 
well i want to display all the employee info that I store in the array when their names are click on....well not when there names are click on because I have a button call display info with the Dim showEmployeeInformation As New frmEmployeeInformation code inside so I guess all i have to do is display the employee info on this page how would I get that done
 
Last edited:
well i want to display all the employee info that I store in the array when their names are click on
Not sure if i understand correctly, so you want to display all of the employee info for that specific employee right? The details method returns a string with all of that info for that specific employee right? So i guess you can just use that?

But if you want you can always just pass the Employee object to the second form. Just create a property/variable that can take the employee object. You can pass whatever you need to that second form using the same concept. As long as the variable/property/object is public/friend you should be able to access it like FormName.VariableName like shown with the label example above. If you wanted to pass all of the employees in your array, just pass the array to the new form.
 
sorry for the confusion I just want to display the employee info thats it I have a button call display info on the first form with the employee names so how would I just get there info to show
 
sorry for the confusion I just want to display the employee info thats it I have a button call display info on the first form with the employee names so how would I just get there info to show
np, so following up with the code you already have, your listbox already contains Employee objects. So when the button is clicked get the SelectedItem and check to see if it is of type Employee, then convert it to an Employee type and you can either send that to your new form or you can just send the string from the Details method.

It's just like the lstEmployeeNames_SelectedIndexChanged method i posted in #23, do the same thing in a button click event. If you want you can create a property/variable on the second form that is of type Employee and just pass that to the second form instead of passing the string from the Details Method.
 
Also just a quick note, you may want to check that the selectedItem in the listbox is of type Employee before you convert it. Although in this situation you won't really encounter a problem because you know all of the objects will be Employee, but if your not sure what is put into the listbox it is always nice to check before converting, otherwise catch it with a try block or something.

ex:

        If TypeOf lstEmployeeNames.SelectedItem Is Employee Then
            Dim Emp As Employee = CType(lstEmployeeNames.SelectedItem, Employee)

            'do something with Emp...
        End If
 
Back
Top