0

I have many classes that have the following members/methods:

private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public bool isNamed(String name) { return getName().Equals(name); }

Every time I create a new class that has a member "name", I have to rewrite all these. Is there a way to write the methods one time and to make them apply to any class I want?

svick
  • 236,525
  • 50
  • 385
  • 514
Stefanos Kargas
  • 10,547
  • 22
  • 76
  • 101

4 Answers4

3

You'd simply define a base class (you could make it abstract):

public abstract class Named 
{
    public string Name { get; set; }    
}

and inherit from it:

public class Person : Named 
{ 
}

You don't really need isNamed as in C#, it is perfectly safe to compare strings with ==.


If your class already inherits from another class which is not Named, you'll have to manually add the Name auto property or resort to simulated multiple inheritance.

Alternatively, you could create a specific modification of Named for every base class:

public abstract class NamedLifeForm : LifeForm
{ 
    public string Name { get; set; }
}

public class Person : NamedLifeForm
{
    // Person inherits both a Name and all relevant members of LifeForm
}

Another alternative would be to create a generic wrapper, Named<T>, that would have two properties: the Name and an instance of T. But that would make construction and access cumbersome, so I don't recommend it.

Community
  • 1
  • 1
Adam
  • 15,537
  • 2
  • 42
  • 63
  • @Downvoter: I'd be happy to know if there are any mistakes in my answer so I can improve it. – Adam Feb 10 '13 at 16:48
  • What if my class inherits from another class which is not "Named"? As far as I know there is no multiple class inheritance in C#, just multiple interface inheritance, which doesn't help me here, because interfaces have no implementation and I want to implement these methods once. – Stefanos Kargas Feb 10 '13 at 17:14
  • @StefanosKargas I've edited my answer in an attempt to provide useful alternatives. There really aren't that many options because auto properties make the problem a lot less relevant as they only take a single line of code. – Adam Feb 10 '13 at 17:47
3

Your code can be converted to:

public String Name { get;set;}

Then you can use it as so:

nObject.Name = "Stefan";
if(nObject.Name == "Stefan"){
   // do something
}else{
   // do something else
}

To apply to all the classes automatically you can just make this into an interface:

public interface INameable{
   public String Name {get;set;}
}

Doing this will allow you to inherit from other base classes of importance.

see here for an example

class YourClass : INameable{
  //implementation
}

And now, YourClass has "Name" property automatically inserted.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • I think naming your variable `object` is confusing, especially since it wouldn't compile (`object` is a keyword). – svick Feb 10 '13 at 16:52
2

C# has AutoProperties just for that:

public String Name {get; set; }

This handles both the getName() and the setName() you talked about.

Usage:
To set a value: Name = "MyName;
To get a value: string theName = Name;

Blachshma
  • 17,097
  • 4
  • 58
  • 72
0

I'd suggest reading up on Object Oriented Programming. You can save yourself a lot of time and effort (and heckling). Here is a good primer http://www.amazon.com/Beginning-Object-Oriented-Programming-Dan-Clark/dp/1430235306

To answer your specific question, you should read about inheritance. It lets you define a "Parent" class with functions. Then you can inherit with "Child" classes and have those same functions.

http://msdn.microsoft.com/en-us/library/ms173149(v=vs.80).aspx

Here is a code example

public class PersonBase
{
     private String name;
     public String getName()
     {
          return this.name;
     }
     public void setName(string name)
     {
          this.name = name;
     }
     public bool isNamed(string name)
     {
           return this.name.Equals(name);
     }
}

public class Employee : PersonBase
{
}

Employee will now have whatever was defined by PersonBase.

As others have pointed out, you can simplify you code with properties. Also you should check for null values before using "this.name".

Here is a link to what properties are: http://msdn.microsoft.com/en-us/library/x9fsa0sw(v=vs.80).aspx

The simplified code example would be:

public class PersonBase
{
     public String Name { get; set; }
}

public class Employee : PersonBase
{
}

I hope this helps get you pointed in the right direction for learning about these concepts.

Sam
  • 2,166
  • 2
  • 20
  • 28