472,796 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,796 software developers and data experts.

class fields vs class properties

What is the difference between class fields and class properties?

Thanks
Mike
Aug 4 '05 #1
7 2351
Koda,

A field holds data, a property access that data.

I hope this helps,

Cor
Aug 4 '05 #2
This article seems to suggest that they are interchangeable:
http://www.vbdotnetheaven.com/Code/Jun2003/2035.asp

If I have a class User should "Name", "emailAddress" be a field or property
if I am going to want to access (set/get) from other classes?

Thanks for the reply.
Mike

"Cor Ligthert [MVP]" wrote:
Koda,

A field holds data, a property access that data.

I hope this helps,

Cor

Aug 4 '05 #3
I will clarify Cor's response:

A field is a variable that is declared - it holds the actual data.
A property is syntactic sugar for Get and Set methods that you would other
wise have to call. So instead of having to say:

myObj,GetObjID()

you can say:

myObj.ObjID

So a property is really a different way to call get/set methods, and it
relies on the 'fields' in the class to get the correct data.

"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:CE**********************************@microsof t.com...
This article seems to suggest that they are interchangeable:
http://www.vbdotnetheaven.com/Code/Jun2003/2035.asp

If I have a class User should "Name", "emailAddress" be a field or
property
if I am going to want to access (set/get) from other classes?

Thanks for the reply.
Mike

"Cor Ligthert [MVP]" wrote:
Koda,

A field holds data, a property access that data.

I hope this helps,

Cor

Aug 4 '05 #4
So based on your response they are "interchangeable" in their use just not in
how they are called.

Thanks
Mike

"Marina" wrote:
I will clarify Cor's response:

A field is a variable that is declared - it holds the actual data.
A property is syntactic sugar for Get and Set methods that you would other
wise have to call. So instead of having to say:

myObj,GetObjID()

you can say:

myObj.ObjID

So a property is really a different way to call get/set methods, and it
relies on the 'fields' in the class to get the correct data.

"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:CE**********************************@microsof t.com...
This article seems to suggest that they are interchangeable:
http://www.vbdotnetheaven.com/Code/Jun2003/2035.asp

If I have a class User should "Name", "emailAddress" be a field or
property
if I am going to want to access (set/get) from other classes?

Thanks for the reply.
Mike

"Cor Ligthert [MVP]" wrote:
Koda,

A field holds data, a property access that data.

I hope this helps,

Cor


Aug 4 '05 #5
Think of a fielf as a variable.

Think of a Property as syntatic sugar for an getter and setter.

e.g. with a property you can do stuff like this:

private string someString;

public string SomeString
{
set
{
if(value == null)
{
throw new ArgumentNullException("someString cannot be null because
I said so");
}
someString = value;
}
}
i.e. you can have code that will execute within a property and still use it
as you would a normal field.

Does this help?

"Koda" wrote:
What is the difference between class fields and class properties?

Thanks
Mike

Aug 4 '05 #6
Koda wrote:
What is the difference between class fields and class properties?


I think it's a little difficult to explain. Generally, a field is a
declared variable:

class Foo {
int bar;
}

This field is a private field, and good OO practice dictates that fields
should always be private, so they can't be accessed from outside the
class Foo. Now, if the field must be accessed from the outside, one used
to use getter/setter methods to accomplish this, for example in
languages like C++. So you'd have public methods that would allow for
getting/setting the value of the bar variable, probably including some
code for validity checking, especially on the setter. Like this:

class Foo {
int bar;
public int GetBar() {
return bar;
}
public void SetBar(int newbar) {
// check newbar for validity
bar = newbar;
}
}

Properties are a syntactically nicer way of putting this same code. In
C#, the same class might look like this, using a property:

class Foo {
int bar;
public int Bar {
get { return bar; }
set {
// check the new value
bar = value;
}
}
}

Looking at the class Foo from the outside, it's also easier to work with
properties, because you can use normal syntax to get the value of a
property or assign a value to it:

...
Foo foo = new Foo();
foo.Bar = 5;
Console.WriteLine(foo.Bar);
...

This advantage is seen by some as a disadvantage, I should mention -
because the user of the property can't see the implementation of the
getter code, it's possible that the property getter does a lot of
computations every time the setter is called. In these cases it might
still be a better idea to code a "traditional" getter in the form of a
method, to suggest to the caller that getting the value is not a trivial
operation.

Now, one final thing I should note: you were asking about *class* fields
and *class* properties. In some languages, this is a way of referring to
what's called (in C#) a "static" field or property. I'm not sure if
that's what you meant, but just in case it is... a static field or
property is one that only exists once per class type, not once per
instance. To declare it, you'd use the keyword "static" with the
declaration of the variable and/or the property. I'm not going into any
more depth with this for now, please ask if you need more information on
this.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Aug 5 '05 #7
Hi,
class fields are member variables of a class which can hold values, class
properties also serve the same purpose, however, can store(read/write)
values through accessors(get/set). Through accessors we can trigger whenever
there is a change or whenever the value of the property is accessed

best
Subin Kushle

"Koda" <Ko**@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
What is the difference between class fields and class properties?

Thanks
Mike


Aug 5 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Nick Dreyer | last post by:
In VB.NET I would like to not have to create property get/set procedures for every class variable I want to expose to Excel VBA projects in COM builds. Can anyone tell me if that is possible, or...
5
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
3
by: John Baro | last post by:
I have a class as: public class a : { private float m_b; public float b { get {
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
16
by: Richard Brown | last post by:
Ok, now I am truely going nuts... probably why I didn't use the Class Builder in VB6 extensively. But, being the 'proper programmer' that I should, I'm trying to bite the bullet and build...
7
by: Koda | last post by:
What is the difference between class fields and class properties? Thanks Mike
3
by: Jordan | last post by:
Suppose I have a system that keeps track of 5 different types of "People". My intent is to have a base Person class, then 5 derived classes for each of the specific person types (e.g., Patient,...
2
by: mgoold2002 | last post by:
Hello. I've just begun programming in VB .NET, and I'm trying to turn all my modules into classes. In order to retrieve/exchange values from one class to another, I initiated New instances of the...
2
by: Torben Laursen | last post by:
I have a complicated class that I use to store a lot of values Now I need a copy of that class and I have been trying to find a way to do this automatic so I don't have to assign all properties and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.