[RESOLVED] [2005] Struggling with array
Hello,
I'm struggling with something that is pretty basic :blush:
I want to create a Products array which stores the product id, a Quantity, and a Value.
I then want to loop through a DataTable containing the data, and put certain data from this table into the array.
With the array, I simply want to send this array over to another form (this bit, I can cope with), and display it in a ListView.
I've been messing around with brackets and curly brackets for ages, to no avail.
Re: [2005] Struggling with array
why not create a list of a structure something like this
Code:
Public Class Form1
Private Structure Product
Sub New(ByVal iID As Integer, ByVal iQuantity As Integer, ByVal iValue As Object)
ID = iID
Quantity = iQuantity
Value = iValue
End Sub
Dim ID As Integer
Dim Quantity As Integer
Dim Value As Object
End Structure
Private Products As New List(Of Product)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Products.Add(New Product(1, 14, 1.456))
End Sub
End Class
Re: [2005] Struggling with array
If all productid, qty and value are of the same datatype (double, for example) then you can use a strongly typed array... Otherwise, you'll have to declare your array as an array of object then cast it back to the correct type when retreiving the values form the elements... Having said that, it's a bad idea to use an array of objects. So instead, you should create a Product structure or class then create an array of type Product.
Re: [2005] Struggling with array
Excellent. Thanks guys. I'd have never have thought of doing this.
Something else to learn!!!! :rolleyes: