Obects are a big part of .NET. In this 2 part article series, Craig shows us how to use objects with ASP.NET, focusing mainly on providing clear working examples.
Using Objects in ASP.NET: Part 1/2 - Class Members (Page 3 of 6 )
To get more granular, classes themselves are made up of members. Members generally consist of the class’ data (or fields) and its functionality (or methods). Both C# and VB (and presumably nearly all .NET languages) build classes using constructs below.
Constructors – Constructors are procedures that run when a class is instantiated as an object using the new statement mentioned above. They are useful for initializing values in fields or properties (see below) or running any other code that needs to run when an object is instantiated. A constructor is defined as follows:
Public Sub New() ... 'Code to make the Constructor do something End Sub
Since constructors cannot return values, the VB.NET compiler will return an error if you try to define a Constructor as a Function rather than a Sub.
· Fields – While variables can be declared anywhere, fields are variables declared at the beginning of the class definition. A variable declared as a field is accessible from anywhere in that class. Variables declared within a function or procedure, are only accessible within that function or procedure. Furthermore a variable can be a simple value such as a character or integer, or a more complex value such as another object. To define a public field in VB.NET you would use the following:
Public FieldName as ValueType ... End Sub
Fields are accessed from outside of your class using the following syntax:
Value = ObjectName.Fieldname
ObjectName.Fieldname = NewValue
· Methods – Functions and Procedures in VB are methods. They are defined within a class and therefore a member of that class.
Public Sub MethodName() ... End Sub
or to write a function that will return a value:
Public Function MethodName() As ValueType ... return newvalue End Function
If you are used to functions in VB6, you should be aware of the syntax change here. VB.NET now uses the return statement like Java, Jscript and C# to return a value.
· Properties – A good analogy for Properties might be to consider them a "hybrid": a cross between a field and a method. If you have used properties in VB 6, they haven’t changed that much. When defined within your code, properties can perform any code that a method can and usually (although not necessarily) encapsulate a matching field within the class. However, when used from another class, properties are accessed as if they are standard fields. This enables the developer writing the properties to ensure that the fields they manipulate never get set to values that would cause errors or data loss within your software. A "Get" block in the property definition is created with code that executes when the property is read. Then a "Set" block is created to define what values in the class change when the property is written to. In their simplest form, properties are defined in the manner below. But it is important to note that within the Get and Set blocks, and code can be written to make the properties do something.
Public Property PropertyName() As ValueType Get Return Value End Get
Set(ByVal Value As ValueType) FieldName = Value End Set End Property
When the properties are read from, they are accessed as if you are reading a field from an object:
SomeValue = MyObject.Property
Similarly, when written to, the property is accessed like so:
MyObject.Property = NewValue
Be aware that in VB.NET, unlike VBScript and VB6, there are not default properties. If you refer to an object in your code, there is no default property that will return a value – you’ll be changing or reading the reference to that object.
Member Accessibility You’ll notice in the fields, methods and properties above, I used the keyword Public. This means that the member being defined is accessible, that is can be read from and/or written to, from another class or even another application. However, there are many different types of access in .NET that you can define for a class member. To make the class easier to use or prevent errors, you can limit your members in various ways:
Public – At this level of access, the data member can be accessed from outside the class by any other class.
Protected – A protected member can only be accessed by members within the same class, or by a derivative of that class using Inheritance. We’ll get to inheritance and protected methods later on.
Friend – A member declared as Friend can be accessed by any other class in the same project.
Protected Friend – the best of the previous two access levels, protected friend means the member can be accessed by the same class, a derivative class or any classes in the same project.
Private – This is the most restrictive access you can define within a class. A member defined as Private can only be accessed within that class. If you define a member using only the Dim statement, the member is considered Private. However, it is a better practice to use Private instead of Dim to declare these types of members and it is easier to read and understand.