0

我正在使用 VS 2012。

我不知道如何在我的代码运行时动态声明变量。我正在尝试编写一个程序,从网站上提取 EMS 调度的数据。该网站每隔几秒钟更新一次,并且发布的每个呼叫都有一个唯一的 ID 号。我想使用每个调度的唯一 ID 号,并使用该唯一 ID 号声明一个新的事件变量,并将其添加到活动调度的集合中。我唯一想不通的部分是,如何在发布时为每个调度声明一个变量,并用其唯一的 ID 号命名?

就像是:

Structure Incident
  Dim id As Integer

  Dim numer As String
  Dim date1 As String
  Dim time As String
  Dim box As String
  Dim type As String
  Dim street As String
  Dim crosstreet As String
  Dim location As String
  Dim announced As Boolean
  Dim complete As Boolean
End Structure

UniqueIDstringfromwebsite = "1234"

Dim (code to get variable declared with unique ID as variable name) as incident

这是我的第一篇文章,我不能完全让代码示例在帖子中正常工作。

4

2 回答 2

1

You can use a Dictionary to identify your var:

Dim myVar As New Dictionary(Of Integer, Incident)

UniqueIDstringfromwebsite = 1234
myVar.Add(UniqueIDstringfromwebsite, New Incident)

I don't think you can change the name of a variable with a value dinamically, and sincerily, and don't get when it can be useful.

And, in this way, better turn your structure into a class.

于 2013-03-19T15:22:59.430 回答
1

这是我的第一个答案——所以你们相处得很好!

你不需要实现一个类而不是一个结构 - 这样你就可以实现一个构造函数。我不认为您想创建一个以 ID 作为名称的变量,但您应该将 ID 添加到调度集合中(这可能是一个列表(事件):

例如

Public Class Incident

     'define private variables

     Private _id as integer
     Private _number as string
     Private _date1 as String
     Private _time as String
     'etc...

     'define public properties

    Public Property Number as string
        Get 
            Return _number
        End Get
        Set (value as string)
            _number = value
        End Set
    End Property

    'Repeat for each Public Property

    'Implement Constuctor that takes ID
    Public Sub New(id as integer)
        _id = id
        'code here to get incident properties based on id
    End Sub

End Class

希望这可以帮助!

于 2013-03-19T15:33:17.057 回答