473,320 Members | 1,722 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,320 software developers and data experts.

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 11218
"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 ValueAsAProperty() 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 MyStringConstant 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**************@TK2MSFTNGP12.phx.gbl...
Hi Mark,

Class SomeObject
Private ValueAsAField As Integer

Public Property ValueAsAProperty() 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%****************@TK2MSFTNGP11.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
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...
3
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
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...
3
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
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
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...
6
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...
3
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
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.