Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# method: is there a parameter name to insert 'derived classname' generically?

Tags:

c#

I want to create a base class Student with a method changeName inside of it. I want studentclasses derived from the base class Student, such as collegestudent etc. I want to be able to change the names of the students.

I have this code:

public abstract class Student
{
    public virtual void changeName(CollegeStudent s, string name)
    {
       s.firstName = name;
    }

    public abstract void outputDetails();
}

public class CollegeStudent : Student
{
    public string firstName;
    public string lastName;
    public string major;
    public double GPA;

    public override void outputDetails()
    {
        Console.WriteLine("Student " + firstName + ");
    }
}

I want to know if its possible to change the public virtual void changeName parameters to a generic parameter that accepts any derived class from Student.

Is this possible in c#?

(Something like this: public virtual void changeName(Anyderivedclass s, string name)

like image 639
Viking Avatar asked Nov 10 '15 08:11

Viking


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

If you changed your design a little, things would become much easier. It seems like you're coming from a Java background, so lets see how C# can help improve your code.

First, Take the FirstName/LastName fields and move them to the base class, as any student registering must supply these anyway. Second, C# has a feature called Properties (specifically here we can use Auto-Implemented Properties, since we don't need validation), which is basically syntactic sugar for get/set methods. You can re-create you student class to look like this:

public abstract class Student
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

This will allow any instance of a derive class to change the firstname and lastname properties, without adding any extra methods:

void Main()
{
    var student = new CollegeStudent();
    student.FirstName = "Yuval";
}

Generally speaking, any instance method on the object you're creating shouldn't be accepting it's own type in order to mutate itself.

like image 150
Yuval Itzchakov Avatar answered Nov 15 '22 02:11

Yuval Itzchakov


But changeName() is an instance method of your abstract Student. Why are you passing in an instance of one of it's derivatives as one of it's parameters?

Why do you need generics? It should be as simple as...

public abstract class Student
{
    public abstract void changeName(string newName);

    public abstract void outputDetails();
}

public class CollegeStudent : Student
{
    public string firstName;
    public string lastName;
    public string major;
    public double GPA;

    public override void changeName(string newName)
    {
          firstName = newName;
    }

    public override void outputDetails()
    {
        Console.WriteLine("Student " + firstName + ");
    }
}

And in fact (as pointed out) the presence of changeName() in the base class suggests that the name properties belong to the base class, so it should be as follows...

public abstract class Student
{
    public string firstName;
    public string lastName;

    public virtual void changeName(string newName)
    { 
        firstName = newName;
    }

    public abstract void outputDetails();
}

public class CollegeStudent : Student
{
    public string major;
    public double GPA;

    public override void outputDetails()
    {
        Console.WriteLine("Student " + firstName + ");
    }
}
like image 43
Matt Avatar answered Nov 15 '22 02:11

Matt