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

Fields vs Properties vs Methods

I am very clear on Properties and Methods and I think I understand Fields but
not sure about this. Do I have the items marked properly in the below class?
Any clarification would be appreciated. Thanks.

Public Class MyClass

'This is a field
Public myfield as string

'This is a property
Public Property () as integer
......
end Property

'This is a method
Public Sub mysub (byval x as integer)
.....
end sub

End Class

--
Dennis in Houston
Nov 21 '05 #1
12 2993
A field is the actual data such as a string or an integer. If it's marked
public it can be accessed directly from other classes using a reference to
your class such as MyClass.MyField=10 or dim x as integer=MyClass.MyField.

A property looks to the outside world like its a field inasmuch as you can
use it to obtain a value from the class or pass some value to the class but
what actually goes on inside the class is dependent on code that runs in the
property code itself. Most often a property will be used to get or set the
contents of a private field such as:

Private _int as Integer

Public Property Int() as Integer
Get
return _int
End Get
Set(ByVal value as Integer)
_int=value
End Set
End Property

This may not look important but the trick is that some code is run whwnever
a property is obtained or set. This enables you to do such things as raise
an event when a property changes like this...

Public Property Int() as Integer
Get
return _int
End Get
Set(ByVal value as Integer)
_int=value
OnIntegerChanged()
End Set
End Property

A property doesn't have to access a field. It might just perform some
processing and return a value that was created at that moment such as:

Public Readonly Property DateString() as String
Get
return DateTime.Now.ToShortDateString()
End Get
End Property

A method just runs a chunk of code. A method may have parameters which are
passed in when the method is called. The method may return a value when the
processing is finished. The method might be overloaded, this is to say that
several methods of the same name but having different parameters might
exist. A method might be virtual or overridable which is to say that classes
derived from a certain class can change the behaviour of the method by
modifying it's definition. This is the basis of polymorphism. Methods in
Visual Basic use the Sub or Function keywords.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:CE**********************************@microsof t.com...
I am very clear on Properties and Methods and I think I understand Fields but not sure about this. Do I have the items marked properly in the below class? Any clarification would be appreciated. Thanks.

Public Class MyClass

'This is a field
Public myfield as string

'This is a property
Public Property () as integer
.....
end Property

'This is a method
Public Sub mysub (byval x as integer)
....
end sub

End Class

--
Dennis in Houston

Nov 21 '05 #2
A field is the actual data such as a string or an integer. If it's marked
public it can be accessed directly from other classes using a reference to
your class such as MyClass.MyField=10 or dim x as integer=MyClass.MyField.

A property looks to the outside world like its a field inasmuch as you can
use it to obtain a value from the class or pass some value to the class but
what actually goes on inside the class is dependent on code that runs in the
property code itself. Most often a property will be used to get or set the
contents of a private field such as:

Private _int as Integer

Public Property Int() as Integer
Get
return _int
End Get
Set(ByVal value as Integer)
_int=value
End Set
End Property

This may not look important but the trick is that some code is run whwnever
a property is obtained or set. This enables you to do such things as raise
an event when a property changes like this...

Public Property Int() as Integer
Get
return _int
End Get
Set(ByVal value as Integer)
_int=value
OnIntegerChanged()
End Set
End Property

A property doesn't have to access a field. It might just perform some
processing and return a value that was created at that moment such as:

Public Readonly Property DateString() as String
Get
return DateTime.Now.ToShortDateString()
End Get
End Property

A method just runs a chunk of code. A method may have parameters which are
passed in when the method is called. The method may return a value when the
processing is finished. The method might be overloaded, this is to say that
several methods of the same name but having different parameters might
exist. A method might be virtual or overridable which is to say that classes
derived from a certain class can change the behaviour of the method by
modifying it's definition. This is the basis of polymorphism. Methods in
Visual Basic use the Sub or Function keywords.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:CE**********************************@microsof t.com...
I am very clear on Properties and Methods and I think I understand Fields but not sure about this. Do I have the items marked properly in the below class? Any clarification would be appreciated. Thanks.

Public Class MyClass

'This is a field
Public myfield as string

'This is a property
Public Property () as integer
.....
end Property

'This is a method
Public Sub mysub (byval x as integer)
....
end sub

End Class

--
Dennis in Houston

Nov 21 '05 #3
Bob,

What a property or a method is, was not the question it goes about the
definition for "field". I do not know if there is for that a general
definition in this case. Therefore I did not answer it, curious that I was
if somebody would come with that.

And to Dennis than as a try for an answer, a field is a very old definition.
When you saw in past a record or a memory description graphical it was
always this

|Field1|Field2|Field3|etc

It is of course still a field in memory, however not so concrete as in past
where it was really forever memory_adress(length)

To describe it now I mostly "try" to use for in memory
Object or Value
In a data row description
Item

Maybe this clears it somehow

Cor
Nov 21 '05 #4
Bob,

What a property or a method is, was not the question it goes about the
definition for "field". I do not know if there is for that a general
definition in this case. Therefore I did not answer it, curious that I was
if somebody would come with that.

And to Dennis than as a try for an answer, a field is a very old definition.
When you saw in past a record or a memory description graphical it was
always this

|Field1|Field2|Field3|etc

It is of course still a field in memory, however not so concrete as in past
where it was really forever memory_adress(length)

To describe it now I mostly "try" to use for in memory
Object or Value
In a data row description
Item

Maybe this clears it somehow

Cor
Nov 21 '05 #5
"Cor Ligthert" <no************@planet.nl> schrieb:
What a property or a method is, was not the question it goes about the
definition for "field". I do not know if there is for that a general
definition in this case. Therefore I did not answer it, curious that I was
if somebody would come with that.


<URL:http://en.wikipedia.org/wiki/Field_%28computer_science%29>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #6
"Cor Ligthert" <no************@planet.nl> schrieb:
What a property or a method is, was not the question it goes about the
definition for "field". I do not know if there is for that a general
definition in this case. Therefore I did not answer it, curious that I was
if somebody would come with that.


<URL:http://en.wikipedia.org/wiki/Field_%28computer_science%29>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #7
>>Maybe this clears it somehow

Very possibly...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Cor Ligthert" <no************@planet.nl> wrote in message
news:ur**************@TK2MSFTNGP15.phx.gbl...
Bob,

What a property or a method is, was not the question it goes about the
definition for "field". I do not know if there is for that a general
definition in this case. Therefore I did not answer it, curious that I was
if somebody would come with that.

And to Dennis than as a try for an answer, a field is a very old definition. When you saw in past a record or a memory description graphical it was
always this

|Field1|Field2|Field3|etc

It is of course still a field in memory, however not so concrete as in past where it was really forever memory_adress(length)

To describe it now I mostly "try" to use for in memory
Object or Value
In a data row description
Item

Maybe this clears it somehow

Cor

Nov 21 '05 #8
>>Maybe this clears it somehow

Very possibly...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Cor Ligthert" <no************@planet.nl> wrote in message
news:ur**************@TK2MSFTNGP15.phx.gbl...
Bob,

What a property or a method is, was not the question it goes about the
definition for "field". I do not know if there is for that a general
definition in this case. Therefore I did not answer it, curious that I was
if somebody would come with that.

And to Dennis than as a try for an answer, a field is a very old definition. When you saw in past a record or a memory description graphical it was
always this

|Field1|Field2|Field3|etc

It is of course still a field in memory, however not so concrete as in past where it was really forever memory_adress(length)

To describe it now I mostly "try" to use for in memory
Object or Value
In a data row description
Item

Maybe this clears it somehow

Cor

Nov 21 '05 #9
Herfried,

When I saw this link I thought stupid from me that I did not look there,
however this gives an answer in the way I gave.

And it is still not accoording the way Dennis was really asking it in my
opinion.

Thanks anyway for pointing me on that.

Cor
Nov 21 '05 #10
Herfried,

When I saw this link I thought stupid from me that I did not look there,
however this gives an answer in the way I gave.

And it is still not accoording the way Dennis was really asking it in my
opinion.

Thanks anyway for pointing me on that.

Cor
Nov 21 '05 #11
Thanks all for answers. I kept seeing the word "Field" used in the MSDN and
was trying to figure out a definition as to what they were referring to. The
MSDN seems to use the term field loosely.

"Bob Powell [MVP]" wrote:
Maybe this clears it somehow


Very possibly...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Cor Ligthert" <no************@planet.nl> wrote in message
news:ur**************@TK2MSFTNGP15.phx.gbl...
Bob,

What a property or a method is, was not the question it goes about the
definition for "field". I do not know if there is for that a general
definition in this case. Therefore I did not answer it, curious that I was
if somebody would come with that.

And to Dennis than as a try for an answer, a field is a very old

definition.
When you saw in past a record or a memory description graphical it was
always this

|Field1|Field2|Field3|etc

It is of course still a field in memory, however not so concrete as in

past
where it was really forever memory_adress(length)

To describe it now I mostly "try" to use for in memory
Object or Value
In a data row description
Item

Maybe this clears it somehow

Cor


Nov 21 '05 #12
Thanks all for answers. I kept seeing the word "Field" used in the MSDN and
was trying to figure out a definition as to what they were referring to. The
MSDN seems to use the term field loosely.

"Bob Powell [MVP]" wrote:
Maybe this clears it somehow


Very possibly...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Cor Ligthert" <no************@planet.nl> wrote in message
news:ur**************@TK2MSFTNGP15.phx.gbl...
Bob,

What a property or a method is, was not the question it goes about the
definition for "field". I do not know if there is for that a general
definition in this case. Therefore I did not answer it, curious that I was
if somebody would come with that.

And to Dennis than as a try for an answer, a field is a very old

definition.
When you saw in past a record or a memory description graphical it was
always this

|Field1|Field2|Field3|etc

It is of course still a field in memory, however not so concrete as in

past
where it was really forever memory_adress(length)

To describe it now I mostly "try" to use for in memory
Object or Value
In a data row description
Item

Maybe this clears it somehow

Cor


Nov 21 '05 #13

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

Similar topics

7
by: Koda | last post by:
What is the difference between class fields and class properties? Thanks Mike
0
by: Boniek | last post by:
If exists any specification about names (in subject) ? I mean spefication in C#. I often see names, like that: fields m_(short of type)NameFields local variables (short of type)NameVariable but...
3
by: Sam Sungshik Kong | last post by:
Hello! While using panel control, I wondered a thing. Panel class is subclass of Control class. Control class has KeyPress event and Focus() method, etc... Then Panel class must have them. I...
9
by: keithv | last post by:
I'm looking at a bunch of code which has lots of class fields converted into properties with just the simplest code: private int empID; public int empID { get { return empID; } set { empID =...
26
by: julien | last post by:
Hello, I don't know when to use fields and when to used properties. It looks to me that using properties is always better. But I guess fields must be better in some cases, otherwise they wouldn't...
5
by: Genboy | last post by:
My "VIS" Website, which is a C# site created in VS.NET, Framework 1.1, is no longer compiling for me via the command line. As I have done 600 times in the last year and a half, I can compile to...
0
by: Dennis | last post by:
I am very clear on Properties and Methods and I think I understand Fields but not sure about this. Do I have the items marked properly in the below class? Any clarification would be appreciated. ...
60
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
2
by: Jon Slaughter | last post by:
I was wondering if maybe allowing "fields" for methods. The reason is to encapsulate the data that is mainly used by the method and to prevent the need of having to create new variables every time...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
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,...
0
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...
0
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...

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.