473,624 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

class fields vs class properties

What is the difference between class fields and class properties?

Thanks
Mike
Aug 4 '05 #1
7 2407
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", "emailAddre ss" 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**@discussio ns.microsoft.co m> wrote in message
news:CE******** *************** ***********@mic rosoft.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", "emailAddre ss" 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 "interchangeabl e" 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**@discussio ns.microsoft.co m> wrote in message
news:CE******** *************** ***********@mic rosoft.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", "emailAddre ss" 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 ArgumentNullExc eption("someStr ing 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.WriteLi ne(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 "traditiona l" 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**@discussio ns.microsoft.co m> wrote in message
news:06******** *************** ***********@mic rosoft.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
312
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 refer me to some documents that will make me understand why this apparent limitation of VB.NET COM builds is acceptable, i.e. why in "good" object code programs I should expect to never have to create large numbers of public class variables that...
5
14421
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 the class and this works but I would also like to know of other ways to do this. Also are there any performance implacations of using sealed?
3
1807
by: John Baro | last post by:
I have a class as: public class a : { private float m_b; public float b { get {
6
2111
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 responsible for instantiating the orders class? Would it be the ui layer or the master class in the business layer? thanks,
16
4238
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 classes for each of my 'entities' that operate within the program, ie, passenger, vehicle, etc. Then seperate UI classes that link to these. (Partially, because eventually several add-ons will be able to reuse the code in the data classes). My...
7
357
by: Koda | last post by:
What is the difference between class fields and class properties? Thanks Mike
3
5321
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, Doctor, Employee, Contractor, etc). Now, at runtime I need to retrieve all the property values from a database and populate all of the fields of both the base and derived classes. Not all fields will need to be exposed via public properties as...
2
1460
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 classes in each class where I needed to retrieve a property or method. I discovered that this reciprocal loading of class instances led to stackoverflow exceptions. I am now remedying this problem by sharing properties among the classes. I...
2
2322
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 classes by hand (about 200 properties). I found this post on google: http://www.thescripts.com/forum/thread225825.html But how does this code work and what do I use instead of "oth" in the last line using System.Reflection; class MyClass {
0
8249
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8348
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2613
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1797
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.