By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,442 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,442 IT Pros & Developers. It's quick & easy.

Fields vs Properties vs Methods

P: n/a
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
Share this Question
Share on Google+
12 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
>>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

P: n/a
>>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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.