Accesing Constructor C#

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 342 Views - Last Post: 03 June 2019 - 11:26 AM Rate Topic: -----

#16 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15106
  • View blog
  • Posts: 60,394
  • Joined: 12-June 08

Re: Accesing Constructor C#

Posted 03 June 2019 - 11:03 AM

Quote

Yes that is true also that is my task but who cares, it is just different name but same function. Don't you think?

No, that is not your task. I am almost guarantee three twizzlers it. You are misreading or misinterpreting what they - the homework givers - are asking. You went down the wrong hole from the get go.

Quote

this makes me think of people that are too strict about fact that makes them forget the beauty in real life. For example if I talk to a girl who isn't very well at English I would get to like her more because that would make me curious about what she whants to say and makes my heart burst a little.

You see?

I don't get the relevance of this non-sequitur outside of trying to cover your obviously gaping large failing with the topic of inheritance in some wabi-sabi hand waving.

You are wrong in your approach. Back up, and do it right.
Was This Post Helpful? 0
  • +
  • -

#17 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Accesing Constructor C#

Posted 03 June 2019 - 11:08 AM

So you mean I should have only one class?
Was This Post Helpful? 0
  • +
  • -

#18 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15106
  • View blog
  • Posts: 60,394
  • Joined: 12-June 08

Re: Accesing Constructor C#

Posted 03 June 2019 - 11:13 AM

Where did you gather that? I gave examples of what would be acceptable inherited classes of type book.. and how your current menagerie is not that.
Was This Post Helpful? 0
  • +
  • -

#19 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Accesing Constructor C#

Posted 03 June 2019 - 11:22 AM

Man I didn't get that code, I can survive with C I don't need A
Was This Post Helpful? 0
  • +
  • -

#20 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15106
  • View blog
  • Posts: 60,394
  • Joined: 12-June 08

Re: Accesing Constructor C#

Posted 03 June 2019 - 11:25 AM

It is not that difficult of a concept. Just keep saying "is-a" or "has-a" and it tends to work itself out.
Was This Post Helpful? 0
  • +
  • -

#21 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6962
  • View blog
  • Posts: 23,666
  • Joined: 05-May 12

Re: Accesing Constructor C#

Posted 03 June 2019 - 11:26 AM

No. What he means is that you need to use the correct domain.

If you really want to insist collecting the title, author, and year for a book, then you'll want to setup your class hierarchy to be something like this:
abstract class BookAttribute { } /* intentionally empty. Ideally should be empty interface, not an empty class */
class Title : BookAttribute { ... }
class Author : BookAttribute { ... }
class Year : BookAttribute { ... }

class Book
{
    public List<BookAttribute> Attributes = new List<BookAttribute>(); 
}


and so for you to get the list of books which I assume is really the assignment, then you'll need to do something like the following pseudo code:
BookAttribute InputAttribute()
{
    Console.WriteLine("Enter type of attribute to add: ");
    switch(Console.ReadLine())
    {
    case "Title":
        get title string
        return new Title(title);
    case "Author":
        get author string
        return new Author(author);
    case "Year":
        get year integer
        return new Year(year);
    }
    return null;
}

Book InputBook()
{
    var book = new Book();
    do
    {
        attribute = InputAttribute();
        if (attribute != null)
            book.Attributes.Add(attribute);
    } while (attribute != null);
    return book.Attributes.Count > 0 ? book : null;
}

:

var bookList = new List<Book>();
do
{
    book = InputBook();
    if (book != null)
        bookList.Add(book);
} while (book != null):



Notice that:
Title IS-A BookAttribute,
Author IS-A BookAttribute,
Year IS-A BookAttribute, AND
A Book HAS-A List of BookAttributes.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2