Can I set a method to only work for a certain property in my custom class?


Nov 25, 2014
Programming Experience
I would like the rename method (only method I have so far) to ONLY be able to be used for path.profile property. Is this possible?

 class paths
        public string profile { get; set; }
        public string profileV2 { get; set; }
        public string profileOld { get; set; }
        public string citrixTemp { get; set; }
        public string profileUnity { get; set; }
        public string cookies { get; set; }
        public string citrix { get; set; }
        public string icaClient { get; set; }
        public string sun { get; set; }
        public string microsoft { get; set; }
        public string appData { get; set; }
        public string systemProfile { get; set; }
        public string users { get; set; }
        public string profiles { get; set; }

        static bool reName(paths path)
            if (Directory.Exists(path.profileOld))
            Directory.Move(path.profile, path.profileOld);
            return true;
I'm not really sure what you mean. Can you elaborate a bit?

Well, I have about 10 properties in my class, and the rename() method as well. What I was curious about, is there a way to write the method, that it would only be available to be used with the profile property?

For instance, when you type path.profile. the intelli-sense would give the rename () option.... but with any other property it wouldn't, even though they are part of the same class.

this begs another question... can I even write my class like this? Where my attempt was to create properties to store data, and methods to manipulate said data? For instance the paths.profile stores a profile path on a server, and the paths.profile.rename() method I had hoped would rename that profile path and add a .old to it. However, every other property in my class are paths that will be deleted only, not renamed. Hence my curiosity about whether this could be accomplished.

It's no real big deal, was really more curious about it, I'm new to C# and .NET and trying to learn more.
It sounds like you're talking about writing an extension method, but such methods extend a type, not a member. If you were to write such an extension method then it would be extending the String type, so you could call it on any String. If I'm interpreting what you're saying correctly then no, you can't limit it to a specific property. In fact, I don't think you can do what you want even if I'm not interpreting it properly because the aim doesn't sound like anything that I've ever done or seen done.
Based on the approach (not the code) that I described in, you can make use of inheritance.

You create a class with the methods that apply to all directories; I've called it StandardDirectory and we will use it later as a base class.

    class StandardDirectory
        public string directoryname;

        public StandardDirectory(string path)
            directoryname = path;

        public void rename(string newname)
            // rename directory
            Console.WriteLine("rename to " + newname);

        public virtual void copy(string newname)
            // copy directory to newpath
            Console.WriteLine("copy to " + newname);

This class has a variable directory; when instantiating, you pass the directory name as an argument. Next it has a few methods
  • rename
  • copy
Note the difference in the declarations of the two, the copy() uses the virtual keyword. We will use this later.
You can use this class straight away in the usual way; e.g
            StandardDirectory standarddir = new StandardDirectory("standarddir");

Next you can create a class that is based on the previous class but adds other functionality (delete) and / or overrides the standard behaviour of an existing method.

    class OtherDirectory : StandardDirectory
        public OtherDirectory(string path)
            : base(path)

        public override void copy(string newname)
            string newpath = "D:\\" + newname;
            //copy directory to newpath
            Console.WriteLine("copy to " + newpath);

        public void delete()
            // your delete code here
            Console.WriteLine("delete " + directoryname);

The class declaration as well as the constructor indicate that this class is based on the earlier created class. Next the class has two methods
  • copy
  • delete

The copy method overrides the method of the base class; that's why the declaration of the method in the StandardDirectory class was declared as virtual. In the OtherDirectory class, we indicate that the copy method overrides the 'original' by using the keyword override; the difference between the two copies is
  • The copy in the base class copies to a path; for the example it can be anything but the thought is that the destination would by a path on the current drive.
  • The copy in the derived class prepends the destination name with a drive letter before copying (so copying to another disk).
This is just an example and for the overridden method you should first check if it does not already contain a drive letter.

The delete method is new; it's only known to instances of the OtherDirectory class, not to instances of the StandardDirectory class.

You can use this class as below
            OtherDirectory otherdirectory = new OtherDirectory("otherdir");

A compile error will occur if you try to use the delete method for an instance of the StandardDirectory class (and intellisense will not show the delete method as being available).

Note that this deviates slightly from your question (I used delete instead of rename to only be available for certain directories); after all, I need to leave you a little bit of the puzzle.

I hope this helps.
Top Bottom