C
C#2y ago
MRW419

❔ I need to make sure I understand and doing this correctly

I'm learning coding in class and this video explain classes and objects. I tried my best to follow what the instruction want while making sure I understand everything. I only got one error which I think I know where I messed up but I want to make sure everything else is correct.
No description
No description
No description
No description
17 Replies
many things
many things2y ago
there is not enough code i guess also please paste text, images are not good $code
MODiX
MODiX2y ago
Posting Code Snippets To post a code snippet type the following: ```cs // code here ``` Notes: - Get an example by typing $codegif in the chat. - Change the language by replacing cs with the language of your choice (for example sql or cpp). - If your code is too long, you can post it to https://paste.mod.gg/ and share the link.
many things
many things2y ago
in the first image technically there isn't a closing }, although it could be outside
MRW419
MRW419OP2y ago
Got it.... Also what do you mean? Oh Yeah, it's outside it
DeliBey
DeliBey2y ago
@mrw419 You need to override ToString() method in Fruits class
MRW419
MRW419OP2y ago
What do you mean?
many things
many things2y ago
well probably first you need to correct the errors why are errors not underlined in ToString you have "fruit 1: " + Apple; which is not a call nor an assignment, so it's a mistake
xdd
xdd2y ago
No description
xdd
xdd2y ago
i thought that was error, but it can work like this also works this way too
Person person = new Person("David", new DateTime(2001, 09, 21));
string pepsi = $"Person :" + person;
Console.WriteLine(pepsi);
Person person = new Person("David", new DateTime(2001, 09, 21));
string pepsi = $"Person :" + person;
Console.WriteLine(pepsi);
xdd
xdd2y ago
@mrw419 So, every class in C# is sub-class of Object class. object have some methods inside it, like ToString. what inheritance means, everything not private from based class u also have in sub-class. like on thing i painted, so, that means in your Fruits class u have ToString Method (this method is virtual, that means it can be overridden), so, that ToString Method works different way in different environment. as i get, all u need to do, thats override this method in your Fruits class.
public override string ToString()
{
// code
}
public override string ToString()
{
// code
}
so that will be behavior of instance of Fruits class when it used like string. i hope i explained it correctly and that was main problem here
No description
No description
xdd
xdd2y ago
and also here, i think we need to call constructor which have 0 arguments
No description
xdd
xdd2y ago
like
Fruits Apple = new Fruits();
Fruits Apple = new Fruits();
or
Fruits Apple = new();
Fruits Apple = new();
MRW419
MRW419OP2y ago
I'll definitely need to overlook this when I get back to school. Since at this moment I'm out eating.
DeliBey
DeliBey2y ago
No description
MRW419
MRW419OP2y ago
Some of those I never heard of yet such as override, internal class, {get; }
xdd
xdd2y ago
class Furits = internal class Fruits, almost everything have access modifiers, for classes by default is internal
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.

Did you find this page helpful?