Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET 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 March 6th, 2009, 12:54 PM
Authorized User
 
Join Date: Jun 2008
Posts: 75
Thanks: 15
Thanked 0 Times in 0 Posts
Default Should I use a property or field?

I understand the differences between a property and a field as explained here http://www.c-sharpcorner.com/UploadF...rtiesInCS.aspx but I am still having troubles figuring out when to use what.

Thanks In Advance



 
Old March 6th, 2009, 01:13 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

As I explained in my posts, Properties hide data, while fields provide direct access to the data of a class. Typically, public fields are considered a bad practice as they don't allow you to protect or validate data. Even if you don't need protection or validation, you should still use properties (for example as automatically implemented properties in C# or in an upcoming release of VB.NET). That way, they more or less behave the same as fields (e.g. easy to implement and proivide direct access to the data), but you can still implement other behavior later.

In other words: when in doubt, use properties.

What is the part that is confusing you exactly?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 6th, 2009, 01:17 PM
Authorized User
 
Join Date: Jun 2008
Posts: 75
Thanks: 15
Thanked 0 Times in 0 Posts
Default Hiding Data

I don't understand the part about hiding data. How do Properties hide data and what is a good scenario for when you would want to hide your data?

Thanks In Advance
 
Old March 6th, 2009, 01:27 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Did you check the posts I linked to (and replied on to you) earlier?

One of the examples is a read-only property that returns someone's full name as a combination of a first and last name; something you can't accomplish with a field.

Another example is, for example, a Discount property on an Order class. It could return the discount based on the current user. E.g.

public int Discount
{
if (CurrentFictitiousUser == "Imar")
{
return 500;
}
return 100;
}

That is, I get a 500 euro discount, while others get just 100.

Finally, for validation, think of an e-mail address (as shown in the earlier posts I mentioned). With a field you can't validate, with a property you can.

Hope this helps. If not, http://en.wikipedia.org/w/index.php?...ns0=1&redirs=0

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
dotnetDeveloper (March 6th, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Create Custom Field Property using ADOX BoGusman Pro VB Databases 2 July 13th, 2006 05:16 PM
Bind a property to a table field? ParadiseIsle Access 2 November 12th, 2005 07:13 PM
Setting visible property on a report field tnzabo Access 3 July 21st, 2005 11:30 AM
Update city field based on zip field nganb SQL Server ASP 0 April 22nd, 2004 10:30 PM





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