Results 1 to 7 of 7

Thread: Ensuring UNIQUE values in arrays

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2003
    Posts
    26

    Ensuring UNIQUE values in arrays

    I'm having problems ensuring that one of the values in my array is unique.
    I have a file that I open to get the information from, validate it, and then put it into an array of structures.
    After validating all the data, I want to make sure that all the IDnumbers are unique. Is there a simple way to do this?

    Here is a snippet of the code:

    Dim x As Integer : Dim y As Integer : Dim size As Integer
    Dim intIndex As Integer : Dim intIndexToRemove As Integer

    size = Customers.GetUpperBound(0)
    MessageBox.Show("SIZE " & size)
    For x = 0 To size - 1
    For y = 0 To size
    If Customers(x).IDNumber = Customers(y).IDNumber Then
    If (Customers(x).Name <> Customers(y).Name) OrElse (Customers(x).Address <> Customers(y).Address) Then
    intIndexToRemove = y
    For intIndex = intIndexToRemove To UBound(Customers) - 1
    Customers(intIndex) = Customers(intIndex + 1)
    Next
    ' Delete the last entry
    ReDim Preserve Customers(UBound(Customers) - 1)
    size -= 1
    End If
    End If
    Next
    Next

  2. #2
    Lively Member
    Join Date
    May 2002
    Posts
    94
    you should just be able to compare the array item with the next.

    Code:
      If Customers(x) = Customers(y) Then...
    If your post is resolved, mark it as such using the thread tools, Keep the forums tidy.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2003
    Posts
    26
    When I try that it ends up reading past the end of the boundary. It goes through the outter loop once, and then crashes.

  4. #4
    Lively Member
    Join Date
    May 2002
    Posts
    94
    ok could you explain a little more on what your trying to accomplish, are your trying to merge 2 arrays together or remove the items in array2 that are dups in array1.
    If your post is resolved, mark it as such using the thread tools, Keep the forums tidy.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2003
    Posts
    26
    Ok. I read a record from a file, validate the data, then move the data into an array of structures. One of the fields in the structure is ID_Number. I feel it is important that this value be unique. Before loading the ID_Numbers from the array into a combo box I want to make sure that all the ID_Numbers are unique.
    If they are not unique then an error message will be built based on the line of input and the message that the ID is not unique, and will be written into a rich textbox that contains my error log. (this is done to prevent the program from crashing by flagging the records with invalid data).

    My problem is that when I come across two ID_Numbers that are the same and try to delete one, the program crashes when it is about to begin to loop through and compare the values of the next ID_Number.

    The file is read sequentially and the ID_Numbers in the data file are not necessisarily in order. I suppose I could put them in order before comparing, but that would involve a lot of swap statements as there are several other fields in the structure.

  6. #6
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    You could store them in a hashtable:

    C# Psuedocode:
    Code:
    Hashtable h = new Hashtable();
    
    while(Structure = ReadFromFile())
    {
         if (h.ContainsKey(Structure.UniqueID))
         {
               MessageBox.Show("This ID Already Found!");
         }
         else
         {
               h.Add(Structure.UniqueID, Structure);
         }
    }
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  7. #7
    Lively Member
    Join Date
    May 2002
    Posts
    94
    Use Classes to create Objects that can hold your information.

    I have attached a form, I created 2 classes for you, 1 a xitem class, and 2 a xitemcollection class, this is for demonstration...

    assign each row in your database result to a xitem and then add it to a xitem collection. Only items with unique ids can be added to a xitemcollection. so then all you would have to do to compare these items would be to add the 2 collection items to a new collection. It might sound confusing, so just break thru the code and set up watches to see whats going on...

    this way you don't need to use structures and you can control the collection a little better.

    Hope this helps
    Attached Files Attached Files
    If your post is resolved, mark it as such using the thread tools, Keep the forums tidy.

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