Property with method?

Maybe I'm missing something obvious, but I've seen in the code where you can have a "HairColor" property and then a method like "HairColor.Update ()". Is it possible?

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

      

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

The reason I am doing this is because I can only call the database to update one column instead of calling a save method that updates each column, hoping for efficiency.

+3


source to share


6 answers


person.HairColor.Update()

means that the type returned by the property HairColor

has a method called Update

. In your example, it looks like HairColor

- this string

, so you need to implement an extension method for string

. For example. something like

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

      



However, I see no point in doing this. A method Update

is a static method that operates on a string, so it doesn't affect the instance Person

. (even if it string

had a method Update

, it would not be associated with a type Person

).

I suppose you would like to use the method Update

on Person

as others have pointed out.

+8


source


This is not possible unless the HairColor return value has an Update () method. Another way to do it is to have

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

      



If the update method checks which fields have been updated, then updates the database based on this information.

+4


source


HairColor

there probably needs to be a struct type to do what you want to work. Also, you will have to do operator overloading. I'm not sure if this is really the design path you want to pursue. If for some twisted reason you absolutely need it , this would be the struct definition you need (very rough, 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;
    }
}

      

Then you can override your object Person

like this:

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

    // whatever else goes here...
}

      

In your code, HairColor

you can just assign whatever you want, as long as it's 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();

      

+3


source


It is not property

with a method, but the type

returned property has this method declared internally. So you use the property and on the return value, which is the type, the method is called.

0


source


It seems to me you want the Update method on the Person class, not the Update method on the line:

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

      

In fact, mixing human class business logic and database based logic is rarely a good idea. So, I would go for something like:

dbSession.SaveOrUpdate(person); // NHibernate

      

Or even an extension method:

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

      

0


source


Could 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
     }
}

      

In this way, you can have more control with certain functions. Remember class responsibility.

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

Hope it helps.

0


source







All Articles