Ok..I am asking a similar sort of question...

If I have a Form...I then take the values of of textboxes(user entered) and pass them in to the constructor of the class and set the attributes to the values passed in to the constructor.

I know I can create objects myself such as myob1,myob2,myob3 etc and store different values in those objects.

What I want to know is..can I make an instance of the class and call the object the name of the title that I pass in to the constructor?

I do not think that you can do this..

So therefore if I wanted to make new objects every time different values are passed in to the constructor..how would I achieve this?

Would you have to pass values in...store the values in a text file say...then pass new values in and overwrite them then store those values and so on?

*EDIT*

I dont think I worded that very well....

At run time I want the user to be able to enter say the name of a book *Alice in wonderland*

This would then create an object called *Alice in wonderland*...

this would happen for each book...

Once this has been done and each title object contains other values (ISDN,Price etc) If you then wanted to be able to cycle through each object looking for the name of the book you want, and pull up the information about that book?

Someone asked a this question the other day, but wanted to catalog DVDs rather than books. No, you can't name the object after the book title, but you can create an Dictionary<TKey, TValue) and use the title of the book as the key.

could you then cycle through the dictionary with a foreach loop?

Yes, it implements IEnumerable (which is the requirement for using foreach).

Ok, thanks, I think I am reading too much in to it, I am thinking that when the user enters the name of a book, it should create an object with that name, this is not possible? You will have to put those details that the user enters in to a list or Dictionary and then retrieve the values from the List or Dictionary (without having to delve in to databases etc).

Right?

It is possible to create an object with the name the user enters, but it requires complex emit code (where you actually generate the IL code that the compiler normally handles for you). You probably don't want to do this.

Normally you'd create an object with various attributes/methods, one of which would be the title. You'd then add the object to a List<> or Dictionary<> (Dictionary makes lookup easy).

Thanks! This is what I thought...you would just create one object..but my lecturer was saying that you create a whole number of objects, which I could not understand.

But the reality is that you create one object and send values to that object, saving each different set of values as you go!

Thanks, understand now!

Is it most likely that if you were building a program for a book shop, you would store the values in a database you can store many different values for each book?

Yes, normally you'd store the data somewhere so you could retrieve it next time your program runs. For small data sets, an text file, xml file, etc. is fine. But once you get into large data sets (or multiple table situations) it is a lot easier to store stuff in a database.

As for the multiple objects, you'd create one object for each book. Maybe that is what he's talking about.

Hmmm, stumped!

It seems like this is going against what I have learnt to now....you say multiple objects...do you mean one object would be the set of values in a list or database?

If not can you advise anywhere I can read about multiple objects..?

Thanks
James

Let's create a book class for an example:

class Book {
    public String Title {get; private set;}
    public String Author {get; private set;} // just one author, for simplicity
    public int Pages {get; private set;}

    public Book(String title, String author, int pages) {
        Title = title;
        Author = author;
        Pages = pages;
    }
}

Now some code to create a dictionary and add some books:

Dictionary<String, Book> myBooks = new Dictionary<String, Book>();

Book bookData = new Book("C# 4.0 In A Nutshell", "Joseph Albahari", 1056); // Object 1
myBooks.Add(bookData.Title, bookData);

bookData = new Book("Pro LINQ Language Integrated Query in C# 2010", "Adam Freeman", 843);  // Object 2
myBooks.Add(bookData.Title, bookData);

bookData = new Book("Expert F# 2.0", "Don Syme", 625);
myBooks.Add(bookData.Title, bookData); // Object 3

We've create multiple book objects and stored them in the dictionary (this is what I believe your instructor means about multiple objects).

We can now iterate through all the books if we want:

foreach (Book b in myBooks.Values) {
    Console.WriteLine("{0}", b.Title);
}

Or we can get one book by title:

Book b = myBook["Expert F# 2.0"];

Or we can get one book by index:

Book b = myBook[1];

Thanks! You should be a lecturer yourself Momerath!

So in a way I was right in thinking that you still only use the one object (although everything is an object) and change it's internal values then save them as you go along.

Thanks Again!
James

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.