❔ Using inheritance in my project

I'm building a book shelf project and I need help with modifying my ctor so I could assign each input for one of the subclasses or the books https://gist.github.com/Programmer-Faraj/9a2f90285e21996c4cf7577505ce6853
Gist
MagazineBook.cs
GitHub Gist: instantly share code, notes, and snippets.
71 Replies
JakenVeina
JakenVeina2y ago
what's the question?
The king of kings
After taking both user inputs for the title & author, I would need to use the public string Type; field to assign one of the se books objects from my sub-classes
JakenVeina
JakenVeina2y ago
what's an "se book"? also, what public string Type; field also, why would that be a field?
The king of kings
sorry, I meant these not the se.
JakenVeina
JakenVeina2y ago
ah so, you've got some UI pulling Title and Author values from the user and then you need to create some Book instance for these
The king of kings
In my base class you can see that I have defined a field
JakenVeina
JakenVeina2y ago
first point: don't make public fields make public properties and for something like this
The king of kings
Kind of I'm not using properties at this point yet
JakenVeina
JakenVeina2y ago
public class Book
{
public required string Title { get; init; }
public required string Author { get; init; }
public required string Type { get; init; }
}
public class Book
{
public required string Title { get; init; }
public required string Author { get; init; }
public required string Type { get; init; }
}
time to start and BookType is redundant when the class is already named Book, make it Type
The king of kings
Ok! But in my assignment they haven't mentioned it Look
JakenVeina
JakenVeina2y ago
ah, okay if you're following an assignment, then yeah, stick to those constraints just be aware, that's shitty code don't do that in any real scenario (public fields)
JakenVeina
JakenVeina2y ago
okay, so it states RIGHT there that the Book class should have "properties"
The king of kings
Really, why my friend?
JakenVeina
JakenVeina2y ago
because it allows classes better control over their state the most specific example.... if your book class exposes a Title field directly and then wants to change the behavior of that field, in the future swapping that field out for a property is a breaking change if it's already a property, adding logic to a getter that already exists would not (necessarily) be a breaking change beyond that, it's just general practice
The king of kings
Many say the same, that those should be private access modifiers and not public, is it because of the accessibility level where whoever can come and change the values from the outside of the class and when they're private, basically no one is able to access & change them, right?
JakenVeina
JakenVeina2y ago
to the point of interoperability with other systems like serializers virtually all serializers will serialize your class's properties and ignore fields that's just the convention of what data models should be
The king of kings
What, I didn't know that 🙆🏻‍♂️
JakenVeina
JakenVeina2y ago
If we're JUST talking about the difference between properties and fields, then no, fields can be readonly just as much as properties can it's a semantic convention, one which costs you nothing to implement so let's start with the Book class per the requirements, it needs to have 3 properties, and a constructor that sets them write that out for me, please
The king of kings
Wow, I'm just amazed on the level of knowledge that you're capable of currently. That's amazing bro. What do you mean by breaking change, I didn't quite understand this btw?
JakenVeina
JakenVeina2y ago
a breaking change is with regard to version control and publishing code
The king of kings
Bingo
JakenVeina
JakenVeina2y ago
bingo indeed well, mostly A) Title should be public B) all of those should not be setable C) Type still makes more sense than BookType
The king of kings
I should read about it, because I'm not sure what version control means in a coding sense.
JakenVeina
JakenVeina2y ago
indeed you should
The king of kings
Aha! Ok! Thanks a lot for teaching me these concpts. Why title should be a public property and not private same thing with the Author? Sorry for asking too many questions, I just want to learn using this thread 😃
JakenVeina
JakenVeina2y ago
per the assignment requirements
Ability to print a complete list of all entered books
presumably, this should mean printing all the information ABOUT each book ergo, it needs to be accessible now
The king of kings
Yes, this is what they want me to implement pretty much.
JakenVeina
JakenVeina2y ago
technically, you could probably get away with keeping them as private fields, given that you're overriding ToString() but that's a poor way to expose information in code it limits your consumer per the "Open-Closed Principle" of Object-Oriented Programming:
Entites should be open for extension, but closed for modification
and this really just plays into that make yourself as freely-usable as possible, while maintaining consistency so instead of forcing the consumers of the Book class to only be able to "print" the book info one way, expose the info as granularly as possible, to allow consumers to piece it together however they like but restrict the ways they can create and modify book info I.E. your requirements don't specify ANY need to make books mutable (I.E. changeable) so, don't give them any and that plays into the book type propertiy don't allow consumers to set that to anything if your data model says "only three types of books exist" set things up so that only those three values can ever be assigned that's where the subclassing will play in
The king of kings
Ok! I have reviewed all of your content now. Although this is a bit advanced topic for my level, but still very worth to learn all these. Man I wish if I could've call you, otherwise we could continue to write here. Now, I see why you didn't use any set in the properties, because basically we are the one who set the book type for the consumer. Are you available?
JakenVeina
JakenVeina2y ago
I say not to use setters because you don't need to the requirements of your problem don't specify that books need to be editable after they've been created that's what a setter does
The king of kings
I noticed you're replaying to many threads, man you're awesome at helping other people and solving their code issues 😀 Aha! Ok! You're totally right.
The king of kings
This how it looks currently
JakenVeina
JakenVeina2y ago
sweet now subclassing requirements state A) you need at least 3 B) you should leverage the constructor to set the type
The king of kings
Now, I have issues with the subclasses where they ask for passing a full arguments based on the params my friend 🤷🏻‍♂️
JakenVeina
JakenVeina2y ago
k what are those errors?
JakenVeina
JakenVeina2y ago
I don't need screenshots, just read them or post the text so, what does that error tell you
The king of kings
I have created these three subclasses if you look at my gist 😉 https://gist.github.com/Programmer-Faraj/9a2f90285e21996c4cf7577505ce6853
Gist
MagazineBook.cs
GitHub Gist: instantly share code, notes, and snippets.
JakenVeina
JakenVeina2y ago
would if I could well, okay, that one seems to work the earlier one didn't regardless, that's not really the point I don't care what your other subclasses are I'm asking about this one what does that error tell you?
The king of kings
Ok! Well! The first error says you need to pass an argument for the string type parameter
public Book(string title, string author, string type)
{
Title = title;
Author = author;
Type = type;
}
public Book(string title, string author, string type)
{
Title = title;
Author = author;
Type = type;
}
JakenVeina
JakenVeina2y ago
right cause that's how you defined the Book constructor that you're calling
The king of kings
I'm passing three params in the base class, so I need to pass three arguments in the sub-classes too
JakenVeina
JakenVeina2y ago
it requires a type parameter so give it
so I need to pass three arguments in the sub-classes too
nope not necessarily the only thing you need to pass 3 arguments to is the thing that you've defined to take 3 arguments the Book constructor
The king of kings
see these are things very tricky to learn for beginners and only people like you knows the solution Aha
JakenVeina
JakenVeina2y ago
yes and no you just need to learn how to think about your problems
The king of kings
You mean only when creating object of a class then we need to pass three arguments?
JakenVeina
JakenVeina2y ago
once you learn how to properly model your problems, the solutions are self-explanatory I mean what I said
the only thing you need to pass 3 arguments to is the thing that you've defined to take 3 arguments the Book constructor
The king of kings
That's a good one My friend, I work at a school and I need to go out, I will catch up withy you when I get my lunch hour 😃 thank you sooo much for your unlimited help in teaching me 🤗
JakenVeina
JakenVeina2y ago
sure thing
The king of kings
Alright. But what would be the thing that takes 3 arguments my friend? I'm new to inheritance and still at the surface and haven't got deeper yet.
JakenVeina
JakenVeina2y ago
the Book constructor, as I've said
The king of kings
Ok! So is that mean I shouldn't pass three arguments to the sub-classes or what? Wait a minute, I've noticed something. base class (Book)'s ctor is already handling title and author, so I don't need to do it again over here in the sub-classes 💁🏻‍♂️ I mean logically, the sub-classes are for only the public string Type; why would I pass the other properties for inheritance, right?
JakenVeina
JakenVeina2y ago
uhhhhhh no the opposite of what you said
The king of kings
Sorry, I said wrongly yesterday until I learned from a member in the server that I would need to pass the same parameters except for the string type
MODiX
MODiX2y ago
Klarth#1705
Because in your initial example, you didn't supply the third parameter. You used a hardcoded string, so there's no need for the caller to pass a parameter.
Quoted by
<@!973964186956726313> from #help-0 (click here)
React with ❌ to remove this embed.
JakenVeina
JakenVeina2y ago
so, where we at with this?
The king of kings
Great to see again I just modified my sub-classes
public Roman(string title, string author) : base(title, author, "(Roman)")
{

}
public Roman(string title, string author) : base(title, author, "(Roman)")
{

}
Yesterday, I was getting two different errors, one for not passing a param for the string type in the base ctor and the other one for being readonly, then I learned those errors from Klarth
JakenVeina
JakenVeina2y ago
beautiful
The king of kings
Yep Although, this seems a bit weird to me. What should I do inside the curly brackets? I mean we usually initialize parameters inside these brackets, but in my case, I'm passing "(Roman)" in the ctor directly. What do you think? Are you available? Are you sleeping or something? 😁
JakenVeina
JakenVeina2y ago
working
What should I do inside the curly brackets?
nothing you said it yourself the one thing the subclass needs to do in the constructor, you're already doing
The king of kings
Nice to see you again It's seems to me that you're from US that's why you show up when it's evening over here in Sweden. Different timezones. Aha! Are you working as a software developer or sort of? Ok! Awesome. I guess this is the implementation that is required for the assignment so it shouldn't go beyond it's limitation. . Man, previously you mentioned very interesting tips here and I really really would like to learn how to do that. I wish if I could call you and get some more tips, like even a half hour whenever you're available!
JakenVeina
JakenVeina2y ago
ask question, receive answer
The king of kings
Aha! So this is all about learning how to model problems?
JakenVeina
JakenVeina2y ago
that's my take, yeah that's a big part of software development, and C# in general, I think
The king of kings
Ok! I understand that. I think asking a specific question for a specific problem is very worth it instead of looking at the screen for hours/days, since a concept in c# can be used/implemented in many different ways depending on the problem. Are you working as a software developer?
JakenVeina
JakenVeina2y ago
yes
The king of kings
Wow, awesome. What are the lest requirements to be a junior developer?
JakenVeina
JakenVeina2y ago
uhh I dunno depends who you ask
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server