Lecture 12 – Structures
Outline
Last lecture, we discussed Enumerations…
Which allow us to place limits on our primitive data types…
We also took a brief look at Objects,
which combine:
Via the Enum Keyword.
Attributes (via Members and Properties) Behaviors (via Methods)… z to form a complex data type,
With a closer look at the special (unique) object we’ve been using: Form1.
In this Lecture, we introduce Structures:
Which allow us to combine several primitive Data Types.
Into a single, newly-defined Type.
Introduction
With Structures, we may combine…
Public members, methods, and properties
Note that members may be of different data types…
Private members and methods.
Although similar to Classes, Structures are a bit different:
First of all, they use a different method of instantiation…
Objects are reference types:
i.e., creation of an instance. To instantiate (make) a new instance , the New keyword is required. For instance: Form1 is made for us, automatically.
Structures, on the other hand, are value types:
To make a new Structure instance, New is not required. z
Ex: We can use the Dim keyword to declare an instance...
This is just like primitive data types (Integer, Double, etc).
Structures and Classes may be built in a similar manner…
Via a separate Class file (*.vb)...
Building a Structure
Structures are often defined separately from the GUI (Form1):
In a separate Class File ( e.g., Customer.vb).
However, a Structure could instead be defined at the top of Form1.
Much as we defined our DayAction Enumeration, last time.
Note: We could also have defined our DayAction…
This is to enhance clarity, reusability, and portability. We will do this, today.
In its own DayAction.vb file (note the matching name).
To define a VB Structure, we use the syntax: Structure Member declarations, properties, and methods
End Structure
After defining our Structure, we may declare a new instance…
Via the Dim keyword, or alternately as a Member of Form1.
We will now discuss this more clearly, by example…
At this point, Open Visual Studio .NET…
And create a new Windows Application , called ‘Structure Demo’.
Example: The Customer Structure
Example (Cont):
three
Example (Cont):
Example (Cont):
Adding Properties to Structures
Like Classes, Structures are defined to have ‘Members’ …
These express structure characteristics (may be Public or Private) In our last example, we accessed Structure Members directly…
Via the dot ( . ) operator, on a one-to-one basis…without abstraction.
However, Members may be handled indirectly via Properties.
These ABSTRACT-AWAY Member details.
We saw this for Classes, last Lecture.
We may also define Properties for Structures.
Public Property prop_Name() As Get_DataType Get ‘method to return the value of Property Statements … End Get Set ‘method to set the value of Property Statements … End Set End Property The Get and Set methods are both accessed via the DOT OPERATOR.
Example: Adding Properties