Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Please Help!


  • Please log in to reply

#1
Princss_Ashes

Princss_Ashes

    New Member

  • Member
  • Pip
  • 1 posts
I am so confused on this. I need to write an indexer returning bool that will search for the book collection for a specific title and return true if the title is in the collection; otherwise return false, and I have to use the foreach statement to loop/iterate thru the collection and ingnore any books that have a null reference, and test this functionality in the Main routine. Here are my files (this is all in C#). Sorry I am so blonde ...... :whistling:

Book.cs

using System;

namespace Book
{
public class Book
{
private string title ;

public string Title
{
get
{
return title ;
}
set
{
title = value ;
}
}

public Book()
{
title = "Unknown title" ;
}
}
}

BookMain.cs

using System;

namespace Book
{
public class BookMain
{
public BookMain()
{
}

public static void Main (string [] args)
{
Book myBooks = new Book() ;
Console.WriteLine ("Created " + myBooks) ;

// Create several books
Book [] manyBooks = new Book [5];

// Create the individual books. It is an error not to
// create each reference in the array.
for (int i = 0; i < manyBooks.Length; i++)
{
manyBooks [i] = new Book() ;
}

manyBooks[0].Title = "Tom Sawyer" ;
manyBooks[1].Title = "Going to Town" ;
manyBooks[2].Title = "Intro to C#" ;

// list the books
for (int i = 0; i < manyBooks.Length; i++)
{
Console.WriteLine ("Book {0} title: {1}", i + 1,
manyBooks [i].Title) ;
}

Console.Write ("\n\n") ;

BookColl moreBooksColl = new BookColl (10);
moreBooksColl [0] = "The best book";
moreBooksColl [3] = "Science Fiction";

moreBooksColl.display ("More Books Collection");
}
}
}

BookColl.cs

sing System;

namespace Book
{
public class BookColl
{
private Book [] books ;

public BookColl()
{
}

public string this [int index]
{
// Make sure the index is within range
get
{
CheckNull (index);
if (index >= books.Length || index < 0)
{
return "Unknown book number" ;
}
return books[index].Title ;
}
set
{
CheckNull (index);
if (index >= books.Length || index < 0)
{
return ; // out of range, can't set
}
books [index].Title = value ;
}
}

public BookColl (int size)
{
books = new Book [size];
}

private void CheckNull (int index)
{
if (books [index] == null)
{
books [index] = new Book();
}
}

public void display (string title)
{
Console.WriteLine ("\n========= {0} ========\n\n", title);
foreach (Book bk in books)
{
if (bk == null)
{
continue;
}

Console.WriteLine("title {0} active", bk.Title);
}

Console.WriteLine ("\n========= End {0} ========\n\n", title);
}
}
}
  • 0

Advertisements


#2
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
Hi Princss_Ashes,
A request, next time can you please intend the code and put in a code-box. It makes it easier for us to read.

From you code:
public string this [int index]
{
// Make sure the index is within range
   get
   {
	  CheckNull (index);
	   if (index >= books.Length || index < 0)
	   {
		  return "Unknown book number";
	   }
	  return books[index].Title;
	}
   set
   {
	 CheckNull (index);
	 if (index >= books.Length || index < 0)
	 {
		return; // out of range, can't set
	 }
	 books [index].Title = value;
   }
}

I thought you wanted an indexer that searched by book-title and returned a bool. The declartion of you indexer is wrong then.
It should be:
public bool this [string title]

Here, I gave you a start, now you can try the rest of the code. If you want, I'll code the rest of it as well.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP