-2

I'm trying to create a simple class by referring to a YouTube video, but I get errors.

Here are the errors: Error

And here is my code:

public class Student
{
    public int stdAge;
    public int stdiD;
    public string stdCitizenship;

    public void printStudent()
    {
        Console.WriteLine("Student Age: " + stdAge);
        Console.WriteLine("Student ID: " + stdiD);
        Console.WriteLine("Student Citizenship:" + stdCitizenship);

    }

}


class Program
{
    static void Main(string[] args)
    {
        Student std1 = new Student("Roslin Hashim");


        std1.stdAge(26);
        std1.stdiD(520308);
        std1.stdCitizenship("Malaysia");
        std1.printStudent();
    }
}

How do I know what the problem is and how to fix it?

D Stanley
  • 149,601
  • 11
  • 178
  • 240
t1nny
  • 3
  • 2
  • 3
    To set value of field/[property](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties) ([related](https://stackoverflow.com/q/295104/1997232)) you have to use following syntax: `std1.stdAge = 26;`. – Sinatr Nov 24 '20 at 15:30
  • 3
    That only vaguely looks like C#. If that's actually what's shown in the youtube video, close it now and go find a different source. – Damien_The_Unbeliever Nov 24 '20 at 15:32
  • In C# we name public things **L**ikeThis, not **l**ikeThis – Caius Jard Nov 24 '20 at 15:36
  • Add a constructor that takes a string: ```public Student(string urString) {/*Your code in here*/ }``` – baltermia Nov 24 '20 at 16:07

1 Answers1

0

You're not setting your values properly. I assume you were trying to set stdAge, stdiD and stdCitizenship by doing the following:

    std1.stdAge(26);
    std1.stdiD(520308);
    std1.stdCitizenship("Malaysia");

You're using them like functions when they are not. You need to put getters and setters on stdAge, stdiD and stdCitizenship and then set them using a simple = sign. You also did not create a constructor that takes in 1 string argument. So you'll need to do that. The following code should do what you're trying to do.

public class Student
{
    //Setting getter and setters
    public int stdAge { get; set; }
    public int stdiD { get; set; }
    public string stdCitizenship { get; set; }

    //Here's the constructor you need
    public Student(string name) {
        //Code to do sometihng with name parameter here.
    }

    public void printStudent()
    {
        Console.WriteLine("Student Age: " + stdAge);
        Console.WriteLine("Student ID: " + stdiD);
        Console.WriteLine("Student Citizenship:" + stdCitizenship);    
    }    
}    

class Program
{
    static void Main(string[] args)
    {
        Student std1 = new Student("Roslin Hashim");    
        
        std1.stdAge = 26;
        std1.stdiD = 520308;
        std1.stdCitizenship = "Malaysia";
        std1.printStudent();
    }
}
dogyear
  • 288
  • 1
  • 7