none
How to generate custom Properties from fields in VB 2015 ? RRS feed

  • Question

  • Hi everybody,

    As title says I'm looking for a convenient way to generate Properties from Class Fields in VB 2015.

    Let's say I've got a Class Person with these fields :

    Public Class clsPerson
    
        Private mName As String
        Private mAge As Integer
    
    End Class

    I right click on the field, then use refactoring, and this is what I get :

      Public Property MName1 As String
            Get
                Return mName
            End Get
            Set(value As String)
                mName = value
            End Set
        End Property
    
        Public Property MAge1 As Integer
            Get
                Return mAge
            End Get
            Set(value As Integer)
                mAge = value
            End Set
        End Property

    That's what I want except for those "MName1" and "MAge1".

    I want to keep my way in creating fields and I always put an "m" and then the field name,

    so Property names should be "Name" and "Age" without that "1" at the end.

    What could be a quick way to get my properties generated ? Maybe some rules I can set in Visual Studio Options ?

    Thanks to everyone able to help.

    Wednesday, February 22, 2017 9:10 AM

All replies

  • Hello,

    In VS2015 (unsure of older versions) select the field, press the key combination CTR+. and you get the following.

    Results

    Public Class clsPerson
    
        Private mName As String
        Private mAge As Integer
    
        Public Property MName1 As String
            Get
                Return mName
            End Get
            Set(value As String)
                mName = value
            End Set
        End Property
    
        Public Property MAge1 As Integer
            Get
                Return mAge
            End Get
            Set(value As Integer)
                mAge = value
            End Set
        End Property
    End Class
    You can of course rename then after the refactor is finished.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, February 22, 2017 10:49 AM
    Moderator
  • Hello,

    In VS2015 (unsure of older versions) select the field, press the key combination CTR+. and you get the following.

    Results

    Public Class clsPerson
    
        Private mName As String
        Private mAge As Integer
    
        Public Property MName1 As String
            Get
                Return mName
            End Get
            Set(value As String)
                mName = value
            End Set
        End Property
    
        Public Property MAge1 As Integer
            Get
                Return mAge
            End Get
            Set(value As Integer)
                mAge = value
            End Set
        End Property
    End Class
    You can of course rename then after the refactor is finished.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Thanks for attention.

    Your answer is just what I said.

    The "rule" VS uses is to rename Properties Names as my/your example.

    So in a 100 fields Class I'll have to change manually each Property Name 100 times...

    My question was just how to change that "rule", but maybe there's nothing in VS Options allowing me to do so...

    Wednesday, February 22, 2017 11:02 AM
  • There are no options to change the rules through Visual Studio options under Tools menu, Options, Refactoring Essentials.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, February 22, 2017 11:15 AM
    Moderator
  • Marco,

    You might want to look at CodeRush - it's pretty flexible once you get the hang of it:

    https://www.devexpress.com/products/coderush/


    "One who has no vices also has no virtues..."

    Wednesday, February 22, 2017 11:23 AM
  • Since version 2010 has VB automatic implemented properties. 

    Since version 2015 that is also for read only. Behind the scene is created a field with a prefix underscore as field. 
    (The prefix _underscore was after a end user preference asked to all Visual Basic MVP's)

    You can of course still write them completely by hand. But that has only sense if you are paid per line. It cost more at creating and at maintenance and has more chance on unwanted errors. But I know there are still persons who avoid also using a computer because on a paper is better they tell (and never tell why it is better).

    https://msdn.microsoft.com/en-us/library/dd293589(v=vs.140).aspx


    Success
    Cor

    Wednesday, February 22, 2017 11:26 AM
  • Select all of the fields you already defined.

    Use the Quick Replace command on the Edit menu.

    In the Find bar of the Replace dialog, enter "Private m".

    In the Replace bar, enter "Public Property " (be sure to include the space).

    Ensure that the range is set to Selection.

    Hit the Replace All button.

    All of your fields will be converted into auto-implemented properties.

    -EDIT-

    Note that your fields, while no longer appearing in the code, still exist and are named the same as the property but prefixed with an underscore.  You can still access them via code.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"



    Wednesday, February 22, 2017 12:34 PM
    Moderator
  • Thanks for attention.

    - CodeRush is third party and DevExpress is not free, so I can't use it.

    - I don't want to use AIP ( Auto-Implemented-Properties ) because I work as a part of a team and we don't use AIP. We have the rule to write full Properties declarations with standard Get/Set methods.

    Wednesday, February 22, 2017 3:09 PM
  • Thanks for attention.

    - CodeRush is third party and DevExpress is not free, so I can't use it.

    - I don't want to use AIP ( Auto-Implemented-Properties ) because I work as a part of a team and we don't use AIP. We have the rule to write full Properties declarations with standard Get/Set methods.

    It was a thought. :)


    "One who has no vices also has no virtues..."

    Wednesday, February 22, 2017 3:13 PM
  • Thanks for attention.



    - I don't want to use AIP ( Auto-Implemented-Properties ) because I work as a part of a team and we don't use AIP. We have the rule to write full Properties declarations with standard Get/Set methods.

    Yes that is what I wrote, doing outdated things without knowing why it is done, beside if they charge the repair of bugs and are paid per line of code. 

    However, not blaming you, probably an outdated knowledge at the team management.

    I write this to warn others before they think it is better to use outdated rules if you cannot tell why you use it. 


    Success
    Cor

    Wednesday, February 22, 2017 3:24 PM
  • Thanks for attention.

    - CodeRush is third party and DevExpress is not free, so I can't use it.

    - I don't want to use AIP ( Auto-Implemented-Properties ) because I work as a part of a team and we don't use AIP. We have the rule to write full Properties declarations with standard Get/Set methods.

    Sounds like you are stuck with a lot of manual coding then... or writing your own VS extension to automate the generation of the boiler-plate code.

    I have to agree with Cor that your internal rules may be in need of a review.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, February 22, 2017 5:07 PM
    Moderator