Zozo
Zozo

Reputation: 183

Passing object attribute to method parameter C#

Lets say I have a person class with name and id and a animal class with the same attributes, and i have a list with persons and animals. Now i want to make a method for returning the last id from that list and increment it. I want to make it universal so i can use it later.

public static int getNextId(List<Object>param)
{
    int lastId = Int32.Parse(param[param.Count - 1].id);
    if (lastId!=0)
    {
        return lastId++;
    }
    return 0;
}

but the 'id' is underlined because object does not have id.

Edit:

something like this in python

def someMethod(self, someList, attr):
    objAttr = getattr(someObject, attr)
    for(item in someList):
        return item.objAttr

Upvotes: 0

Views: 1336

Answers (3)

Alexander Petrov
Alexander Petrov

Reputation: 14231

public static int GetNextId(List<object> param)
{
    int id = param.OfType<Person>().Last().Id;
    return ++id;
}

OfType method filters the elements based on a specified type. Only people will be filtered from the list of people and animals.

Last method returns a last element of a sequence.

Upvotes: 0

Fabio Silva Lima
Fabio Silva Lima

Reputation: 714

Create a Person class with a Id Property like this:

public class Person
{
     public int Id {get; set;}
}

So you can use:

public static int getNextId(List<Person>param)
{
    int lastId = param.Last().Id;
    if (lastId!=0)
    {
        return lastId++;
    }
    return 0;
}

Another aproach is using interfaces to make "universal" like you said:

public interface IId
{
     int Id {get;set;}
}

public class Person : IId
{
     public int Id {get; set;}
}

public static int getNextId(List<IId>param)
{
    int lastId = param.Last().Id;
    if (lastId!=0)
    {
        return lastId++;
    }
    return 0;
}

Upvotes: 0

germi
germi

Reputation: 4658

Your approach is not how you handle stuff like this in statically typed languages such as C#. You can only access properties / fields that are declared on the specific type. And object does not have a public field or property called id.

There are some ways around this: One would be having a base class that has an id property from which your other classes could inherit:

public class IdHolder
{
    public int Id { get; set; }
}

public class Person : IdHolder
{
    // Person inherits the 'Id' property from IdHolder
    // other properties unique to person...
}

IdHolder could just as well be an interface or an abstract class - it depends on your specific use case (note that you would have to implement the Id property in each implementing class if you'd chose to make IdHolder an interface).

If you chose to have a base class (or interface, ...) you'd change your method to accept that as a parameter:

public static int getNextId(List<IdHolder>param)
{
    int lastId = param[param.Count - 1].Id;
    if (lastId!=0)
    {
        return lastId++;
    }
    return 0;
}    

Another - slightly dirty - option is to use reflection. As I don't think that this is a sensible route for you to take, I won't go further into this here.

I'd advise you to have a look at an intro book into C# as there are some other aspects of your code that don't really follow C# guidelines (e. g. using camelCase instead of PascalCase).

Upvotes: 1

Related Questions