473,595 Members | 2,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is the difference between a Field and a Propery



Hi Everyone--

Please help.

What is the difference between a Field and a Property?

Here is the context.

In the VS.NET 2002 documentation, in the "String Members" section, we have
the following...

....

Public Fields

....

Public Properties

....

....so, what is the difference?

In particular, I am noticing the String.Empty field, which is read-only.

Why isn't this simply a read-only Property?

Which leads one to think that there IS a difference between a Field and a
Property, but it is not clear to me what that difference is.

Is there a catch here?

That sums up my inquiry.

Please advise.

Thank you.

--Mark

Nov 20 '05 #1
6 11240
"Mark Kamoski" <mk******@yahoo .com> schrieb
What is the difference between a Field and a Property?

Here is the context.

In the VS.NET 2002 documentation, in the "String Members" section, we
have the following...

...

Public Fields

...

Public Properties

...

...so, what is the difference?
It's explained in the VB.Net docs.

Public Field As Integer 'Field

Public Property Prop() As Integer 'Property
Get
End Get
Set
End Set
End Property
In particular, I am noticing the String.Empty field, which is
read-only.

Why isn't this simply a read-only Property?
A Field IS simpler:

'Field:
Public Shared Readonly Empty As String = ""

'Property:
Public Shared Readonly Property Empty() As String
Get
End Get
End property
Which leads one to think that there IS a difference between a Field
and a Property, but it is not clear to me what that difference is.

Is there a catch here?

--
Armin

Nov 20 '05 #2
Hi Mark,

Class SomeObject
Private ValueAsAField As Integer

Public Property ValueAsAPropert y() As Integer
Get
Return ValueAsAField
End Get
Set
If Value < 0 Then Value = 0
ValueAsAField = Value
End Set
End Property
'More code follows which uses ValueAsAField
'perhaps in string handling or array access.
: : :
End Class

A Field is an area of memory within an object that stores some
information. That's all it does. All it <can> do.

A Property is, in its simplest form, a routine which guards access to a
corresponding field. The example above for instance won't allow negative
numbers to be stored but will set it to the minimum of zero. If the Field were
Public, the user of the object could set ValueAsAField to -1 which would
presumably cause problems later, eg in accessing an array item.

A Public Field allows direct access to the data - the caller simply plucks
the value out - while a Property always requires extra code to be run and acts
(for a Private Field) as a gate-keeper.

If you have a Public Field there is no point having a Property. If you
change you mind about it being Public, you can rename it, make it Private and
create a Property with the original name. This will then do checking or
whatever.

String.Empty is a constant. As it will never change its value, it doesn't
need any Property to guard it. Whether it is implemented as a Field or a Const
is another question.

Regards,
Fergus
Nov 20 '05 #3


Hi Fergus--

Thank you very much for your thoughts. They help to clear things up.

BTW, it seems that String.Empty is NOT actually a real Constant in the
sense that one cannot write...

Public Const MyStringConstan t As String = String.Empty

....because the IDE complains that a "Constant expression is required".

But, I understand the gist of what you are saying in that the value of
String.Empty is constant (lowercase emphasis) and unchanging.

I guess what made things less than clear was the terminology-- in that
Property is a keyword and "Field" is not. And so on. I thought there was
something fundamental that I had missed, such a "Field" being a special
type of Property or something. It is good to know that that is not the
case.

Thank you very much.

--Mark
"Fergus Cooney" <fi******@tesco .net> wrote in message
news:e5******** ******@TK2MSFTN GP12.phx.gbl...
Hi Mark,

Class SomeObject
Private ValueAsAField As Integer

Public Property ValueAsAPropert y() As Integer
Get
Return ValueAsAField
End Get
Set
If Value < 0 Then Value = 0
ValueAsAField = Value
End Set
End Property
'More code follows which uses ValueAsAField
'perhaps in string handling or array access.
: : :
End Class

A Field is an area of memory within an object that stores some
information. That's all it does. All it <can> do.

A Property is, in its simplest form, a routine which guards access to a
corresponding field. The example above for instance won't allow negative
numbers to be stored but will set it to the minimum of zero. If the Field
were
Public, the user of the object could set ValueAsAField to -1 which would
presumably cause problems later, eg in accessing an array item.

A Public Field allows direct access to the data - the caller simply
plucks
the value out - while a Property always requires extra code to be run and
acts
(for a Private Field) as a gate-keeper.

If you have a Public Field there is no point having a Property. If you
change you mind about it being Public, you can rename it, make it Private
and
create a Property with the original name. This will then do checking or
whatever.

String.Empty is a constant. As it will never change its value, it
doesn't
need any Property to guard it. Whether it is implemented as a Field or a
Const
is another question.

Regards,
Fergus

Nov 20 '05 #4
Hi Mark,

You're welcome.

Lol, come back when you start reading about Asynchronous Delegates and
Reflection - documentation written for mind-readers - plenty of scope for
confusion.

The same applies to a lot of the documentation in between. ;-)

Regards,
Fergus
MVP [Windows Start button, Shutdown dialogue]
Nov 20 '05 #5

"Fergus Cooney" <fi******@tesco .net> wrote in message
news:O%******** ********@TK2MSF TNGP11.phx.gbl. ..
Hi Mark,

You're welcome.

Lol, come back when you start reading about Asynchronous Delegates and
Reflection - documentation written for mind-readers - plenty of scope for
confusion.
YES!!!!!!!!!!!! !!!!!!!!!!!!!! THIS IS TRUE!

I didn't notice this before but at the bottom of the reflection documents
and delegate documents it says

"Requires version 1.1 Framework and PHD."

Missed that last part apparently.


The same applies to a lot of the documentation in between. ;-)

Regards,
Fergus
MVP [Windows Start button, Shutdown dialogue]

Nov 20 '05 #6
BIG LOL.

Nov 20 '05 #7

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

Similar topics

3
2688
by: Pavils Jurjans | last post by:
Hello, I have bumped upon this problem: I do some client-side form processing with JavaScript, and for this I loop over all the forms in the document. In order to identify them, I read their "name" property (which sources from "name" HTML attribue). The problem is, that if the form contains form control named "name", it overwrites the form name property. In fact, I'm quite surprised that it's so easy to spoil any of the form object...
3
9566
by: Paul T. Rong | last post by:
Do "" and Null both mean nothing?¡¡ If I don't type anything in text box, the its value is Null£¿¡¡Or it is ¡°¡±£¿ I don¡¯ think they are the same, but I don¡¯t know their difference. Thanks.
13
6004
by: My4thPersonality | last post by:
I am reading something about the details of C#, and I came acros the statements readonly and const. I do not understand, it seems to be the same, what's the difference? Here is the text were it refers to: const long size = ((long)int.MaxValue + 1) / 4; Constant-Value must be computable at compile time readonly DateTime date;
3
1626
by: david | last post by:
How to set the size of a field in DataGrid? Right now the size is automatically set and determined by the size of the colunm name. Thanks David
4
32733
by: Shwetabh | last post by:
Hi, My question is, is there any difference between a NULL and a Blank (Unknown, Not Applicable) field in MS SQL or are they the same? Awaiting your comments, Regards
3
4148
by: bbawa1 | last post by:
Hi, I have a table which has a field ItemsReceived of type datetime. I have a grid view which has two columns. In first column i have to show the data from field ItemsReceived and in second column I have to show difference between Currenttime and date from ItemReceived. How can I do that.
6
1835
by: Jack | last post by:
Hi, I am still new to .NET so, i'm sorry if my question is a bit too simple :-) I would like to know what is the "best-practice-way" of sending email from asp.net (VbScript). I want to make a "contact us" page for my website. Tnx.
3
1167
by: JoeP | last post by:
Hi All, Need to check if the propery of an object exits. oMyObject.Message In some cases the Message property may not exists. I know the GetType() can do it. How would you check it?
1
2476
by: Joey Fontaine | last post by:
I just noticed that, when using intellisense, the DateTime.MaxValue field has a static property icon whereas the Decimal.MaxValue field has a constant property icon. However, when looking at the PropertyInfo data displayed by using reflection against the types, I can see no inherent differences between the properties. How does microsoft determind this difference? Or, how can I determine the difference using reflection?
0
7882
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8260
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...
0
8379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8017
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
8250
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6671
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...
0
3908
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1490
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1222
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.