Home

Introduction to Classes

 

Classes Fundamentals

 

Introduction

We saw how to lay the foundation of an object by defining its structure. Here is an example:

Structure Rectangle
    Dim Length As Double
    Dim Height As Double
End Structure

Another type of structure is created using the Class keyword. Here is an example:

Class Rectangle
    Dim Length As Double
    Dim Height As Double
End Class

Notice that, at this time, only the keyword is different. In reality there are many more differences.

Creating an Object With Class

We saw that, before using an object of type Structure, you must declare a variable for it. This rule also applies to a class. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">
Class Rectangle
    Dim Length As Double
    Dim Height As Double
End Class
</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim Rect As Rectangle
%>

</body>
</html>

The computer uses two types of memory: the stack and the heap. All the variables we have declared so far go into the stack memory and they are called value variables. When you declare a variable from the types we have seen so far (Byte, Short, Integer, Structure, etc type). The computer automatically reserves (allocates) memory for that variable in the stack. On the other hand, if you want to create an object that is of a class type, you must explicitly request that memory be allocated for the variable. To do this, when declaring the variable for the class, to reserve the necessary area of memory, use the New operator followed by the name of the class and assign this expression to the variable. Here is an example:

<%
    Dim Rect As Rectangle
    Rect = New Rectangle
%>

You can also assign the allocation after the declaration. Here is an example:

<%
    Dim Rect As Rectangle = New Rectangle
%>

Instead of first declaring the variable before allocating memory for it, you can take care of this directly when declaring the variable by inserting the New operator between the As keyword and the name of the class. This can be done as follows:

<%
    Dim Rect As New Rectangle
%>
Author Note In C++ and Visual Basic, you can omit or add parentheses to the name of the class when allocating memory for it, like this:
<%
    Dim Rect As Rectangle
    Rect = New Rectangle()
%>

or like this:

<%
    Dim Rect As New Rectangle()
%>

Everything we reviewed for the structure is also applicable here:

  • To access the member of a class, type the name of the variable, followed by a period, followed by the name of the member you want. The member you are trying to use must be part of the class. If you are the Code Editor in Microsoft Visual Web Developer or Microsoft Visual Studio, the Intellisense will assist you
  • To access the members of an object, you can include the code in a With VariableName and End With section
 
 
 
 

Objects Members and Access Modifiers

 

Introduction

When you create a class, you can exercise control on how its members can be accessed outside.

The Friendly Members of a Class

A member variable of a structure class is referred to as a friend if it can be accessed by any class of the source file. Classes outside of its source file cannot access such a member. To create such a member variable, precede it with the Friend keyword. Here is an example:

Structure Circle
    Friend Radius As Double
End Structure

The Private Members of a Class

A member of a structure or class is referred to as private if it can be accessed only by other members of the same structure or class. To mark a member as private, precede it the Private keyword. Here is an example:

Public Structure Point
    Dim x As Integer
    Private y As Integer
End Structure

The Public Members of a Class

If you want a member of a structure or a class to be accessed outside of its source file, you must make that member public. To do this, precede the member with the Public keyword. Here is an example:

Public Structure Point
    Public x As Integer
    Dim y As Integer
End Structure

We mentioned that one of the differences between a structure and a class is that the latter must be declared using the New operator. Another difference has to do with access levels. By default, all members of a structure are public. This means that, if you omit the access level on a member of a structure, it is automatically made public and can be accessed outside the source file. By default, all members of a class are private. This means that if you create a member of class using the Dim keyword, the member is made public. If you want the member of a class to be accessible outside the class, mark it as public.

Classes and Access Modifiers

 

Introduction

You can exercise control on the access of a structure or class. You can make a structure or class friendly, private, or public.

A Private Object

A structure or a class is referred to as private if it belongs to another structure or class. In other words, such a structure or class must be created inside another structure or class. To create a private structure or class, precede it with the Private keyword. Here is an example:

Public Class Rectangle
    Dim Length As Double
    Dim Height As Double

    Private Class Box
        
    End Class
End Class

A Friendly Class

A structure or a class is referred to as friendly if it can be accessed by other structures and classes of the same source file by not outside that source file.

To create a friendly class, precede it with the Friend keyword. Here is an example:

Friend Class Square
    Public Side As Double
End Class

A Public Class

A structure or a class is referred to as public if it can be accessed outside its source file. To create such a structure or class, precede the Structure or Class keyword with Public when creating it. Here is an example:

Public Structure Circle
    Friend Radius As Double
End Structure
 
 
   
 

Previous Copyright © 2009-2010 FunctionX Next