430 likes | 601 Views
Prototyping for HCI. Spring 2004 (Week 8) Jorge A. Toro. The Language. Custom Classes. Custom classes. VB.NET is Object-Oriented So far, you have handled pre-defined classes (Form, Button, Integer, String, etc…) -but-
E N D
Prototyping for HCI Spring 2004 (Week 8) Jorge A. Toro
The Language Custom Classes
Custom classes • VB.NET is Object-Oriented • So far, you have handled pre-defined classes (Form, Button, Integer, String, etc…) -but- • You can also create your own classes that can ease the coding and fit the needs of your prototype
Custom classes • How do you know when you need a custom class? • You need to do some design decisions on what kind of classes you will need. • There is no golden rule in this, many different classes can be created and used, it all depends how you want to architect your code.
Custom classes • When you are writing the code inside a form, you are actually writing the code for the Form’s class…
Your form is defined as a class Your form
Custom classes • Creating a custom class • Custom classes are created in separate files • Same as different forms are in separate files • To add a custom class file • File -> Add New Item… • Select Class, give a name to it • Done.
2 1 Select Class 2Name the Class
This code is automatically generated. Here is where you write the code to define the properties and methods of your class. 3 The class file appears in your Solution Explorer window
Custom classes • Defining properties for the class • One way: Declare global variables as Public. PublicClass Member Public p_id AsString Public p_lname AsString Public p_fname AsString Public p_address AsString Public p_city AsString Public p_state AsString Public p_zip AsString EndClass Properties
Custom classes • Using your custom class • You create instances (objects) of the class • Use the New keyword, the same way you use it to create forms.
Form1 class You now can declare Member objects
Custom classes • Everything declared with either Dim or Private will not be accessible from outside the class.
Member Class p_ssn is not declared Public. It will be accessible only inside this class.
Form1 Class x is an instance of Member class p_ssn is not accessible outside the class, it was not declared Public.
Custom classes • Defining methods • All the subs and Functions declared Public are considered methods and are accessible outside the class.
Member Class ResetNames is a Sub declared Public. It will be accessible outside.
Form1 Class ResetNames is accessible.
Custom classes • Sometimes, you want your object to start with some preset values in some of its properties. • Constructor • Used to provide values to the properties when an instance is created • You can have different constructors for different situations • A constructor is always declared as Public and it is always named New
Constructor1 (default) Constructor2(custom)
constructor1 is used here constructor2 is used here
The Language Modules
Modules • Modules are files where you can write code that does not belong to any particular form. • Global functions • Global Subs • Global variables • Global variables declared in a Module are global to all the project.
1 Select Module 2Name the Module
This code is automatically generated. Here is where you write the code. The module file appears in your Solution Explorer window
Modules • Sub Main • You can set the Startup Object of your project to a special sub named Main • You create the Sub Main inside a Module file.
mainForm is a global variable mainForm is created mainForm is shown
Modules • In the previous example, if you set your startup object of your project to be the Sub Main, you have to create the instance of Form1 and show it. • This is useful when you have a prototype with many form files.
The Language Class Libraries
Class libraries • A separate project for creating classes for use in other applications • This is what you will get from me • A project with some classes inside
Class libraries • Using the library in your prototype • (1/2) Add the library into your prototype’s project
Class libraries • Using the library in your prototype • (2/2) Reference the class library in your project so you can start using the classes • Why do you have to do this? • Because the classes I wrote are inside a separateproject, they are not part of yours.
1 Select your project 2Select “Add Reference”
1 Click over the Projects tab 2 Select the Utils Project 3 Click Select
The library will appear here Click Ok
The Utils library will appear listed under the References folder in your project