[英]VB.net - Adding to an ArrayList
This is my first time using ArrayList
. 这是我第一次使用
ArrayList
。 I have tried to add to BarcodeArray
( as an arraylist ) and execution is failing with the error message: 我试图添加到
BarcodeArray
( 作为arraylist ),执行失败并显示错误消息:
Object reference not set to an instance of an object.
你调用的对象是空的。
My code is shown below: 我的代码如下所示:
'Populate the arrays
BarcodeArray.Add(txt_Barcode.Text)
CategoryArray.Add(cmb_Categories.Text)
TitleArray.Add(txt_Title.Text)
DescriptionArray.Add(txt_Description.Text)
QuantityArray.Add(txt_Quantity.Text)
RRPArray.Add(txt_RRP.Text)
CostArray.Add(txt_Cost.Text)
This message appears when line 2 is executed. 当执行第2行时,将显示此消息。 How do I add text to an ArrayList from a textbox without getting this error?
如何在不出现此错误的情况下从文本框向ArrayList添加文本?
In .NET, you need to instantiate objects before you can call methods on them. 在.NET中,您需要实例化对象,然后才能在对象上调用方法。 Example:
例:
Dim a As ArrayList
a.Add(...) ' Error: object reference `a` has not been set
The solution is to initialize the variable with a new ArrayList: 解决方案是使用新的 ArrayList初始化变量:
Dim a As ArrayList
a = New ArrayList()
a.Add(...)
or, alternatively: 或者,或者:
Dim a As New ArrayList()
a.Add(...)
BTW, ArrayList
is an old class that mainly exists for backwards compatibility. 顺便说一句,
ArrayList
是一个旧类,主要为了向后兼容而存在。 When you are starting a new project, use the generic List
class instead: 在开始新项目时,请改用通用
List
类 :
Dim a As New List(Of String)()
a.Add(...)
This issue you're having is that you are not instantiating your ArrayLists before using them. 您遇到的问题是,在使用它们之前没有实例化ArrayList。 You would need to do something like this to get your code to work:
您需要执行以下操作才能使代码正常工作:
Dim barcodeArray as New ArrayList()
barcodeArray.Add(txt_Barcode.Text)
... etc ...
But in your case, I think I would create a new class: 但就您而言,我想我将创建一个新类:
Public Class Product
Public Property Barcode as String
Public Property Category as String
Public Property Title as String
... etc ...
End Class
Then I would use it in code like this: 然后,我将在以下代码中使用它:
Dim productList as New List(Of Product)()
productList.Add(new Product() With {
.Barcode = txt_Barcode.Text,
.Category = cmb_Categories.Text,
.Title = txt_Title.Text,
... etc ...
})
This would let you use a single Product
object rather than separate ArrayList
objects, which would be a maintenance nightmare. 这将使您可以使用单个
Product
对象,而不是单独的ArrayList
对象,这将是维护的噩梦。
You need to instantiate it before you can use. 您需要实例化它才能使用。 The problem in line 2 is that it is
null
at that time ( Nothing
in VB.NET ) since it is not created 第2行中的问题在于,由于当时未创建,所以它当时为
null
(在VB.NET中为 Nothing
)。
Since all the values you want to add to the list has same type which is String
i suggest you use a List(Of String)
instead of ArrayList
由于您要添加到列表中的所有值都具有与
String
相同的类型 ,因此我建议您使用List(Of String)
而不是ArrayList
Try the following: 请尝试以下操作:
Dim BarcodeArray as List(Of String) = New List(Of String)( { txt_Barcode.Text } )
Dim CategoryArray as List(Of String) = New List(Of String)( { cmb_Categories.Text } )
' ...
' Same for the other Lists you will need to use
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.