CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2009
    Posts
    160

    automatically add getters and setters to priv vars

    is there a way that could automate writing getters and setters for private variables in c#?

  2. #2
    Join Date
    Sep 2009
    Posts
    23

    Re: automatically add getters and setters to priv vars

    You can use options in visual studio 2008 - just right click and select Wrap local variable.... or something. I pretty sure that you can add hot key for it. If you want full automatic I believe that the easies way is to write some program for it or a plug in for VS which is not very complex.

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: automatically add getters and setters to priv vars

    Quote Originally Posted by glourung View Post
    You can use options in visual studio 2008 - just right click and select Wrap local variable.... or something.
    Right-click -> Refactor -> Encapsulate field

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: automatically add getters and setters to priv vars

    If you're using .NET 3.0 or higher you can use the automatic property syntax:

    Code:
    // Public getter, private setter
    public int MyProperty {
        get; private set;
    }
    
    // Both are private
    int MyOtherProperty {
        get; set;
    }
    
    // Internal setter, private getter
    internal MyLastProperty {
        private get; set;
    }
    Is that what you wanted?
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    Dec 2008
    Posts
    144

    Re: automatically add getters and setters to priv vars

    You can use code snippets in 2008- I don't know if they are available in previous versions. Just write 'prop' and then immediately hit Tab twice- this will stub out the code for a public property by default, but you can easily change that. It'll pop out something like this:
    Code:
    public int MyProperty { get; set; }
    Just change the type from int to whatever you need, and change the name MyProperty to the name you want to use. You can change it around more by writing private just before set to make it a private set property, etc.

    HTH

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured