469,358 Members | 1,659 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

class fields vs class properties

What is the difference between class fields and class properties?

Thanks
Mike
Aug 4 '05 #1
7 2223
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by John Baro | last post: by
6 posts views Thread by rodchar | last post: by
16 posts views Thread by Richard Brown | last post: by
7 posts views Thread by Koda | last post: by
2 posts views Thread by Torben Laursen | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.