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
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.
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.
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.
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
If your post is resolved, mark it as such using the thread tools, Keep the forums tidy.