Fields vs Properties vs Methods

G

Guest

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
 
B

Bob Powell [MVP]

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.
 
C

Cor Ligthert

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
 
C

Cor Ligthert

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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top