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.