C
C#16mo 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
HtmlCompiler
HtmlCompiler16mo ago
there is not enough code i guess also please paste text, images are not good $code
MODiX
MODiX16mo 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.
HtmlCompiler
HtmlCompiler16mo ago
in the first image technically there isn't a closing }, although it could be outside
MRW419
MRW419OP16mo ago
Got it.... Also what do you mean? Oh Yeah, it's outside it
Servator
Servator16mo ago
@mrw419 You need to override ToString() method in Fruits class
MRW419
MRW419OP16mo ago
What do you mean?
HtmlCompiler
HtmlCompiler16mo 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
wwww
wwww16mo ago
No description
wwww
wwww16mo 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);
wwww
wwww16mo 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
wwww
wwww16mo ago
and also here, i think we need to call constructor which have 0 arguments
No description
wwww
wwww16mo ago
like
Fruits Apple = new Fruits();
Fruits Apple = new Fruits();
or
Fruits Apple = new();
Fruits Apple = new();
MRW419
MRW419OP16mo ago
I'll definitely need to overlook this when I get back to school. Since at this moment I'm out eating.
Servator
Servator16mo ago
No description
MRW419
MRW419OP16mo ago
Some of those I never heard of yet such as override, internal class, {get; }
wwww
wwww16mo ago
class Furits = internal class Fruits, almost everything have access modifiers, for classes by default is internal
Accord
Accord16mo 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?