question

Shervan360-6172 avatar image
0 Votes"
Shervan360-6172 asked cooldadtx answered

How can I define a method in property?

Hello,

I'd like to define a method into a property in my Human class like C#: DateTime.Now.ToLongDateString();

For example:

Human.Age.toDays()

Human is class and Age is the property, return an int and toDays a method, return DateTime.

Thanks

dotnet-csharpdotnet-cli
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Do you want to return the number of days between two dates?

     public class AgeTest
     {
         public DateTime DateOfBirth { get; set; }
         public int ToDays 
         { 
             get
             {
                 TimeSpan ts = DateTime.Now - DateOfBirth;
                 return ts.Days;
             } 
         }
    
         public AgeTest() : this(DateTime.Now)
         {
    
         }
         public AgeTest(DateTime dateOfBirth)
         {
             DateOfBirth = dateOfBirth;
         }
    
     }
    
    
     class Program
     {
         static async Task Main(string[] args)
         {
             AgeTest test = new AgeTest(new DateTime(2021, 1, 1));
             Console.WriteLine(test.ToDays);
         }
    
     }


1 Vote 1 ·

Thank you,

But I'd like to call a method in a property like this:

DateTime.Now.ToLongDateString();

In the above example Now is a property and ToLongDateString() is a method.

0 Votes 0 ·
AgaveJoe avatar image AgaveJoe Shervan360-6172 ·

Your response is very confusing. There is nothing stopping you from calling a method from within a property. Is there anyway you can share code that illustrates the problem you are trying to solve?


     public class AgeTest
     {
         public DateTime DateOfBirth { get; set; }
         public int ToDays
         {
             get
             {
                 TimeSpan ts = Subtract(DateTime.Now);
                 return ts.Days;
             }
         }
    
         public string TestMethod
         {
             get 
             { 
                 return DateOfBirth.ToLongDateString(); 
             }
         }
    
         private TimeSpan Subtract(DateTime FromDate)
         {
             return FromDate - DateOfBirth;
         }
    
         public AgeTest() : this(DateTime.Now)
         {
    
         }
         public AgeTest(DateTime dateOfBirth)
         {
             DateOfBirth = dateOfBirth;
         }
    
     }
    
    
     class Program
     {
         static async Task Main(string[] args)
         {
             AgeTest test = new AgeTest(new DateTime(2021, 1, 1));
             Console.WriteLine(test.ToDays);
             Console.WriteLine(test.TestMethod);
         }
     }




0 Votes 0 ·
Show more comments

1 Answer

cooldadtx avatar image
1 Vote"
cooldadtx answered

I believe you are confused on your terminology. Human.Age.toDays() is not a method defined on a property. This is simply an expression. Let's break it apart.

Human is some identifier you have in your code. It might be a local variable, parameter, or property on the class you are currently in. Doesn't matter here but what does matter is what that type is. Let's assume it is of type HumanBeing. In order to do this Human.Age there must be a property defined on the HumanBeing type called Age. Perhaps like this.

public class HumanBeing
{
   public int Age { get; set; }
}


Now, given a variable of type HumanBeing you can access the Age property using the member access operator (.).

HumanBeing human = new HumanBeing();
human.Age = 10;

HumanBeing human2 = new HumanBeing();
human2.Age = human.Age;


The return value of the Human.Age property is an integer. From that expression you can now access the members of the int type such as ToString.

string result = human.Age.ToString();  //Call the ToString method on the Age property of the variable human


The ToString method has nothing to do with HumanBeing but rather it is a method of the int type. You can chain member calls together indefinitely until you eventually call a method that has no return value.

So, in your example code look at the type of the property Age on HumanBeing. Then go to that types definition and you should find the toDays method. Alternatively right click the toDays call and select Go to Implementation/Definition if you are in Visual Studio.

In more advanced cases you can "extend" a type with new methods by defining an extension method. This is not something you should be doing until you have a good grasp of types and members. However just in case the toDays method is an extension method, when you go to definition on it you might end up in a static class rather than type of the property. In this case you are seeing an extension method. An extension method allows us to extend a type with new methods when it is not practical to modify the type itself (such as ints). In this case we define a static method on a static class and accept the value as the first argument with the keyword this. The compiler then allows the static method to be called as though it is a method on the type when in fact it isn't. This is really for discoverability and readability. Under the hood the code is converted to a call to the static method of the static class. Thus your toDays method might look like this.

public static class MyIntExtensions
{
   public static int toDays ( this int source )
   {
      return source;
   }
}

//Call as either an extension method
int age = 10;
age.toDays();

//or as a regular static method
MyIntExtensions.toDays(age);
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.