在VB.NET比较阵列 [英] Comparing arrays in VB.NET

查看:214
本文介绍了在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.

我有两个数据源 - 一个是包含部件号,说明,等。另一种是不具有在传统意义上的数据库中的CAD系统一个SQL Server。我试图做的是从SQL Server读出材料清单,并将其与该CAD装配图,以确保CAD系统包含相同的信息作为SQL Server。

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.

从SQL Server获取的数据是相当简单的。我查询数据库并填充DataGrid。完成。快。很简单。

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

从CAD系统获取的数据是一个涉及多一点。我必须加载装配图把所有的零部件的列表,然后加载这些单个的图纸,从图纸拉​​编号属性。这是一个有些耗时和缓慢的过程(不幸),因为每个文件必须实际进行访问。我这些属性加载到一个数组(我猜的名单可能会更有效)。

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).

所以现在我有一个DataGrid和阵列的部件号。我需要对它们进行比较,并相应地着色网格。网格应保持透明,如果两者中存在的部分,颜色排黄色的,如果只在网格存在,并添加如果只在阵列中红色的行。

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. 默认网格黄行。

  2. 循环穿过阵列的电网和循环比较。如果发现匹配,使行透明和从阵列中删除元素

  3. 步骤2完成后,该数组应该只包含未在网格中发现的元素。调整数组删除空元素。

  4. 的数组中的元素添加到网格和颜色这些新行红色。

与此逻辑的问题是,它似乎从性能的角度看昂贵。当然有更好的方法?另外,如果我修改网格以某种方式(如度假)我要再次经历的过程。我真的AP preciate一些这方面的建议。

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.

谢谢!

注:写在Visual Studio 2005

Note: written in Visual Studio 2005.

推荐答案

您可以加载在一个字典(部件编号索引)从CAD系统中的数据。然后,你可以去通过电网并检查它在字典中,这是一个快速的操作存在(O(1))。正如你所说,除去在字典中找到的元素,并在DataGrid中添加其他元素,你可以做的完全一样。

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.

下面是一些code创建和使用的字典(用C#风格的注释,以preserve格式化):

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 ...

编辑:

1)是的,如果你的密钥(在这里,部件号)是一个字符串,那么词典(串,...)将被使用。

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

2)我以为你有一个名为部件类,其中载有关于部分的一些信息。如果你只是有一个编号,并没有其他的信息,那么你可以创建HashSet的来代替。它是基本相同的字典,但这种结构的值也是密钥。您可以创建一个HashSet的是这样的:

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)

我不会通过code的例子,因为它是非常接近的词典。的containsKey变得包含,并添加只接受一个参数(这将是你在这里部件号)。

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)是的,通过他们循环,并将其添加到HashSet的。

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

这篇关于在VB.NET比较阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆