To start, I have the following class:
Public Class CD
Private _name As String
Private _price As Double
Private _artist As String
Private _category As String
Private _count As Integer
'Dim i As Integer
Public Sub New()
_name = ""
_price = 0
_artist = ""
_category = ""
_count = 0
End Sub
Public Property Title As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
[color="#00FF00"]
'remaining class properties listed in same format as one above[/color]
Then I have a CD_Store class where I am trying to create an array of the class objects above:
Public Class CD_Store
Public stock(19) As CD
[color="#00FF00"]
'constructor[/color]
Public Sub New()
stock = New CD() {}
End Sub
[color="#00FF00"]
'initialize stock array[/color]
Public Sub StoreOpen()
For i As Integer = 0 To 19 Step 1
CreateCD(i)
PopulateStock(i)
Next
End Sub
Private Sub CreateCD(ByVal i As Integer)
If i = 0 Then
stock(i).Title = "Blue Print Album"
stock(i).Price = 16.99
stock(i).Artist = "Jay-Z"
stock(i).Category = "Rap"
stock(i).Count = 5
ElseIf i = 1 Then
stock(i).Title = "Play On"
stock(i).Price = 16.99
stock(i).Artist = "Carrie Underwood"
stock(i).Category = "Country"
stock(i).Count = 10
ElseIf i = 2 Then
stock(i).Title = "Here For A Good Time"
stock(i).Price = 12.99
stock(i).Artist = "George Strait"
stock(i).Category = "Country"
stock(i).Count = 2
[color="#00FF00"]'There are actually 20 total statements as shown to add 20 sets of elements to the array[/color]
End If
End Sub
Private Sub PopulateStock(ByVal i As Integer)
stock(i) = New CD()
End Sub
When I run it, I get an "Index out of Range" error on the first line of the CreateCD() procedure:
stock(i).Title = "Blue Print Album"
I have also tried different variations of the above code such as:
Public Sub New()
stock = New CD() [color="#00FF00"]'I removed the curly brackets[/color]
End Sub
Which produces the error: Value of type 'CD' cannot be converted to '1-dimensional array of CD'.
I tried this:
Public Class CD_Store
Public stock() As CD
'constructor
Public Sub New()
stock(19) = New CD() {}
End Sub
Which produced the error: Value of type '1-dimensional array of CD' cannot be converted to 'CD'
Any advice is appreciated.

New Topic/Question
Reply



MultiQuote





|