0

This is what I have tried so far:

class Student 
{
    private string name;

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            if (value != null && value != "") // if empty, no go
            {
                name = value;
            }
        }          
    }

Now I need to add a list of names there i.e "Jake, Bolt, House, Doe"

I have attempted

Student n = new Student(Jake, Bolt, House, Doe); // no good.

1st week learning programming. Advise is greatly appreciated.

Gegee
  • 35
  • 2

4 Answers4

1

When you use the new syntax, you are essentially calling a special method called a constructor. A constructor always has the same name as the class and never has a return type (not even void). Otherwise, you write it like an ordinary method (with certain enhanced abilities). So for example

class Student
{
    public Student(string name)
    {
        Console.WriteLine(name);
    }
}

If you then instantiate a Student, the constructor runs:

var s = new Student("Joe");  //Outputs "Joe"

If you would like to accept two names, you could of course add a constructor with two arguments:

class Student
{
    public Student(string name1, string name2)
    {
        //Implementation
    }
}

Or if you want a flexible number of arguments, you can use the params keyword:

class Student
{
    public Student(params string[] name)
    {
        //Implementation
    }
}

Although it looks like an array to the constructor, the caller can supply a list of arguments as if they were separate parameters. The compiler will convert the parameter list into an array.

var s = new Student("John","Dick","Harry");

On the other hand, this whole approach doesn't make sense to me. A student has only one name. Maybe you need a list of students, like this:

class Student
{
    public string Name { get; set; }
    public Student(string name)
    {
        this.Name = name;
    }
}

var s = new System.Collections.Generic.List<Student>();
s.Add(new Student("John"));
s.Add(new Student("Dick"));
s.Add(new Student("Harry"));

Here is a link to a working example on DotNetFiddle

John Wu
  • 50,556
  • 8
  • 44
  • 80
  • While most of this answer is fine, I wouldn't recommend creating a special collection class just for the sake of it - just use `List` instead of creating a `StudentList` class. – Jon Skeet May 08 '18 at 18:33
  • Tried this but its saying "the contextual keyword "var" may only appear within a local variable declaration..." – Gegee May 08 '18 at 19:20
  • I'll add a link to a working example so you can see how it all fits together. – John Wu May 08 '18 at 19:46
  • Thanks John !, Worked Great – Gegee May 12 '18 at 02:38
0

First of all, if you need to pass the student name to the constructor, you need to create a constructor that accepts a string parameter so you can use it to set the name of the student (it doesn't work automatically):

class Student 
{
    public Student(string stName)
    {
        Name = stName;
    }

    // Rest of your class.
    // .
}

Now, you can create students like this:

Student st1 = new Student("Jake");   // Note that the name needs to be enclosed in double
Student st2 = new Student("Bolt");   // quotes ("someName") because it's a string literal.

This will create two Student objects that you can add to a List<Student>:

List<Student> students = new List<Student>();
students.Add(st1);
students.Add(st2);

Now, that you get the idea, you can also create a static method in the Student class which takes multiple student names as parameters using the params keyword, creates the students for you and returns a List<Student> like this:

class Student
{
    public static List<Student> CreateStudents(params string[] names)
    {
        List<Student> students = new List<Student>();
        foreach (string stName in names)
        {
            students.Add(new Student(stName));
        }
        return students;
    }

    // Rest of your class.
    // .
}

Which you can call like this:

List<Student> students = Student.CreateStudents("Jake", "Bolt", "House", "Doe");
0
{
    ....
    var jake = new Student("Jake");
    var bolt = new Student("Bolt");
    var house = new Student("House");
    var doe = new Student("Doe");
}

public class Student 
{
    public Student(String name) { Name = name; } 
    public string Name { get; set; }        
}

You don't need anything inside the property setter unless you are trying to leave the previously set name if no value is set which would be a strange requirement.

jwize
  • 4,230
  • 1
  • 33
  • 51
  • This does not prevent `Name` from being set if the string is not null or empty which the OP's logic currently has – maccettura May 08 '18 at 18:26
  • var s = new Student(); s.Name = "Hello"; s.Name = null; Clearly using his code the result would be Console.Write(s.Name ); // Outputs Hello. I never claimed that Name would be prevented from being set if it was non-null – jwize May 09 '18 at 23:07
0

If this is the Student class, so I would say that you will need to create multiple Students with different names.

Something like that:

Student s1 = new Student("Joe");
Student s2 = new Student("Mark");
Student s3 = new Student("Gegee");

And then you can add these Student to a list to be available for future use, something like:

List<Student> studentList = new List<Student>{
s1, s2, s3
};

But for sure in this case you will need to implement the constructor in the Student class:

class Student{
  public Student(string name)
  { this.name = name; }
}

And also you can use your Property Name to set the Student name instead of the constructor:

Student s1 = new Student();
s1.Name = "Joe";

In case you want this Student class to hold multiple students, I would suggest call it something like Class or Group and this class will contain multiple student objects or list of students names.

Amr Elgarhy
  • 66,568
  • 69
  • 184
  • 301