Writing classes help

Posted on 2005-04-26
Last Modified: 2010-05-18
Here is the entire source code for your console application:

Sub Main()
      'Create a new book
      Dim mybook as New Book()

      'Create a new page and set the new page's .SourceDoc
      'to the argument supplied in .Add()
      'Display the .SourceDoc of the newly-added page
      Console.WriteLine(mybook.Pages(0).SourceDoc & " = c:\copyright.txt")

      'Display the read-only number of pages that have been added using Pages.Add()
      Console.WriteLine("Number of pages = " & mybook.Pages.Count & " = 1")

      'Delete the added page
End Sub

Now write the source for Book class that allows this console application to work. The first Console.WriteLine() should display "c:\copyright.txt = c:\copyright.txt". The second line displayed by Console.WriteLine() should display "Number of pages = 1 = 1".
Question by:ineedyourhelp
    LVL 44

    Expert Comment

    this looks very suspiciously like a homework assignment, and EE experts are NOT allowed to write answers to homework (see the EE Member guidelines)

    What have you already tried?  We can ASSIST you in figuring this out (it is VERY elementary, and you should not need to ask us how to do this, if you have been paying attention in class).


    Author Comment

    I thought the easiest way would to show it in an assignment-like format. You are not doing homework. And as a side note, I'm really tired of hearing people in forums like this and in chat rooms making the assumption--erroneous, in this case--that the helpers are doing someone else's homework.

    Author Comment

    If you would rather have me try to explain what I'm trying to do instead of asking in the prescribed way above, here, I hope this appeases you:

    I have a class and need to add sub procedures to properties in the class. In the class, I want to be able to add new elements to an array in a manner that is much like adding elements to a ListView. In a ListView, there is a collection of Items, and each item has an .Add() and .Remove() method. That's what I'd like my class to do too--follow the VB.NET scheme of adding items.

    Currently, my class has a public Property named FileName that is an array, and to add or remove items from the array, I have to use ReDim Preserve to resize the FileName of the project to a larger size, then set the new element in the array equal to the new FileName I want to add.

    Alright, there's my attempt. If that doesn't make much sense, as I predicted it wont, then you may now understand why I wrote the question in the aforeposted format.
    LVL 8

    Accepted Solution


    I would suggest that you need a collection, rather than an array.

    A collection exposes a count property and add function by default.

    In my opinion you need 3 classes:

    Page Class
    -- SouceDoc property (string)
    -- New constructor takes in SourceDoc as parameter, and sets the property
    -- Sub New (pSourceDoc)
    -- me.SourceDoc=pSourceDoc
    -- End Sub

    PageCollection class
    -- Inherits CollectionBase
    -- Sub Add (ByVal pPage as Page)
    -- Me.list.add(pPage)
    -- End Sub

    -- Sub Add(ByVal pSourceDoc as string)
    -- Me.List.Add(New Page(pSourceDoc))
    -- End Sub

    Book class
    -- Pages property
    -- Public Property Pages as PageCollection
    -- Set .....
    -- Get .....

    Hope to help,


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now