0

I have a question that I've been searching for the answer to but have not found anything as of yet. This leads me to think that I'm not asking the question correctly as I search, so I'll apologize in advance for my own confusion and lack of knowledge.

I am trying to determine if it is possible, and if so, how I can construct classes so that the Properties have additional custom methods attached to them. If feels like the hierarchy of classes, as I currently do them is very flat. Here is an extremely simplistic class and an example of what I'm attempting to execute:

Updated

namespace Test
{
    public class MyClass
    {
        public string Value
        {
            get; set;
        }
        // HOW DO I CREATE 'AddRepetitiveCharacter(string Character, int Index)' for the 'Value' property?
    }

    static class Program
    {
        static void Main()
        {
            MyClass myclass = new MyClass();
            Label1.Text = myclass.Value.AddRepetitiveCharacter("-", 3);
        }
    }
}

How can I take the Value property and add additional layers of methods to it so that I could perform additional actions on just that property? Is this possible? I've noticed that with a Struct, such as DateTime, I can do a Datetime.Now.. I'm looking to do something similar with my classes... if possible... Otherwise I can do the more traditional:

Label1.Text = AddRepetitiveCharacter(myclass.Value,"-",3);
BBL Admin
  • 57
  • 1
  • 2
  • 9
  • 1
    This is not possible in C# straitforward way.. However if you create extension method for the boolean type it is possible. but the down side is that it will be available to all boolean property. – Jenish Rabadiya Dec 02 '15 at 03:58
  • `Exists` is a `bool`. It's either `true` or `false`. What would calling `GetTotalCount()` on a `bool` do? – Blorgbeard Dec 02 '15 at 04:00
  • C# does not currently support Extension method on properties. See this answer for more info: http://stackoverflow.com/questions/619033/does-c-sharp-have-extension-properties – Steve Dec 02 '15 at 04:04
  • why dont you just write this in two lines or multiple lines? not everything should be done in one line right? – M.kazem Akhgary Dec 02 '15 at 04:08
  • Yeah, my apologies, that was a completely unrealistic example. Should have put in something more realistic. Looks like I've essentially got my answer from the comments, but I'll update my question so it makes more sense. – BBL Admin Dec 02 '15 at 04:14

1 Answers1

2

DateTime.Now is a static property which returns instance of its own struct:

public struct DateTime
{
    public static DateTime Now
    {
        get {
            return /* */;
        }
    }

    public DateTime AddDays(int days) 
    {

    }

    /* All other members of DateTime struct */
}

As property type is DateTime struct, it can contain any members, which can struct contain.

At the same time, your Exists returns primitive bool which cannot have any.

You can make your method return something else, like custom class or collection, in order to make it work.

namespace Test
{
    public class ExistsClass
    {
        public bool Result { get; set; } = true;

        public int GetTotalCount() 
        {
            return Result ? 1 : 0;
        }
    }

    public class MyClass
    {
        public ExistsClass Exists { get; set; } = new ExistsClass();
    }

    static class Program
    {
        static void Main()
        {
            MyClass myclass = new MyClass();
            Console.WriteLine(myclass.Exists.GetTotalCount()); 
        }
    }
}

This snippet will now output 1, because Result is true by default.

Very important note: it should only be done if it does make sense in terms of architecture, model and logics.

For example, it sounds completely wrong for method Exists to have GetTotalCount method. How the boolean fact (yes \ no) of existence can have count property?

Update:

In your example with string and AddRepetitiveCharacter it sounds like you don't want and don't need your own class. You want this property to be string, but with some extended functionality. That's why we have extension methods - it allows you to write methods, which will extend the functionality of any native or your custom class, and use it just like class members:

public static class MyStringExtensions  
{
    public static string AddRepetitiveCharacter(this string originalString, char c, int count) 
    {
        return originalString + new String(c, count);
    }
}

Then, you will be able to use is in the following way in any place of your project:

MyClass myclass = new MyClass();

// both are working and equivalent:
Label1.Text = myclass.Value.AddRepetitiveCharacter('-', 3);
Label1.Text = MyStringExtensions.AddRepetitiveCharacter(myclass.Value, '-', 3);

or simply

string text = "abc";

// both are working and equivalent:
Console.WriteLine(text.AddRepetitiveCharacter('-', 3)); // abc---
Console.WriteLine(MyStringExtensions.AddRepetitiveCharacter(text, '-', 3)); // abc---

Notice the following:

  1. this before method argument
  2. Extension class should be static
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
  • Yes, others pointed out the error of trying to do that with a bool. I updated my question slightly to reflect a situation I was considering doing in this way vs the normal method. Your examples and explanation of the way the DateTime struct in particular work make sense and was very helpful. For my current needs, a more conventional approach will work better. Thanks for your help! – BBL Admin Dec 02 '15 at 04:30
  • Oh wow, I've never explored extensions before. I like that _a lot_, now If only I could upvote your answer :) Thanks again! – BBL Admin Dec 02 '15 at 04:46