GIDForums  

Go Back   GIDForums > Computer Programming Forums > .NET Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Oct-2005, 13:39
Kacyndra's Avatar
Kacyndra Kacyndra is offline
Member
 
Join Date: May 2005
Location: Maryland
Posts: 226
Kacyndra will become famous soon enough

Classes in C#


Hello,
i'm taking my very first C# class, and i think i need to help.

It's a very simple project. I'm supposed to get book information from a form and add it into a class array.

So far i have my Book and Library Classes.
this is my ADD Button.
C-SHARP / C# Code:
private void btnAdd_Click(object sender, EventArgs e)
        {
            string _author = txtAuthor.Text;
            double _price = double.Parse(txtPrice.Text);
            string _title = txtTitle.Text;
            bool _stock = chkStock.Checked;
            int Count = 0;
            Book.CoverType cover;
            if (rdHard.Checked)
            {
                cover = Book.CoverType.Hard;
            }
            else if (rdPaper.Checked)
            {
                cover = Book.CoverType.Paper;
            }
            else if (rdSoft.Checked)
            {
                cover = Book.CoverType.Soft;
            }
            else
            {
                DialogResult dr = MessageBox.Show("You Must pick the Cover Type", "ERROR", MessageBoxButtons.OK);
            }
            //error is here...
            Library[Count++] = new Book(_title, _author, _price, _stock, cover);

       }

the error that i'm getting is:
Error 1 'HW2.Library' is a 'type' but is used like a 'variable' C:\School\CMIS398\HW2\Main.cs 48 13 HW2

i don't understand what it means.. please help.


this thing is, our teacher gave us a working program to go by, and i though i did a pretty good job incorporating it into my own, but i keep getting this error.

here is the code he gave us for his ADD button:
C-SHARP / C# Code:
		private void addButton_Click(object sender, System.EventArgs e)
		{
			string name = nameBox.Text;
			string sAge  = ageBox.Text;
			int age = int.Parse(sAge);
			string title = titleBox.SelectedText;
			bool member = memberBox.Checked;
			Person.SexType sex;
			if (maleBox.Checked)
				sex = Person.SexType.Male;
			else
				sex = Person.SexType.Female;
			people[count++] = new Person(title,name, age, member, sex);
			nameBox.Text = "";
			ageBox.Text = "";
			titleBox.Text = "Mr.";
			memberBox.Checked = false;
			maleBox.Checked = true;
			femaleBox.Checked = false;
		}


EDIT: i'v decided to add my 2 classes here too, just in case. since i dont' really know what i'm doing yet, i might have done something wrong there too.

C-SHARP / C# Code:
class Book
    {
        private string isbm, title, author;
        private double price;
        private bool inStock, found;
        public enum CoverType
        {
            Hard, Soft, Paper
        }
        private CoverType Cover;


        public Book()
        //default constructor
        {
            title = "";
            author = "";
            price = 0.0;
            inStock = true;
        }

        public Book(string title, string author, double price, bool inStock, CoverType Cover)
        // Constructor which initializes the object
        {
            this.title = title;
            this.author = author;
            this.price = price;
            this.inStock = inStock;
            this.Cover = Cover;
        }

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

        public String Author
        {
            set
            {
                author = value;
            }
            get
            {
                return author;
            }
        }

        public double Price
        {
            set
            {
                price = value;
            }
            get
            {
                return price;

            }
        }

    }


LIBRARY

C-SHARP / C# Code:

class Library
    {

        private Book[] MyBook;
        private int lastISBN = 0;
		private int booksStored;

		/// 
		/// Constructor.  The size is the maximum number of books to be
		/// stored in the library.
		/// 
		/// 
		public Library(int size)
		{
			MyBook = new Book[size];
			booksStored = 0;
		}

		/// 
		/// This is the default constructor that sets the size to
		/// 10 items.
		/// 
		public Library() : this(10)
		{
		}

		/// 
		/// This method removes all items from the Library.
		/// 
		public void emptyLibrary()
		{
			booksStored = 0;
		}

		/// 
		/// This is the indexer that returns a book from the given
		/// array position.
		/// 
		public Book this [int index]
		{
            get
            {
              return MyBook[index];
            }
		}

		/// 
		/// This is the indexer which returns a book that matches
		/// on the isbn.
		/// 
		public Book this[string bookAuthor]
		{
            get
            {
                for (int i = 0; i < lastISBN; i++)
                {
                    //if (Library[i].author.equals(bookAuthor))
                    //{
                       // return people[i];
                   // }
                }
                return null;
            }
       
       
           
	    }

		/// 
		/// This method adds a book to the library.  Make sure that
		/// you return false if you cannot add the book.  The reasons
		/// that you cannot add the book are:
		/// 	1 - The book already exists.
		///		2 - The array is already full.
		/// 
		/// 
		/// 
		public bool Add(Book book)
		{
            MyBook[booksStored++] = book;
            return true;
		}

		/// 
		/// This method deletes a book from the library.  Make sure you return
		/// false if the book does not exist and is not deleted.
		/// 
		/// 
		/// 
		public bool Delete(string isbn)
		{
            return true;

		}

    }
__________________
Xrum!
  #2  
Old 07-Oct-2005, 01:14
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 893
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Classes in C#


Hi, Kacyndra.

Just like in C++, classes need to be instantiated in order to be used - you must create an object.
So, in order to use the Library class, you have to do something like:

C-SHARP / C# Code:
Library _library = new Library(20); // for a 20 size - see the constructor

Now, I see the intent of the classes you shown is not really to have an array of Libraries, but rather one container Library class holding all the books (you can of course create more Libraries if need be).
So, why not use the Add member function of the Library class to add Books to it?
Remember we instantiated the Library to hold up to 20 books, so instead of:

C-SHARP / C# Code:
//error is here...
Library[Count++] = new Book(_title, _author, _price, _stock, cover)

do

C-SHARP / C# Code:
Book newBook = new Book(_title, _author, _price, _stock, cover);
_library.Add(newBook);

I have not tested the code, but this is the logic that most appeals to me. If you can't get it to work, I'll further look into it.

Kind regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 07-Oct-2005, 01:22
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 893
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Classes in C#


Quote:
Originally Posted by Kacyndra
C-SHARP / C# Code:
class Library
    {
		/// 
		/// This is the indexer that returns a book from the given
		/// array position.
		/// 
		public Book this [int index]
		{
            get
            {
              return MyBook[index];
            }
		}

		/// 
		/// This is the indexer which returns a book that matches
		/// on the isbn.
		/// 
		public Book this[string bookAuthor]
		{
            get
            {
                for (int i = 0; i < lastISBN; i++)
                {
                    //if (Library[i].author.equals(bookAuthor))
                    //{
                       // return people[i];
                   // }
                }
                return null;
            }
       
       
           
	    }
	
    }

Also, I see you have an indexer for returning books:

C-SHARP / C# Code:
Book firstBook =  _library[0];

But you do not have one for adding books, because there is no set component - only get. If you will add one (see my little tutorial for reference), you will be able to use the indexer instead of the Load method:

C-SHARP / C# Code:
Book newBook = new Book(_title, _author, _price, _stock, cover);
 _library[5] = newBook; // set the 6th item to newBook

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Assistance with classes... Bravebird C++ Forum 7 27-Apr-2005 13:17
bug w/ templated classes f15e MS Visual C++ / MFC Forum 4 06-Mar-2005 23:56
Fairly simple classes help please sammacs C++ Forum 0 30-Nov-2004 09:58
First time using classes crystalattice C++ Forum 6 13-Oct-2004 08:21
using windows WMI and CMI classes in Dev-c++ Dawis C++ Forum 0 26-Oct-2003 04:18

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:55.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.