 |
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

November 6th, 2004, 02:18 PM
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Auto generate get and set methods
Hi all,
Are there any way to generates set and get methods for private variables of C# or VB.net classes? It takes to much time to do that if you have alot of variables.
Thanks,
John
|

November 8th, 2004, 06:44 AM
|
Registered User
|
|
Join Date: Nov 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
What about writing your own property Get and Set Tool?
|

December 24th, 2007, 06:39 AM
|
Registered User
|
|
Join Date: Dec 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes there is. I have written a website for just that purpose:
www.houen.net/codegenerator
Simply paste in the instance variables and it will respond with get and set functions. It supports Java, C#, PHP, Flash and C++.
VB support will be coming shortly.
Java example:
insert
Code:
private int numusers
generates
Code:
//-------------QUERIES-------------
public int getNumusers() {
return this.numusers;
}
//-------------COMMANDS------------
public void setNumusers(int numusers) {
this.numusers = numusers;
}
Whether you want camelCase or under_score in the function name is up to you
|

December 29th, 2007, 06:00 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
That's not how you write properties in C#.
private int _numusers;
public int NumberOfUsers
{
get { return _numusers; }
set { _numusers= value; }
}
/- Sam Judson : Wrox Technical Editor -/
|

December 29th, 2007, 07:52 AM
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
May I ask for what purpose you need such a tool?
I'm asking because I'm currently writing code generator which creates classes and properties based on selected database schema. I could extend that a bit to suit your needs. It's just that I'm a pretty lazy coder at home so it might take a while :)
|

December 29th, 2007, 09:23 AM
|
Registered User
|
|
Join Date: Dec 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by samjudson
That's not how you write properties in C#.
private int _numusers;
public int NumberOfUsers
{
get { return _numusers; }
set { _numusers= value; }
}
/- Sam Judson : Wrox Technical Editor -/
|
Yes, i know that is a way to do it in C#, however, correct me if im wrong but both are valid ways of writing the functions. The reason i made the one above is that it is the one i am comfortable with from Java and other languages, and the one you write can be autogenerated by Visual Studio 2005 and greater.
|

December 29th, 2007, 10:37 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Technically, the autogenerated java-style methods that houen has provided will function, but they are methods not properties. By not being true C# properties they are not discoverable nor bindable.
If you are using a Visual Studio product there is a shortcut to creating a property: type 'prop' and hit TAB a few times. VS will write out the whole property code for you with TAB anchors on the three areas you would change: data type, backing variable name and the public property name. You hit tab to move between the three fields. As you update the values VS changes the code as necessary. When done, you hit enter and VS moves your cursor to after the property code so you can enter another. With this method you can enter properties quite fast.
Another method is to put in the backing variable by itself, then use a refactoring tool (VS has some basic ones built in) to encapsulate the field in a property. If you know you are going to make a property, I'd use the previous suggestion. Property refactoring is useful if you find later on that you need to expose a class field that you are already using.
In .NET 3.5 C# supports autogenerated properties. The syntax is something like this:
public string FirstName { get; set; }
When this is compiled, .NET generates a backing variable for you.
-Peter
|

January 9th, 2008, 03:47 AM
|
|
Quote:
quote:
If you are using a Visual Studio product there is a shortcut to creating a property: type 'prop' and hit TAB a few times.
|
I am getting this in c# but not in vb.net
Any similar method is there!
regds
jomet.
---------------------------------------------
Once you start a working on something,
dont be afraid of failure and dont abandon it.
People who work sincerely are the happiest.
|

January 9th, 2008, 03:58 AM
|
|
Hi Imar,
i tested it, nicely working!
Many thanks for your sudden reply!
regds
jomet.
---------------------------------------------
Once you start a working on something,
dont be afraid of failure and dont abandon it.
People who work sincerely are the happiest.
|
|
 |