Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing arrays in VB.NET

Tags:

arrays

sql

vb.net

Let me provide a little detail to explain what I'm trying to accomplish before I get into the nuts and bolts of the question.

I've got two data sources - one is a SQL Server containing part numbers, descriptions, etc. The other is a CAD system that does not have a database in a traditional sense. What I'm trying to do is read out the bill of materials from the SQL Server and compare it to the CAD assembly drawing to ensure that the CAD system contains the same information as the SQL Server.

Getting the data from the SQL Server is fairly straight forward. I query the database and populate a datagrid. Done. Quick. Easy.

Getting the data from the CAD system is a little more involved. I have to load the assembly drawing to get a listing of all the component parts and then load those individual drawings to pull the "Part Number" property from the drawing. This is a somewhat time consuming and slow process (unfortunately) since each of the files must actually be accessed. I load those properties into an array (I guess a list might be more efficient).

So now I have a datagrid and array with part numbers. I need to compare them and colorize the grid accordingly. The grid should remain transparent if the part exists in both, color the row yellow if it only exists in the grid, and add a row colored red if only in the array.

As best I can tell, this means looping through the array on each line of the grid. The thought process is this:

  1. Default the grid to yellow rows.
  2. Loop through the grid and loop through the array to compare. If a match is found, make the row transparent and delete the element from the array.
  3. After step 2 is completed, the array should only contain elements that are not found in the grid. Resize the array to remove the empty elements.
  4. Add the elements of the array to the grid and color those new rows red.

The problems with this logic is that it seems expensive from a performance standpoint. Surely there is a better method? Also, if I modify the grid in some manner (like a resort) I have to go through the process again. I'd really appreciate some advice on this.

Thanks!

Note: written in Visual Studio 2005.

like image 693
Mr Furious Avatar asked Dec 18 '22 07:12

Mr Furious


1 Answers

You could load the data from the CAD system in a dictionary (indexed by part number). Then you could go through the grid and check if it exists in the dictionary, which is a fast operation ( O(1) ). You could do exactly as you say, remove the found elements in the dictionary and add the remaining elements in the datagrid.

Here's some code for creating and using a dictionary (used C# style comments to preserve formatting):

//First argument is your key type, second is your item type
Dim cadParts As New Dictionary(Of Integer, Part)

//Add items to the parts dictionary
For Each part As Part In cadPartsArray
  cadParts.Add(part.PartNumber,part)
Next

//Check if a part exists
Dim partNumber As Integer = 12345
If cadParts.ContainsKey(partNumber) ...

//Remove a part
cadParts.Remove(partNumber)

//Go through the remaining values
For Each part As Part In cadParts.Values ...

Edit:

1) Yes, if your key (here, part number) is a string, then a Dictionary(Of String,...) would be used.

2) I assumed you had a class named Part, which contained some information about a part. If you just have a part number, and no other info, then you could create a Hashset instead. It is basically the same as a dictionary, but with this structure the value is also your key. You would create a hashset like this:

Dim cadParts As New Hashset(Of String)

I won't go through code examples because it is very close to the Dictionary. ContainsKey becomes Contains, and Add accepts only one argument (which would be your part number here).

3) Yes, loop through them and add them to the hashset.

like image 176
Meta-Knight Avatar answered Dec 22 '22 00:12

Meta-Knight