Sie sind auf Seite 1von 10

Lecture 12 – Structures

Outline
“ Last lecture, we discussed Enumerations…
“ Which allow us to place limits on our primitive data types…
“ Via the Enum Keyword.
“ We also took a brief look at Objects,
“ which combine:
“ 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…
“ i.e., creation of an instance.
“ Objects are reference types:
“ 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).
“ This is to enhance clarity, reusability, and portability.
“ We will do this, today.
“ 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…
“ 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

Das könnte Ihnen auch gefallen