Dynamic Array Problems

TravisJRyan

New member
Joined
Apr 4, 2005
Messages
2
Programming Experience
Beginner
Dynamic Array Problems - Array of created Class Objects

I am in a VB.NET class in college and I am being stumped on this dynamic array attempt.

I figured out the way to redim the array when a new item is added. So that all works, but my array is a place holder for my "pet" object that I have created a class for. It has the properties, Name, Qty, Type, and Price.

My code doesnt work and I am not sure why, it all seems logical to me. . can anyone deciper my code and tell me why it doesnt?

When it adds a new item it just changes the first item in the array and if its a like item then it just changes the qty like it should. . .but the new item part wont work. . .sigh

VB.NET:
#Region " Entry Compile "
	Private Sub Compile_Entry()
		Dim loopIndex As Integer
		Dim itemFound As Boolean
		With Item
			.Name = nameBox.Text
			.Price = Decimal.Parse(priceBox.Text)
			.Qty = Integer.Parse(qtyBox.Text)
			.Type = itemTypeBox.SelectedIndex
		End With
		If ItemIndex = 0 Then
			Inventory(0) = Item
			ItemIndex += 1
		Else
			Do While itemFound = False And loopIndex < ItemIndex
			 If Item.Name = Inventory(loopIndex).Name And Item.Price = Inventory(loopIndex).Price And Item.Type = Inventory(loopIndex).Type Then
				    itemFound = True
				Else
				    loopIndex += 1
				End If
			Loop
			If itemFound Then
				existingItemADD(loopIndex)
			Else
				newItem()
			End If
		End If
	End Sub
	Private Sub existingItemADD(ByVal index)
		Inventory(index).Qty = Item.Qty
	End Sub
	Private Sub newItem()
		ItemIndex += 1
		ReDim Preserve Inventory(ItemIndex)
		Inventory(ItemIndex) = Item
	End Sub
#End Region
 
Last edited:
Sorry. . here is the global definitions I am using


VB.NET:
#Region " Definitions "
	Private Item As New pet
	Private Inventory(0) As pet
	Private ItemIndex As Integer
	Dim Types() As String = {"Live Animal", "Frozen Good", "Dry Good", "Misc Good"}
#End Region

Item is the current item that is entered from the input boxes in the form
Inventory is the dynamic array I am working with
ItemIndex is just a counter for the amount of products in the array. . .pretty much the (array.length)
Types() is just the array for the combo box, simulating its coming from an flat file or database for now.
 
So what is happening is that every time you enter the "sub"
Private Sub newItem()
ItemIndex += 1
ReDim Preserve Inventory(ItemIndex)
Inventory(ItemIndex) = Item
End Sub
ItemIndex is equal to 0 try checking it like this
Private Sub newItem()
msgbox(ItemIndex)
ItemIndex += 1
ReDim Preserve Inventory(ItemIndex)
Inventory(ItemIndex) = Item
End Sub
see if that is the case!
 
Back
Top