我有一个循环通过数组的VB.Net程序,试图弄清楚#34;输送机上的瓶子在哪里?#34;。该计划的目的是直观地显示工作人员,输送机如何使用VB.net和标签工作。这很难解释,所以我会尽我所能。
Bottle_Number(10)Bottle_Position(128)
我想在输送机上的所有128个站点跟踪10瓶。
我们有一个只能装10瓶的输送机。我需要追踪每瓶10瓶的位置。一旦瓶子11打开 - 这意味着瓶子1完成并离开传送带。因此,瓶子11变成瓶子1,所以我需要将瓶子1的位置重置为0,并继续跟踪瓶子2-9,同时还跟踪瓶子11(不是瓶子1)。一旦瓶子12打开,它就会变成瓶子2,我需要将瓶子2的位置重置为“0”和“0”。并继续跟踪所有瓶子。
任何帮助将不胜感激。
这是我的代码:
Public Class frmMain
Dim Product_Position(10) As Integer
Dim Inches_Per_Pulse As Integer
Dim PulseNumber As Integer
Dim Product_Counter As Integer
Dim Product_Location(10) As Integer
Dim Function1 As Integer
Dim Function2 As Integer
Dim Function3 As Integer
Dim Function4 As Integer
Dim Function5 As Integer
Dim Function6 As Integer
Dim Function7 As Integer
Dim Function8 As Integer
Dim Function9 As Integer
Dim Function10 As Integer
Dim Product_in_Tunel As Integer
Dim test As Integer
Dim Roll_OVer As Boolean
Dim Product_Counter_Test As Integer
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
lblStatus.BackColor = Color.Green
lblStatus.Text = "Conveyor Status: Running"
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
lblStatus.BackColor = Color.Red
lblStatus.Text = "Conveyor Status: Off"
End Sub
Private replace_next As Integer
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
If Product_Counter = 10 Then
replace_next += 1
If replace_next > 10 Then
replace_next = 1 ' replace them in turn 1..10, then loop back to 1
Product_Position(replace_next) = 0 ' put initial position here
End If
End If
Product_Counter = Product_Counter + 1
If Product_Counter > 10 Then
Product_Counter = 1
Roll_over = True
End If
'MsgBox(Product_Counter)
'MsgBox(replace_next)
End Sub
Private Sub btnPulse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPulse.Click
Get_Location()
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PulseNumber = "0"
Inches_Per_Pulse = "1"
Roll_OVer = False
'MsgBox("Test")
End Sub
Public Sub Get_Location()
'MsgBox(Product_Counter)
If Roll_OVer = True Then
Product_Counter_Test = 10
'MsgBox("i'm stuck here")
End If
If Roll_OVer = False Then
Product_Counter_Test = Product_Counter
End If
'MsgBox(Product_Counter_Test)
'MsgBox("am I here - Yes")
For test = 1 To Product_Counter_Test
'MsgBox("This works")
Product_Position(test) = Product_Position(test) + Inches_Per_Pulse
Next
PulseNumber = PulseNumber + 1
ClearLabels()
lblProduct1Position.Text = Product_Position(1)
lblProduct2Position.Text = Product_Position(2)
lblProduct3Position.Text = Product_Position(3)
lblProduct4Position.Text = Product_Position(4)
lblProduct5Position.Text = Product_Position(5)
lblProduct6Position.Text = Product_Position(6)
lblProduct7Position.Text = Product_Position(7)
lblProduct8Position.Text = Product_Position(8)
lblProduct9Position.Text = Product_Position(9)
lblProduct10Position.Text = Product_Position(10)
End Sub
Public Sub ClearLabels()
lblProduct1Position.Text = ""
lblProduct2Position.Text = ""
lblProduct3Position.Text = ""
lblProduct4Position.Text = ""
lblProduct5Position.Text = ""
lblProduct6Position.Text = ""
lblProduct7Position.Text = ""
lblProduct8Position.Text = ""
lblProduct9Position.Text = ""
lblProduct10Position.Text = ""
End Sub
“脉冲”按钮实际上是驱动传送带的,每个脉冲(单击按钮)表示传送带正在向前移动。
现在,一旦程序进入第11瓶,它就会重置并且只会向前移动#34;新的"瓶子(瓶子1)。它应该继续增加剩余的瓶子直到它们到达终点并为它们做同样的事情 - 将位置重置为0并重新开始计数。
答案 0 :(得分:2)
你的传送带是FIFO(先进先出),所以不是不断地移动,重新索引和/或重建(=重置?)一个数组,使它 看起来像是FIFO < / em> ,Net包含Queue(Of T)
集合 ,即FIFO 。
也可以使用LinkedList(Of T)
。普通List(Of T)
也可以工作,但是如果添加/删除频率很高,那将导致在阵列引擎盖下发生相同的低效转换。
唯一的问题是强制执行大小限制,使用小型包装器可以轻松处理。我认为瓶子除了它们的位置之外还有一些有趣或可识别的东西。测试代码使用序列ID和内容。
Friend Class Bottle
Public Property Contents As String
Public Property SequenceID As Int32
' etc
Public Overrides Function ToString() As String
Return String.Format("{0}: ({1})", SequenceID.ToString("00"), Contents)
End Function
End Class
您可能需要显示更多相关信息。集合类:
Friend Class BottleQueue
Private mcol As Queue(Of Bottle)
Private lbls As Label()
Private MaxSize As Int32 = 10 ' default
Public Sub New(size As Int32)
MaxSize = size
mcol = New Queue(Of Bottle)
End Sub
Public Sub New(size As Int32, l As Label())
Me.New(size)
lbls = l
End Sub
Public Sub Add(b As Bottle)
mcol.Enqueue(b)
Do Until mcol.Count <= MaxSize
mcol.Dequeue()
Loop
UpdateDisplay()
End Sub
Public Function Peek() As Bottle
Return mcol.ElementAtOrDefault(0)
End Function
Public ReadOnly Property Count() As Int32
Get
Return mcol.Count
End Get
End Property
Public Function Remove() As Bottle
Dim b As Bottle = Nothing
If mcol.Count > 0 Then
b = mcol.Dequeue
UpdateDisplay()
End If
Return b
End Function
Private Sub UpdateDisplay()
Dim n As Int32
If lbls Is Nothing OrElse lbls.Count = 0 Then
Return
End If
For n = 0 To mcol.Count - 1
lbls(n).Text = mcol.ElementAtOrDefault(n).ToString
Next
For n = n To lbls.Count - 1
lbls(n).Text = "(empty)"
Next
End Sub
Public ReadOnly Property GetQueue As Bottle()
Get
Return mcol.ToArray()
End Get
End Property
End Class
该类有2个内置显示方式。一个更新一组标签。由于它是一个集合,它还提供了一种获取当前集合的方法,以便进行集合类型控制,例如Listbox
。更好的方法是,如果集合本身是“可观察的”,那么它可以用作数据源。
它还提供了一种手动Remove
下一个瓶子的方法。从特定索引(例如Remove(3)
)中删除与Queue
是对立的,所以它没有实现。
测试代码:
' form level vars:
Private BottleQ As BottleQueue
Private BottleID As Int32 = 7
' form load, passing the labels to use
' using a queue size of FIVE for test
BottleQ = New BottleQueue(5, New Label() {Label1, Label2, Label3, Label4, Label5})
添加项目:
Dim material = {"Napalm", "Beer", "Perfume", "Pepsi", "Cyanide", "Wine"}
' add new bottle with something in it
BottleQ.Add(New Bottle With {.Contents = material(RNG.Next(0, material.Count)),
.SequenceID = BottleID})
BottleID += 1
' clear and show the contents in a listbox:
lbQueView.Items.Clear()
lbQueView.Items.AddRange(BottleQ.GetQueue)
BottleId
任意从7开始,内容是随机的。 BTW,material
显示了我使用数组的唯一方法:当内容被修复并提前知道时。在几乎所有其他情况下,一种或另一种.NET集合可能是更好的选择。
因为它不是可观察的集合(并且与FIFO性质有点不一致),所以每次都需要清除列表框。这可能是类的内部,就像标签显示一样。结果:
在右边,前5个按顺序显示;稍后点击3次,结果显示在左侧:所有内容都向上移动了3个,并添加了3个新项目。
注意:如果使用此代码的代码需要知道何时/哪个Bottle
从传送带中移除,则该类可能包含ItemRemoved
事件,该事件在添加力量时提供刚删除的项目/瓶子出。情况可能就是这样,但问题并没有提及。