7 Replies - 8423 Views - Last Post: 23 October 2011 - 08:12 AM Rate Topic: -----

#1 fsavage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 11

Learning to use arrays of objects

Posted 20 October 2011 - 05:13 PM

Hi all, I am trying to learn to create an array of objects, but I keep running into errors and am hoping someone can explain what I am doing wrong in my code.

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Learning to use arrays of objects

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Learning to use arrays of objects

Posted 20 October 2011 - 05:24 PM

What if you remove the whole line, because you have said above that it will be of size 19. I mean the whole line in constructor ;)
Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1052
  • View blog
  • Posts: 4,077
  • Joined: 02-July 08

Re: Learning to use arrays of objects

Posted 20 October 2011 - 05:30 PM

It's time to get away from simple arrays. You should be using a List(Of CD)
Then you don't have to declare the size and ReDim the size later. You can simply .Add and .Remove CD's as needed. You won't need the contructor just initialize the List.

Public Class CD_Store
   Public stock As New List(Of CD)
....

Was This Post Helpful? 1
  • +
  • -

#4 fsavage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 11

Re: Learning to use arrays of objects

Posted 21 October 2011 - 07:55 PM

Thank you _Hawk_, I agree as I have been reading a lot about using lists and they do seem like a better option. i have emailed my instructor to see if we can use a list instead. In the mean time, I have been reading up on using lists and have been playing with my code to create the List(Of CD) as such:
 Private _stock As New List(Of CD)

    Private Sub CreateCD(ByVal i As Integer)
        Stock.Add(New CD("Blue Print Album",16.99,"Jay-Z","Rap", 5))
        Stock.Add(New CD("Play On", 16.99, "Carrie Underwood", "Country", 10))
        Stock.Add(New CD("Here for A Good Time", 12.99, "George Strait", "Country", 2))
        Stock.Add(New CD("Drake", 14.99, "Drake", "Rap", 1))

    End Sub


Now I am attempting to access this list from another page, and pass it to a method. So in here is the first part of my other page:

Partial Class Inventory
    Inherits System.Web.UI.Page
    Dim store As New CD_Store

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblCount.Text = store.TotalCount
        CreateTable(store.Stock)

    End Sub

    Private Sub CreateTable(ByVal item As List(Of CD))
        ' Create a TableItemStyle object that can be
        ' set as the default style for all cells
        ' in the table.


I do not get an error, but the line CreateTable(store.Stock) does not see any child elements. So far all of the examples I have found show using the list with in the same class it was created in, but not how to use it outside of the class. Do you know of any examples or tutorials that show this?

I may still have to figure out the array issue, if my instructor requires it. So any pointers on my error in my original code will be helpful.

thank you
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1052
  • View blog
  • Posts: 4,077
  • Joined: 02-July 08

Re: Learning to use arrays of objects

Posted 21 October 2011 - 08:29 PM

Well, the access modifier:

Private Sub CreateTable(ByVal item As List(Of CD))

May need to be,

Public Sub CreateTable(ByVal item As List(Of CD))

I am not sure how this interacts on a web app though.

This post has been edited by _HAWK_: 21 October 2011 - 08:29 PM

Was This Post Helpful? 0
  • +
  • -

#6 fsavage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 11

Re: Learning to use arrays of objects

Posted 22 October 2011 - 11:00 AM

So I am back to using the array, as that is what we are being required to use for this assignment. I have tried the following modification:

    Public stock(19) As CD


    'constructor
    Public Sub New()
        'I added this loop to instantiate the stock() array
        For n As Integer = 0 To 19
            stock(n) = New CD()
        Next
    End Sub


I no longer get the any of the errors, but it returns an empty array even after calling my create CD method:

   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

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,450
  • Joined: 29-May 08

Re: Learning to use arrays of objects

Posted 22 October 2011 - 11:27 AM

Quote

So I am back to using the array, as that is what we are being required to use for this assignment.

Thank sucks big time and I think in my opinion, that curriculum needs updating today. The World has moved on past Arrays, to Generic Collections.

They want an then let's give them an Array.
Dim Stock() As CD = {
  New CD(Title:= "Blue Print Album", Price:= 16.99,Artist:= "Jay-Z", Genre:= "Rap", StockCount:= 5)),
  New CD("Play On", 16.99, "Carrie Underwood", "Country", 10)),
  New CD("Here for A Good Time", 12.99, "George Strait", "Country", 2)),
  New CD("Drake", 14.99, "Drake", "Rap", 1))
}


This utilises vb.net's Array Literal syntax and Named Parameters.
If Option Infer On you can get rid of the As CD, as the compiler can infer the type of the array.
Was This Post Helpful? 0
  • +
  • -

#8 fsavage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 11

Re: Learning to use arrays of objects

Posted 23 October 2011 - 08:12 AM

Thank you Adam, I finally got the array to work. I found a tutorial to help with figuring out how to access that array from outside of the class and I am currently doing the following:

I have built my CreateCD as a function to return the array for use elsewhere:
    Public Shared Function CreateCD() As CD()
        Dim stock(19) As CD
        stock(0) = New CD(Title:="Blue Print Album", Price:=16.99, Artist:="Jay-Z", Category:="Rap", Count:=5)
        stock(1) = New CD("Play On", 16.99, "Carrie Underwood", "Country", 10)
        stock(2) = New CD("Here for A Good Time", 12.99, "George Strait", "Country", 2)
        stock(3) = New CD("Drake", 14.99, "Drake", "Rap", 1)
        Return stock
    End Function


...and then I call it in another procedure:
Partial Class PurchaseMain
    Inherits System.Web.UI.Page

    Dim stock() As CD = CD_Store.CreateCD   '//new instance of stock array

    'lists available cd's
    Private Sub LoadList()
        For Each item As CD In stock
            If item.Count > 0 Then
                rblAvailCD.Items.Add(item.Title)
                n = 0   '//initialize class variable to count # of selections
            End If
        Next
    End Sub


This works, but I am concerned that by accessing the array variable this way

Dim stock() As CD = CD_Store.CreateCD 


I am actually calling the CreateCD function to process each time, am I correct? If so how would I access the array from outside of the class with out calling that function each time? I ask because I will be changing the available quantities as purchases are made, and I do not want the CreateCD function to set those quantities back to the original count.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1