Public Class Form1
Public Structure Stock
Dim prodSKU As String
Dim sizeAmt As Integer
Dim prodName As String
Dim category As String
Dim sellPrice As Decimal
Dim buyPrice As Decimal
End Structure
Dim product(300) As Stock
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
product(0).prodSKU = txtSKU.Text
product(1).prodSKU = "af"
Call CheckData(Stock, txtSKU, product, txtName)
End Sub
Function CheckData(Stock As Object, txtSku As TextBox, product() As Object, txtName As TextBox)
For index = 0 To 299
txtSku.Text = product(index).prodSKU
txtName.Text = product(index).prodName
If txtSku.Text = product(index).prodSKU Or txtName.Text = product(index).prodName Then
Dim Msg, Style, Title, Response
Msg = "That SKU/Product Name already exists, enter another" ' Define message.
Style = vbOK ' Define buttons.
Title = "Repeated Saved Data" ' Define title
Response = MsgBox(Msg, Style, Title)
If Response = vbOK Then ' User chose Ok
Exit Function
End If
End If
Next
End Function
End Class
我正在尝试使用结构作为参数之一来调用函数“ CheckData”,但似乎无法弄清楚。注意:相对于VB来说,这是新手,请保持柔和。
答案 0 :(得分:0)
首先,请启用严格选项。 您可以在以下位置找到此设置 工具菜单->选项->项目和解决方案-> VB默认值。 这样可以避免在运行时出现错误。
Function CheckData(Stock As Object, txtSku As TextBox, product() As Object, txtName As TextBox)
Stock As Object
将变量与结构命名为一个好主意。参数的模式是“变量名”为“数据类型”。您的结构是公开的,因此可以通过每种方法看到。尝试远离对象作为数据类型。您永远不会在函数中使用变量Stock,所以这是不必要的。记住Stock是一个局部变量。它与称为股票的结构无关。它是对象类型。
txtSku As TextBox
看来txtSku
是您表单上的TextBox
。不必将其传递给您的函数,因为该函数可以直接访问它。
product() As Object
product()
是一个表单级别变量,因此对于该表单的所有方法均可见。没必要。
txtName As TextBox
与#2相同-不必要
Function
需要一个DataType
私有函数CheckData()为布尔值
和函数主体中的返回
Return True
它不必是Boolean
,可以是任何类型。这只是一个例子。
由于未返回任何内容,因此应为Sub
过程。
Dim product(300) As Stock
声明具有301个元素的数组,索引从0到300。在vb.net中,数组声明为product(UBound)或上限。这为301库存结构保留了一些内存。使用List(Of T)T代表Type可能更好。因此,您的列表将是
Dim product as New List(Of Stock)
通过这种方式,您不必担心列表中有多少项。
现在进入您的btnSave_Click
创建Stock
将stck设置为新库存
设置一些属性
stck.prodSku = txtSKU.Text
添加到产品列表
product.Add(stck)
重复使用相同的变量,将其设置为Stock的新实例
stck = New Stock
stck.prodSKU = "af"
procuct.Add(stck)
在这种情况下和大多数情况下,无需使用Call
关键字。
CheckData()
Public Structure Stock
Dim prodSKU As String
Dim sizeAmt As Integer
Dim prodName As String
Dim category As String
Dim sellPrice As Decimal
Dim buyPrice As Decimal
End Structure
'A plural name helps identify this as a collection
Private products As New List(Of Stock)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Create a new Stock object
Dim stck As New Stock
stck.prodSKU = txtSKU.Text
'Add the Stock object to the list
products.Add(stck)
stck = New Stock
stck.prodSKU = "af"
products.Add(stck)
End Sub
Private Sub CheckData()
For Each item As Stock In products
'These values will be overwritten on every iteration of the loop
txtSku.Text = item.prodSKU
txtName.Text = item.prodName
'Well this is kind of silly you just set these properties so of course it is true!
'If txtSku.Text = item.prodSKU Or txtName.Text = product(Index).prodName Then
'With Option Strict uninitiated varaibles must have a type
'Dim Msg, Style, Title, Response
'the variable is assigned a value so the compiler now knows the type
Dim Msg = "That SKU/Product Name already exists, enter another" ' Define message.
Dim Title = "Repeated Saved Data" ' Define title
'Since the user can only click OK this loop will only go once
Dim Response As DialogResult = MessageBox.Show(Msg, Title, MessageBoxButtons.OK)
If Response = vbOK Then ' User chose Ok
Exit Sub
End If
Next
End Sub