Why the compiler is giving me an error of Age ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why the compiler is giving me an error of Age ?

Base Class using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Assignment_3_Csharp { internal class Person { private int age; public virtual int SETAGE { get { return age; } } } } Another Class ng System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Assignment_3_Csharp { internal class STUDENTANDTEACHERTEST { static void Main(string[] args) { Person person = new Person(); Console.WriteLine("Hello"); Student student = new Student(); student.Age = 21; Console.WriteLine("Hello , my age is : {0} "); } } }

19th Jun 2022, 8:19 AM
Mustafa Azzoz
Mustafa Azzoz - avatar
5 Answers
+ 1
your age property is not well defined there is different ways to do this: This is quite common and usually enough class Person { public int Age {get; set;} } use this if the age variable does more actions when it's being set. This is where you would do validation. class Person { private int _age; public int Age { get { return _age; } set { _age = value; } }
19th Jun 2022, 10:23 AM
Apollo-Roboto
Apollo-Roboto - avatar
+ 1
oh yeah, but I have to be honest, even after my many years of experience I'm still googling the basics! There is so much to learn and so much concepts to understand. it's impossible to know it all. If syntax is difficult for you, I'd recommend you to explore other languages after learning the basics. Knowing more languages helps a lot to break through this syntax barrier.
19th Jun 2022, 10:45 AM
Apollo-Roboto
Apollo-Roboto - avatar
0
Thank you Apollo-Roboto but I find OOP very hard , any tips how it can get easier ? I mean the theory of OOP 4 pillars are easy but applying them into code is the challenge
19th Jun 2022, 10:26 AM
Mustafa Azzoz
Mustafa Azzoz - avatar
0
I like to see a class as a collection of information. From your example, you can say that you have a collection of information for a student. The class is just there to keep it together.
19th Jun 2022, 10:34 AM
Apollo-Roboto
Apollo-Roboto - avatar
0
And the syntaxes will get easier by practice , right ? Apollo-Roboto
19th Jun 2022, 10:35 AM
Mustafa Azzoz
Mustafa Azzoz - avatar