472,345 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,345 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 11097
"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...
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...
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,...
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 ...
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? ...
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...
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...
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...
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

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.