Wrox Programmer Forums
|
.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
 
Old November 6th, 2004, 02:18 PM
Authorized User
 
Join Date: Oct 2004
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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


 
Old November 8th, 2004, 06:44 AM
Registered User
 
Join Date: Nov 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What about writing your own property Get and Set Tool?


 
Old December 24th, 2007, 06:39 AM
Registered User
 
Join Date: Dec 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old December 29th, 2007, 06:00 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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 -/
 
Old December 29th, 2007, 07:52 AM
Authorized User
 
Join Date: Sep 2007
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 :)

 
Old December 29th, 2007, 09:23 AM
Registered User
 
Join Date: Dec 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

 
Old December 29th, 2007, 10:37 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
 
Old January 9th, 2008, 03:47 AM
jomet
Guest
 
Posts: n/a
Default

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.
 
Old January 9th, 2008, 03:50 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi jomet,

It's property -> Tab in Visual Basic....

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old January 9th, 2008, 03:58 AM
jomet
Guest
 
Posts: n/a
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
C# Auto Generate Codes lasith C# 0 February 23rd, 2007 02:25 AM
how to auto generate date? okboy SQL Server 2000 3 December 12th, 2006 08:17 PM
Auto generate no mateenmohd Classic ASP Basics 3 June 11th, 2006 07:06 AM
Auto generate? nikryden BOOK: Beginning VB.NET Databases 2 December 19th, 2005 12:43 PM
auto generate an number utarian Access 2 March 28th, 2005 02:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.