Results 1 to 15 of 15

Thread: [RESOLVED] [2005] newbie array question

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved [RESOLVED] [2005] newbie array question

    Hi all

    I have three quick questions about arrays


    1) arrays vs arraylists

    as so far they seem the say with the exception that the array list is one dimentional and easier to work with

    can any one else shed any light on the diffrences

    2) can you do a .binarysearch in an array
    edit: i know you can but does it search each feild of the array or can you spesify that you only want to search on feild of it.

    3) if i wanted to store info with severl feilds

    name1, IT , 23.12
    name2, CI , 40.12
    name3, AD , 12.01

    would a multi dimentional array be able to hold this sort of data?

    would it look something like
    Dim newarray(1, 1, 1) As String
    Last edited by Crash893; Jul 12th, 2006 at 04:06 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,237

    Re: newbie array question

    To answer the third question: Don't use a multidimensional array for that. Instead, use an array of either structures or classes, each of which holds the three data items you have there, probably two strings and a double. This will be MUCH easier to work with when compared with a multidimensional array.

    A partial answer to the first question: Look at the additional methods. You can use either one, but arraylist will have some added functionality. I'm not sure which I prefer to use. If arraylist is easier for you to use, then use it, since you will not see any real cost to using it.

    I have no answer to 2.
    My usual boring signature: Nothing

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,439

    Re: newbie array question

    An ArrayList is implemented as a wrapper for a one-dimensional array of Object. The default Length of the array is 16 and the ArrayList class keeps an internal variable that indicates how many elements are to be exposed as items. This variable is exposed through the Count property. The advantage of the ArrayList over an array is that when you need to exceed the current size all the resizing is done for you rather than you having to use ReDim Preserve. Each time the array is resized it doubles in size. This would suggest that calling BinarySearch on an ArrayList simply executes Array.BinarySearch on the internal array.

    A multidimensional array could hold the data you want but I'd suggest that it is a poor choice. A multidimensional array is a matrix, and as such it is "proper" that every element have an equivalent meaning. There are no row or column objects in a multidimensional array. I'd suggest defining a class or structure to hold each row of your data and then creating a one-dimensional array of instances of that type. This is OOP after all, so you should use objects.

  4. #4

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: newbie array question

    Here is what i got so far
    im having trouble searching through the collection
    any advice


    VB Code:
    1. Public Class Form1
    2.     Dim list As New ArrayList
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim newuser As New user
    5.         newuser.username = TextBox1.Text
    6.         newuser.department = TextBox2.Text
    7.         newuser.domestictotal = TextBox3.Text
    8.         list.Add(newuser)
    9.         TextBox1.Clear()
    10.         TextBox2.Clear()
    11.         TextBox3.Clear()
    12.     End Sub
    13.  
    14.     Public Structure user
    15.         'Declare data members
    16.         Public username As String
    17.         Public department As String
    18.         Public domestictotal As Decimal
    19.     End Structure
    20.  
    21.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    22.  
    23.         Dim idx As Integer = list.BinarySearch(TextBox4.Text)
    24.         MessageBox.Show(idx)
    25.     End Sub
    26.  
    27. End Class

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,439

    Re: newbie array question

    Your ArrayList contains user objects and you're doing a BinarySearch for a String object. There's no way that you can ever find a match that way. What you would need to do is create a new user object with presumable the username field set to the string you want to search for, then call the overload of BinarySearch that takes an IComparer object as a parameter. Your IComparer implementation will have to return a value based on the username field of the two objects being compared.

    Also, what conventions you use are up to you but I recommend following the generally accepted convention of using title case for type names, camel case for private member or local variables and title case for public fields. That would make your type 'User' rather than 'user', your 'newuser' variable would be 'newUser' and your 'username', 'department' and 'domestictotal' fields would be 'UserName', 'Department' and 'DomesticTotal'. That will keep your code in line with the conventions used in the Framework and make it easier to read.

  6. #6

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: newbie array question

    that may be a bit out of my current understanding

    but couldnt i also make a sorted list
    with the key as the user name and the collection as the value

    then search on the key and just add to the feild in structure that is relevant?


    im also unsure how to display the information of the structure in the array

    would it look something like

    textbox1.text = list(x).department

  7. #7

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: newbie array question

    Well i now have a working sorted list of structures

    and it works beautifuly

    my main hurdle now is how to display the info

    below is my code to display the key and the value ( provided the value is a decimal ( money)

    but with the structure im not sure how to do this

    VB Code:
    1. Dim x As String
    2.         Dim y As Decimal
    3.         Dim total As Decimal
    4.         Dim i As Integer
    5.  
    6.         For i = 0 To ipassinfo.Count - 1
    7.             x = ipassinfo.GetKey(i)
    8.             y = ipassinfo.GetByIndex(i)
    9.  
    10.             total = y + total
    11.             tb_usertotals.AppendText(x.PadRight(20) & ControlChars.Tab & y.ToString("C") & vbNewLine)
    12.             Me.TextBox1.Text = i
    13.         Next i
    14.         Me.TextBox2.Text = total.ToString("c")

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,439

    Re: newbie array question

    When you get a value from the SortedList you are getting a User object. How do you normally get a field or property from an object? A structure is just like a class in this regard.

  9. #9

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: newbie array question

    Im sorry i think i may be missing something (not out of the norm)

    if i usually wanted to get the value for a structure

    i would just do something like
    textbox1.text = structure.structureelement

    but with the sortedlist


    im lost on how to spesify witch structure

    im thinking its something like



    Dim x As String
    Dim y As Decimal
    Dim total As Decimal
    Dim i As Integer

    For i = 0 To ipassinfo.Count - 1
    x = ipassinfo.GetKey(i)
    With ipassinfo.GetByIndex(i)
    y = userinfo.domestictotal
    End With
    total = y + total
    tb_usertotals.AppendText(x.PadRight(20) & ControlChars.Tab & y.ToString("C") & vbNewLine)
    Me.TextBox1.Text = i
    Next i
    Me.TextBox2.Text = total.ToString("c")


    seems like its the right path but it returns the same value for each person

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,439

    Re: newbie array question

    That's becuase you are assigning the value of userinfo.domestictotal to the 'y' variable every time. If you want the domestictotal field of the object from the collection then you have to get the domestictotal field of the object from the collection:
    VB Code:
    1. y = DirectCast(ipassinfo.GetByIndex(i), user).domestictotal
    Also, if you're just going to enumerate the SortedList like that and not use the usernames as a key to access the values then there's no point using a SortedList. You may as well just use an ArrayList. You would then enumerate the objects in the ArrayList and get the username and domestictotal fields from each object:
    VB Code:
    1. For Each usr As user In myArrayList
    2.     MessageBox.Show(String.Format("UserName: {0}, DomesticTotal: {1}", usr.username, usr.domestictotal))
    3. Next usr

  11. #11

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: newbie array question

    Iím still using the user name as the key

    So that I can consolidate the different entries by testing weather or no the username already exists

    I have to come through one more time after this to read in a text file to assignee each user a department (provide to me from HR)

    I included user name in my structure only as redundancy ill probably trim it later

  12. #12

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: newbie array question

    seems only to return the last entry into the structure
    VB Code:
    1. y = DirectCast(ipassinfo.GetByIndex(i), user).domestictotal


    IF i dont user a sorted list with the username as the key
    how can i test and see if the user has already been created.

    Also, if you're just going to enumerate the SortedList like that and not use the usernames as a key to access the values then there's no point using a SortedList. You may as well just use an ArrayList. You would then enumerate the objects in the ArrayList and get the username and domestictotal fields from each object:

    visual basic code:
    VB Code:
    1. For Each usr As user In myArrayList
    2.         MessageBox.Show(String.Format("UserName: {0}, DomesticTotal: {1}", usr.username, usr.domestictotal))
    3.     Next usr

  13. #13
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,439

    Re: newbie array question

    If I'm not mistaken the ArrayList.Contains method calls the Equals method of the items to determine whether the specified value is present. If you override the Equals method to compare just the user name to determine equality then that will work, although you'd have to be sure that that was compatible with how you were going to use the structure elsewhere. Another method would be to use BinarySearch and pass an IComparer object that compared just the suer name of two user objects to determine relative values.

  14. #14

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: newbie array question

    something like?

    VB Code:
    1. Dim ipassarray As New ArrayList
    2.                 userinfo.username = "Robert"
    3.                 userinfo.department = "???"
    4.                 userinfo.domestictotal = 12.01
    5.                 ipassarray.Add(userinfo)
    6.  
    7.                 If ipassarray.Equals(userinfo.username) Then
    8.                     MessageBox.Show("hi")
    9.                 End If

  15. #15

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: newbie array question

    VB Code:
    1. Dim userarray As user()
    2.                 ReDim userarray(ipassinfo.Values.Count - 1)
    3.  
    4.                 ipassinfo.Values.CopyTo(userarray, 0)
    5.                 'Array.Sort(userarray, New MyStructComparator)
    6.                 Dim u As user
    7.                 For Each u In userarray
    8.                     MessageBox.Show(String.Format("UserName: {0}, DomesticTotal: {1}", u.username, u.domestictotal))

    displays fine

    the problem is its still displaying the wrong value
    and i am completely lost becuase if i add look in the list and see the user.domestictotal i get 284.23

    but when i run this it says 47.21 which i checked and is the first entry in her list so its like its just displaying the first value

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