Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 594 Vote(s) - 3.39 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can we generate getters and setters in Visual Studio?

#1
By "generate", I mean auto-generation of the code necessary for a particular selected (set of) variable(s).

But any more explicit explication or comment on good practice is welcome.

Reply

#2
By generate, do you mean auto-generate? If that's not what you mean:

Visual Studio 2008 has the easiest implementation for this:

public PropertyType PropertyName { get; set; }

In the background this creates an implied instance variable to which your property is stored and retrieved.

However if you want to put in more logic in your Properties, you will have to have an instance variable for it:

private PropertyType _property;

public PropertyType PropertyName
{
get
{
//logic here
return _property;
}
set
{
//logic here
_property = value;
}
}

Previous versions of Visual Studio always used this longhand method as well.
Reply

#3
Rather than using <kbd>Ctrl</kbd> + <kbd>K</kbd>, <kbd>X</kbd> you can also just type `prop` and then hit <kbd>Tab</kbd> twice.


Reply

#4
I don't have Visual Studio installed on my machine anymore (and I'm using Linux), but I do remember that there was an wizard hidden somewhere inside one of the menus that gave access to a class builder.

With this wizard, you could define all your classes' details, including methods and attributes. If I remember well, there was an option through which you could ask Visual Studio to create the setters and getters automatically for you.

I know it's quite vague, but check it out and you might find it.


Reply

#5
If you are using Visual Studio 2005 and up, you can create a setter/getter real fast using the insert snippet command.

Right click on your code, click on *Insert Snippet* (<kbd>Ctrl</kbd>+<kbd>K</kbd>,<kbd>X</kbd>), and then choose "prop" from the list.

Reply

#6
If you're using ReSharper, go into the ReSharper menu → *Code* → *Generate...*

(Or hit <kbd>Alt</kbd> + <kbd>Ins</kbd> inside the surrounding class), and you'll get all the options for generating getters and/or setters you can think of :-)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through