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:
Dont' use Cint to convert a string to an integer if it's already an integer.

Why are you using Cint() to convert a numeric string back into an integer when you can simply do this:

Dim mintEmployeeID() As Integer = {2562, 2172, 2679, 2204, 2901} 'etc

However, a phone number is NOT an integer! It's a String and should stay that way. Otherwise, it will subtract one part of the number from another before converting it to an integer.

There are probably a lot more issues with your code but it's very hard to read as you have it all bunched together on one line. Please redo your code and post it properly.
 
sorry about that guys and also I try to remove the cint and got a error message.
I will do the phone number as a string now though
 
the error message I got was Option Strict On prohibits implicit conversions from string to integer

That is probably because you tried to convert a string into an integer Implicitly. If you are setting a variable with a number value do not surround the numbers with quotes.

Example:
        Dim someName As String = "MyName"
        Dim someNumber As Integer = 456
 
updated


VB.NET:
Option Strict On
Option Explicit On


Public Class frmEmployeeDirectory
    Dim mstrEmployeeNames() As String = {"Anthony", "Billy", "Ashley", "David", "Brittany", "Vanessa", "Craig", "Bria"}
    Dim mintEmployeeID() As Integer = {2562, 2172, 2679, 2204, 2901, 2331, 2463, 27072}
    Dim mstrDeparmentName() As String = {"Legal", "Engineering", "Accounting", "R&D", " Manager", "Help Desk", "Programmer", "Legal"}
    Dim mstrPhoneNumber() As String = {"773-651-9087", "773-342-3997", "312-892-5420", "708-430-3877", "773-543-7943", "312-692-1288", "2463", "708-389-9090"}


    Private Names As Integer
    Private Sub frmEmployeeDirectory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub
    Private Sub lstEmployeeNames_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstEmployeeNames.SelectedIndexChanged
        Names = CType(lstEmployeeNames.SelectedItem, Employees).Names
        Me.Text = Names.ToString
    End Sub
End Class


'Private Sub btnChooseName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChooseName.Click
'user chooses name to get their employee ID, Dept Name and Phone number


'End Sub
'End Class
 
maybe use Structures/Classes instead?

Are you familiar with Structures or Classes? If so i would suggest using a structure or class instead of storing the data in separate arrays. I am assuming that from your example, "Anthony has id:2562, name: legal, and number: 773-651-9087"

So an example of using a structure could be something like:

Public Class Form1
    Structure Employee
        Property Name As String
        Property ID As Integer
        Property Dept As String
        Property Number As String
    End Structure

    Dim mstrEmployees() As Employee = {New Employee With {.Name = "Anthony", .ID = 2562, .Dept = "Legal", .Number = "773-651-9087"}, _
                                      New Employee With {.Name = "Billy", .ID = 2172, .Dept = "Engineering", .Number = "773-342-3997"}} 'and so forth if you want to do it in one line

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.DataSource = mstrEmployees
        ListBox1.DisplayMember = "Name"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim selectedItem As Object = ListBox1.SelectedItem

        If (Not selectedItem Is Nothing) AndAlso (TypeOf selectedItem Is Employee) Then
            Dim employee As Employee = CType(selectedItem, Employee)

            MsgBox(String.Format("Name: {0}{4}ID: {1}{4}Dept: {2}{4}Number: {3}", employee.Name, employee.ID, employee.Dept, employee.Number, Environment.NewLine))
        End If
    End Sub
End Class


Instead of using a msgbox like shown in example you can create a new form and pass the properties of the employee to it.

And if you do not want to use structures you can just get the data from the arrays that you have by using an Index number, since they should all have the same index number?

I would suggest using a structure or class though, maybe you can even use a DataTable if you like? But personally i think the easiest way would be to use a structure or class for this case. Maybe use a list/dictionary or arraylist that way its a bit more dynamic, but you can just use an array if you like.
 
thank you so much so would I declare the the new as a instance in the same form and put the results shown???
 
actually the only reason why I did arrays was because the chapter i read about was all about arrays this does seem faster though,
 
actually the only reason why I did arrays was because the chapter i read about was all about arrays this does seem faster though,

Well using the code you posted in #8, just get the selectedindex from the listbox. Use that selected index to get the corresponding data for the employee.

For ex: say Anthony is selected, that should return 0 as the selected index number (assuming you have it displayed in that order)
So then just retrieve the id,number and so forth from the other arrays using the same index number.

maybe something like this
        Private Sub btnChooseName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChooseName.Click
            Dim selectedIndex As Integer = lstEmployeeNames.SelectedIndex

            If selectedIndex <> -1 Then
                Dim id As Integer = mintEmployeeID(selectedIndex)
                Dim dept As String = mstrDeparmentName(selectedIndex)

                'something like this if you have variables/properties on form2 that you want to set
                Dim frm2 As New Form2
                frm2.id = id
                frm2.dept = dept
                'Dim frm2 As New Form2(id, dept) 'something like this if you wrote a constructor

                frm2.Show()
                'frm2.ShowDialog() 'if you wanted a dialog

                'MsgBox(id.ToString & dept) 'using a msgbox
            End If
        End Sub


However if you do it using this method, you are assuming that these separate arrays should correspond to each other, if the index does not for some reason then you will get a number/id/etc that does not match the name, which is why i would suggest a class/structure for this case.
 
thank you so much so would I declare the the new as a instance in the same form and put the results shown???
I'm not sure if i understand what your asking here?

Are you talking about the second form that you want to pass the data to?
 
Back
Top