473,383 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 2386
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.