1.02k likes | 1.15k Views
Teaching an Introductory Programming Class. Using Visual Basic .NET. We are. Anita C. Millspaugh Professor, Computer Information Systems Mt. San Antonio college Julia Case Bradley Professor Emeritus, Computer Information Systems Mt. San Antonio College Bradley/Millspaugh texts:
E N D
Teaching an Introductory Programming Class Using Visual Basic .NET
We are... Anita C. Millspaugh Professor, Computer Information Systems Mt. San Antonio college Julia Case Bradley Professor Emeritus, Computer Information Systems Mt. San Antonio College Bradley/Millspaugh texts: VB 4, VB 5, VB 6, Advanced VB, Java and the new VB .NET Spring 2002
You… Teach Visual Basic 6? Are planning to teach Visual Basic .NET? Have taught an OOP language such as Java, SmallTalk, or C++? Spring 2002
Our Goals for Today • Help you transition from VB 6 to VB .NET • Look at the differences between Visual Basic 6: object based; event driven Visual Basic .NET: object oriented • Introduce the new Visual Studio IDE (integrated development environment) • Show the changes to the language and the new features Spring 2002
Our Approach • This is an introductory programming course • Main topics: • Program planning • Programming logic • Variables, constants, calculations, decisions, loops, data files • Language syntax • Coding standards • Testing and debugging • The VB .NET course still covers these topics, with OOP concepts embedded throughout. Spring 2002
What is the .NET Framework? • Platform for building and running applications • Designed for cross-platform compatibility • Common Language Runtime (CLR) • Share methods and objects from different languages • Many languages to choose from — VB, C#, COBOL, Fortran, J# … Spring 2002
The Biggest Change in VB .NET — OOP • A new mind set • New keywords: Inherits Overrides Overloads • Terminology introduced in Chapter 1 • Concepts covered in Chapter 6 Spring 2002
The Second Biggest Change — Web Forms • Programming for the Web vs. Windows • New set of controls • Client side vs. server side • Web page layout • Very simple projects presented in Chapter 9 Spring 2002
The Third Biggest Change — Data File Handling • Database uses ADO .NET (Chapter 10) • No Data control • “Simple” program is more difficult • Binding is more robust and likely to be used by “real programmers” • Must write code for navigation • Simple data files use streams (Chapter 11) Spring 2002
Chapter 1 — IntroductionTopics Covered • Objects, Methods, and Properties • The Visual Studio IDE • Planning a Project • Creating a Project • Labels and Buttons • Code • Syntax and Logic Errors • Help Spring 2002
Chapter 1 — Changes • New IDE Spring 2002
Chapter 1 (continued) • Terminology VS IDE Solution Explorer Form Designer Editor window • Tabbed display is the default for the IDE • AutoHide toolbox; can be “pinned” open Spring 2002
Chapter 1 (continued) PushPin Tabbed Window Locked Control Spring 2002
Chapter 1 (continued) • A solution can contain multiple projects • Forms, classes, and code modules have a .vb extension • The IDE automatically creates a folder for the project Spring 2002
Chapter 1 (continued) • Form Designer • Similar to VB 6 • Double-Click a control in the toolbox: • New default-sized control • Appears on top of a selected control • Top-left of the form if no control is selected • Locked controls have a dark border when selected • Command Buttons are now Buttons • Use “btn” for prefix • No more Caption property — now it is Text Spring 2002
Chapter 1 (continued) • The Editor window (formerly the Code window) • Lots more IntelliSense help; can be confusing • Declarations section replaces General Declarations • Class List and Method List - Selecting the object and event is somewhat different • Collapsible Regions in code Spring 2002
Chapter 1 (continued) Tabs Method List Class List Collapsed Region Collapsed Procedure Spring 2002
Chapter 1 (continued) • Complications: • Changing the name of the form requires the project’s startup object to be changed • PrintForm is no longer supported • IDE Print does not offer Form Image or Form Text • No edit-and-continue execution – Must restart (recompile) if edits are made during break time Spring 2002
Chapter 1 (continued) • Form and controls no longer have Caption property • Forms, buttons, labels all use the Text property • The Alignment property becomes TextAlign • The maximum length of identifiers is 16,383 • All new Help — MSDN • Redesigned Object Browser • Me.Close() Spring 2002
Chapter 2 — More ControlsTopics Covered • Check Boxes • Radio Buttons • Group Boxes • Picture Boxes • Designing an Interface • Setting the Tab Order • Setting up keyboard access keys (shortcuts) Spring 2002
Chapter 2 — Changes • No more Image control, Line control, or Shape control • Use PictureBox for graphics; Label for lines • OptionButton becomes RadioButton • Frame becomes GroupBox • AcceptButton and CancelButton are properties of the form • Set the form’s StartPosition property; no more Form Layout Window Spring 2002
Chapter 2 (continued) • PictureBox controls • Use Image property to hold a graphic • Set SizeMode property to StretchImage — replaces Stretch property of Images • CheckBox controls • Value property becomes the Checked property (Boolean) • It’s event is CheckChanged • Great new method for setting tab order Spring 2002
Chapter 2 (continued) View / Tab Order Spring 2002
Chapter 2 (continued) • New component tray holds non-visible controls • To add ToolTips, add a ToolTip control to the component tray • New ToolTip on ToolTip1 property is added to the form and all controls • Text boxes have a Clear method txtName.Clear() txtName.Text = “” • Focus method replaces SetFocus txtName.Focus() Spring 2002
Chapter 2 (continued) • Many colors available through the Color class lblMessage.ForeColor = Color.Blue Color.Red Color.Yellow Color.Aquamarine Color.Bisque Color.Chocolate Color.Cadetblue … and dozens more Spring 2002
Chapter 3 — Variables,Constants and CalculationsTopics Covered • Variables, constants, calculations • Data types • Converting text input to numeric • Formatting output • Error Handling (Exceptions) • Message Boxes Spring 2002
Chapter 3 — Changes • Strongly Typed Data • Option Strict On – Enforces strong data typing • Data Types Short (old Integer) Integer (old Long) Decimal (Currency is gone) Object ( Variant is gone) Date (replaces DateTime) Spring 2002
Chapter 3 (continued) • Initialize a variable at declarationDim intMax As Integer = 100I Dim decRate As Decimal = 0.08D • Declare multiple variables at onceDim intCount, intNumber As Integer • Convert all input to correct data type (Do not use Val function) decSale = CDec(txtSale.Text) • CDec and CInt — Can parse: $ , () + - • CInt rounds to nearest EVEN number Spring 2002
Chapter 3 (continued) • New assignment Operators += –= *= /= &= decTotal += decSale • Structured Exception Handling Try (Code that could cause an exception) Catch [excName As excType] (Code to handle the exception) Finally (Code to execute in any case) End Try • We can catch input errors before covering If statements! Spring 2002
Chapter 3 (continued) • MessageBox class MessageBox.Show(Arguments) • Several choices for argument list • Introduces concept of Overloaded functions • Multiple signatures to choose from MessageBox.Show(TextMessage) • MessageBox.Show(TextMessage, TitlebarText) • MessageBox.Show(TextMessage, TitlebarText, _MessageBoxButtons) • MessageBox.Show(TextMessage, TitlebarText, _MessageBoxButtons, MessageBoxIcon) Spring 2002
Chapter 3 (continued) • MessageBox Icons and buttons • Enumeration of constants Spring 2002
Chapter 4 — Decisions and ConditionsTopics Covered • Conditions / Relational Operators • Block Ifs • Compound Conditions • Validation • Message Boxes that return the user response • Debugging Spring 2002
Chapter 4 — Changes • Block IF unchanged • Conditions — always compare like data types If CInt(txtInput.Text) > 10I Then • String ToUpper and ToLower methods (replace UCase and LCase functions) If txtInput.Text.ToUpper = “YES” Spring 2002
Chapter 4 (continued) • MessageBox.Show method returns an object of type DialogResult • Dim dgrResponse As DialogResult • dgrResponse = MessageBox.Show(”Shall we?", _ • "The Question", MessageBoxButtons.YesNo, _ • MessageBoxIcon.Question) • If dgrResponse = DialogResult.Yes Then • MessageBox.Show("You said Yes!") • Else • MessageBox.Show("You said No!") • End If Spring 2002
Chapter 4 (continued) • Replace vbCrLf, vbCr, vbLf with constants in the ControlChars class ControlChars.NewLine Dim strMultilineMessage As String strMultilineMessage = “Line 1” & _ ControlChars.NewLine & “Line 2” Spring 2002
Chapter 4 (continued) • Calling a procedure requires parentheses VB 6 Way: ClearControls orCall ClearControls() VB .NET Way: [Call] ClearControls() • Calling an event procedure requires two arguments • Private Sub btnExit_Click(ByVal sender _ As System.Object, ByVal e _ As System.EventArgs) Handles btnExit.Click • Call the event procedure: [Call] btnExit_Click(sender, e) Spring 2002
Chapter 4 (continued) • Debug.WriteLine (Replaces Debug.Print) Debug.WriteLine(“I got this far”) • Clear the Output Window: Right-click / Clear All • New Autos window — shows current value of variables and objects within approx. 6 statements • Immediate window only valid at run time • Keyboard shortcut for Step Into — F11 Spring 2002
Chapter 5 — Menus, Sub Procedures, and Sub FunctionsTopics Covered • Creating Menus and Menu Items • Creating ContextMenus • Displaying Common Dialog Boxes • Writing General Procedures Spring 2002
Chapter 5 — Changes • Create a menu by adding a MainMenu control to the component tray • Great new Menu Designer replaces the Menu Editor Spring 2002
Chapter 5 (continued) • For Context menus, add a ContextMenu control • A ContextMenu uses the same Menu Designer as a MainMenu • Attach a context menu by setting the ContextMenu property of a control or the form Spring 2002
Chapter 5 (continued) • New ColorDialog and FontDialog controls • Added to the component tray • Use the control’s Show method to display • Can set and retrieve properties of the control • With dlgColor • .Color = frmMain.BackColor • .ShowDialog() • .frmMain.BackColor = .Color • End With Spring 2002
Chapter 5 (continued) • The FontDialog control • 'Change the font for the total label • With dlgFont • .Font = lblTotal.Font • .ShowDialog() • lblTotal.Font = .Font • End With Spring 2002
Chapter 5 (continued) • Arguments are passed ByVal (by default) • The Editor adds ByVal if you leave it out • New Return statement • Can still set function name to value • Better to use the Return statement Private Function CalculateSum( _ ByVal intNum1 As Integer, _ ByVal intNum2 As Integer) As Integer 'Calculate the sum of the numbers Return intNum1 + intNum2 End Function Spring 2002
Chapter 6 — OOPTopics Covered • Object-oriented terminology • Multitier applications • Class vs. Object • Visibility — Public, Private, Protected • Constructors • Instantiation • Inheritance Spring 2002
Chapter 6 — Changes • Namespaces • Organize class libraries • Provide unique naming region • Can create your own namespace • Specifying namespaces lblMessage.Font = New System.Drawing.Font( _ "Arial", 12) or Imports System.Drawing.Font ... lblMessage.Font = New Font("Arial", 12) Spring 2002
Chapter 6 (continued) • Classes vs. Objects • Use New keyword to instantiate • For exception handling, declare the name at the module level and instantiate in a Try/Catch block • Encapsulation • Classes hide implementation details • Only the Public properties and methods can be seen by other classes Spring 2002
Chapter 6 (continued) • Inheritance • New class based on existing class Spring 2002
Chapter 6 (continued) • Inheritance (continued) • All public properties and methods are inherited • Visual inheritance — can inherit a form • New keywords to support inheritance • Inherits • Overrides • Overridable Public Class StudentBookSale Inherits BookSale Spring 2002
Chapter 6 (continued) • Inheritance (continued) • In base class Public Overridable Function _ ExtendedPrice() As Decimal • In derived class Overrides Function ExtendedPrice() _ As Decimal Spring 2002
Chapter 6 (continued) • Polymorphism • Consistent naming, so that many classes can have the identically-named methods that perform an action appropriate for the object • Example: The Add method for collections, lists, arrays, database • Identically named methods in a class, with different implementations based on the arguments • Keyword: Overloads • Example: MessageBox.Show method, which has many different argument lists (signatures) Spring 2002