Michael C. Gates
Michael C. Gates

Reputation: 992

Property with method?

Maybe I am overlooking something obvious, but I have seen in code where you may have a property like "HairColor", and then a method like "HairColor.Update()". Is this possible?

Person person = new Person(int personID);
person.HairColor = "Blonde";
person.HairColor.Update();

I have specific properties that I want to be able to extend on a case by case basis. I guess I could have a method called "HairColorUpdate", but seems that HairColor.Update() should be possible. I don't want to use "set" because I don't always want to update the DB this way.

The reason I am doing this is because I may only want to call the database to update one column instead of calling my save method which updates every column hopefully improving efficiency.

Upvotes: 3

Views: 137

Answers (6)

Base33
Base33

Reputation: 3215

Couldn't you just do:

public class HairColor : String
{
     private int _personID;

     public HairColor(int personID)
     {
         _personID = personID;
     }

     public void Update()
     {
          //update the database here for hair color
     }
}

This way you can have more control with specific functionality. Remember, class responsibility.

This solution is only if you really want to update one column of course.

Hope that helps.

Upvotes: 0

code4life
code4life

Reputation: 15794

HairColor would probably have to be a struct type to make what you want to work. Plus, you would have to do operator overloading. I'm not sure if this is truly the design path you want to pursue. If, for some perverse reason, you absolutely must do it, this is the kind of struct definition you would need (very roughly, LOL):

public struct HairColorStruct
{
    private string m_hairColor;

    public void Update()
    {
        // do whatever you need to do here...
    }

    // the very basic operator overloads that you would need...
    public static implicit operator HairColorStruct(string color)
    {
        var result = new HairColorStruct();
        result.m_hairColor = color;
        return result;
    }

    public static explicit operator string(HairColorStruct hc)
    {
        return hc.m_hairColor;
    }

    public override string ToString()
    {
        return m_hairColor;
    }

    public static bool operator ==(HairColorStruct from, HairColorStruct to)
    {
        return from.m_hairColor == to.m_hairColor;
    }

    public static bool operator ==(HairColorStruct from, string to)
    {
        return from.m_hairColor == to;
    }

    public static bool operator !=(HairColorStruct from, HairColorStruct to)
    {
        return from.m_hairColor != to.m_hairColor;
    }

    public static bool operator !=(HairColorStruct from, string to)
    {
        return from.m_hairColor != to;
    }
}

You can then redefine your Person object like this:

public class Person
{
    public HairColorStruct HairColor { get; set; }

    // whatever else goes here...
}

In your code, HairColor can be simply assigned whatever you desire as long as it's a string:

var person = new Person();
person.HairColor = "Blonde";

// this will emit "True" to the console...
if (person.HairColor == "Blonde")
{
    Console.WriteLine(true);
}
else
{
    Console.WriteLine(false);
}

// update the info per your logic...
person.HairColor.Update();

// you can change the hair color and update again, LOL
person.HairColor = "Brown";
person.HairColor.Update();

Upvotes: 3

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

I think you need Update method on the Person class, not Update method on string:

Person person = new Person(int personID);
person.HairColor = "Blonde";
// set other properties
person.Update();

Actually mixing business logic of person class and database-related logic is rarely good idea. So, I'd go to something like:

dbSession.SaveOrUpdate(person); // NHibernate

Or even extension method:

public static void Update(this Person person)
{
   // connect to database and do update query
}

Upvotes: 0

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

person.HairColor.Update() just means that the type returned by the HairColor property has a method called Update. In your example it looks like HairColor is a string, so to achieve that you need to implement an extension method for string. E.g. something like

static class StringExtension
{
    public static void Update(this string s)
    {
        // whatever
    }
}

However, I don't see the point of doing this. The Update method is a static method that works on the string, so it doesn't affect the Person instance. (even if string did have an Update method it would be unrelated to the Person type).

I believe you would want to have the Update method on Person instead as others have pointed out.

Upvotes: 8

Jetti
Jetti

Reputation: 2458

That is not possible, unless the return value of HairColor has an Update() method. Another way to do this is to have

Person person = new Person(int personID);
person.HairColor = "Blonde";
person.Update();

Where the update method checks to see which fields have been updated and then updates the database based on that information.

Upvotes: 4

Tigran
Tigran

Reputation: 62248

It's not a property with a method, but type returned by the property has that method declared inside. So you use a property and within returned value, which is the type, call the method.

Upvotes: 0

Related Questions