在VB.NET中尽可能快地遍历自制按钮坐标

时间:2014-11-28 08:48:06

标签: arrays vb.net datatable

是否有更好/更快的方式临时存储30-40个矩形坐标而不是创建数据表并循环迭代?

    Dim optionbuttons As New DataTable
Private Sub addcolumns()
    optionbuttons.Columns.Add("name", GetType(String))
    optionbuttons.Columns.Add("x1", GetType(Integer))
    optionbuttons.Columns.Add("y1", GetType(Integer))
    optionbuttons.Columns.Add("x2", GetType(Integer))
    optionbuttons.Columns.Add("y2", GetType(Integer))
    optionbuttons.Columns.Add("noCode", GetType(Integer))
    optionbuttons.Columns.Add("level", GetType(Integer))
End Sub

任何有关临时存储此数据的更好方法的建议都将受到欢迎。这些选项是通过linq-sql从sql server中提取的。

在JavaScript中,我曾经使用关联数组来存储这些数据,但我不确定这是否可以在vb.net中存在

2 个答案:

答案 0 :(得分:1)

我首先要创建一个代表我的矩形的类:

Public Class MyButton
    Public Sub New(ButtonName As String, x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer, ButtonNoCode As Integer, ButtonLevel As Integer)
        Me.Name = ButtonName
        Me.UpperLeft = New Point(x1, y1)
        Me.BottomRight = New Point(x2, y2)
        Me.NoCode = ButtonNoCode
        Me.Level = ButtonLevel
    End Sub

    Public Property Name As String
    Public Property UpperLeft As Point
    Public Property BottomRight As Point
    Public Property NoCode As Integer
    Public Property Level As Integer
End Class

然后我将填充数据表中的列表:

Public Function PopulateListFromDataTable(dt As DataTable) As List(Of MyButton)
    Dim ReturnValue As New List(Of MyButton)
    For Each r As DataRow In dt.Rows
        ReturnValue.Add(New MyButton(r("name"), r("x1"), r("y1"), r("x2"), r("y2"), r("noCode"), r("level")))
    Next
    Return ReturnValue
End Function

这为您提供了具有命名属性的类的所有细节,并且它们全部位于可以轻松迭代的列表中:

For Each b As MyButton In MyListOfButtons

答案 1 :(得分:0)

   Public Function PopulateList(name As String, x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer, nocode As Integer, level As Integer) As MyButton

    Return (New MyButton(name, x1, y2, x2, y2, nocode, level))

   End Function

这是正确的吗?