Presentation is loading. Please wait.

Presentation is loading. Please wait.

User Defined Types, Collections, Enumerations & Classes CIS 338 Tony Lopez (a Cal Poly student) updated April 2003.

Similar presentations


Presentation on theme: "User Defined Types, Collections, Enumerations & Classes CIS 338 Tony Lopez (a Cal Poly student) updated April 2003."— Presentation transcript:

1 User Defined Types, Collections, Enumerations & Classes CIS 338 Tony Lopez (a Cal Poly student) updated April 2003

2 User Defined Types  Integer, String, Double, etc.  Use the ‘Let’ keyword in assignment  Can be passed as parameter to a Function or Procedure  Can be a return value of a function  Cannot be passed ByVal  Like C++ Structures

3 Creating User Defined Types  Must be created in a Module (struct)  Can use Enumerations, or other User Defined Types e.g., Public Type Student ID As String Name As String Major As Majors End Type

4 Using User Defined Types  Declare same as other data types Dim newStudent As Student newStudent.ID = "111-22-3333" newStudent.Name = "Jenny" newStudent.Major = Majors.CIS [Enumeration]

5 Get, Let, Set Review  Get – only used in Property Functions when user gets a value from the class  Let – used to assign values  Dim nIndex as Integer  Let nIndex = 0 (optional; could use following instead)  nIndex = 0  Set – used to assign Objects (required)  Dim colNames as Collection  Set colNames = New Collection

6 Accessing Private Variables  Java – getters and setters public void setName(String someName) { name = someName; } public String getName () { return name; // name declared above }

7 Accessing Private Variables  Visual Basic – Property Get, Let, Set Public Property Let Name(ByVal nuName As String) sName = nuName 'sName declared in this Sub End Property Public Property Get Name() As String Name = sName End Property Public Property Set MyObj(ByVal nuMyObject As Object) objMyObject = nuMyObj End Property

8 Properties  Allows for the familiar dot operator  sStudentName = objStudent.Name ‘ =GET  Let objStudent.Name = “Jenny”  Set objStudent.MyObject = objObject  Doesn’t violate encapsulation  Allows for code to be run in the Property Get, Let, Set functions

9 Collections  Collection – a group of items that is itself a type of object (p. 373)  Can be passed to and from Functions  Use New when declaring  Use Set when assigning  Forms, Controls collections (collect)  User Defined Collections (usercollect)

10 Collections Properties and Methods  Common to all Collections  Count property  Item method  Common to Most Collections  Add  Remove  Special collections may have other methods

11 User Defined Collections  Property and Methods  Count property  Item method  Add method  Remove method

12 User-Defined Collections  Dim col[Name] As New Collection  Loop through collection (usercollect)  For Each [type variable] In col[Name]  ‘ do something with [type variable]  Next [type variable]  For Each.. Next must use Variants or Objects (objcollect)  Use Variant for Strings

13 Enumerations  Allows you to define a group of constants as a data type (p. 372) ‘ Enumeration Declaration Public Enum Majors(types) ACC = 0 CIS = 1 EBZ = 2 FRL = 3 IB = 4 MKT = 5 MHR = 6 TOM = 7 End Enum

14 Enumerations  Works with integers  String constants won’t work  ACC = “Accounting” NG, must use ACC=0  Using the enum: Dim myMajor As Major myMajor = CIS ‘ or myMajor = Major.CIS (stored as CIS=1)

15 Classes  Use Class Module  Create instances of the class using the New keyword  Dim objStudent as New clsStudent  Combine attributes (Properties) and methods (Sub Procedures and Functions) into one object  Similar to classes in Java  Can be placed in Collections


Download ppt "User Defined Types, Collections, Enumerations & Classes CIS 338 Tony Lopez (a Cal Poly student) updated April 2003."

Similar presentations


Ads by Google