Upload
philomena-booker
View
215
Download
0
Embed Size (px)
Citation preview
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms
Visual Basic Study #1
April 01, 2004
Nam, Sedong ([email protected])
Visual Basic Study Group
SPARCS (http://sparcs.org) / KAIST (http://www.kaist.ac.kr)
그림 : http://www.brotherblotz.com/interlaken_materials.htm
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms 세미나 안내
• 이 세미나는 같이 공부 하는 세미나– 발표자도 잘 모름 , 먼저 조금 봤을 뿐
• 질문은 바로 바로– 발표자 말을 끊고서라도 바로 바로 !
– 답변이 길면 끝난 후에
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms 타이포그래피 안내
• 글자 색– 중요한 것– 알아야 할 용어– 잘 이해가 안 된 것– 궁금한 것
• Code uses this font, Lucida Console
표 머리이것저것 표는 이런 색으로 그림
‘ Source Code are in this box
Execution Command are in this box
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms 목차
• Same as the Textbook
1. Introduction
• What Is the Microsoft .NET framework?
• What Is Visual Basic .NET?
• An Example Visual Basic .NET Program
2. The Visual Basic .NET Language
• Source Files
• Identifiers
• Keywords
• Literals
• Namespaces
• Symbolic Constants
• Variables
• Scope
• Access Modifiers
• Assignment
• Operators and Expressions
• To be continued
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Introduction
• VB .NET
– Is fully object-oriented
– Have full access to the .NET framework
– Application run within .NET common language runtime
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms What is the Microsoft .NET Framework?
• A new way to expose operating system and other APIs
– Uniform across all languages (C++, VB, ASP.NET, …)
• A new infrastructure for managing application execution
– Common Language Runtime (CLR)
– Common Type System (CTS)
– Common Language Specification (CLS)
– Microsoft Intermediate Language (MSIL), JIT
• A new web server paradigm
• A new focus on distributed-application architecture
– Internet in mind
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms What Is Visual Basic .NET?
• In brief, better than VB6 (--;)
• Significant departure from previous generations
– VB6 code can’t be compiled by the VB.NET compiler
– Migration tool (of course, not perfect)
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Hello, World
• Edit: Editor such as Window’s Notepad (Vim)
• Save: Hello.vb (Any filename ends with .vb)
• Compile:
• Execute:
Show!
Imports System
Public Module Hello Public Sub Main Console.WriteLine("hello, world") End SubEnd Module
vbc Hello.vb
Hello.exe
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Hello, World: 첫인상 ( 교재 외 )
• ‘;’ 없군– 줄 단위 파싱– 한 줄로 해야 할 것을 두 줄에 나누어 쓸 때는 ‘ _’ 사용
• ‘{’ 없네– End (Next, Loop, …)
– 인덴테이션은 상관 없음 (Python?)
• Java 비슷하군– Imports
– 클래스명 첫 문자를 대문자로 ( 하지만 메소드 명까지도 대문자네 )
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Hello, World: The Imports statement
• Imports
– Is merely a convenience
– Imports Namespace
• Same code without Imports:
‘ Imports System
Public Module Hello Public Sub Main System.Console.WriteLine("hello, world") End SubEnd Module
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Hello, World: The Main
• Main
– 1 Main in 1 Application• More than 1: compiler option:
• Main Code in Class:
– Shared = Static in Java
/main:location
Imports System
Public Class Hello Public Shared Sub Main Console.WriteLine("hello, world") End SubEnd Class
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Source Files
• All source files: .vb extension
• The source files of declarations: unimportant
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Identifiers
• Identifiers: given names to– Namespaces
– Types• enumerations, structures, classes, standard modules, interfaces,
and delegates
– Type members• Methods, constructors, events, constants, fields, and properties
– Variables
• Characters– First: Alphabetic or “_”
– Others: Alphanumeric or “_”
– Case insensitive• But, case sensitive when called from case-sensitive language
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Keywords
• Keywords: words with special meaning in a VB .NET
Keyword DescriptionAddHandlerOf Visual Basic .NET Statement
AddressOf Visual Basic .NET Statement
Alias Used in the Declare statement
And Boolean Operator
AndAlso Boolean Operator
Ansi Used in the Declare Statement
Append Used as symbolic constant in the FileOpen function
As Used in variable declaration (Dim, Friend, etc)
…
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Literals (1/4)
• Literals: representation of values
• Numeric
– Integer types• Integer (Default, ~2147483648)
• Long (~9223372036854775807)
• Byte, Short
– Floating Point types• Single, Double (Default), Decimal
x = y * 3.14xd = y * 3.14D
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Literals (2/4)
• String
– Quotation mark in String Literals: type twice
• Character
– Strings and Characters are distinct types
– Next code: compile time error if Option Strict is On:
Sonsole.WriteLine(“So then Dave said, “”hello, world””.”)
Dim MyChar as CharMyChar = “A”c
‘ WrongDim MyChar as CharMyChar = “A”
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Literals (3/4)
• Date
– Format: m/d/yyyy• always (regardless of regional setting)
• Nothing– Nothing: uninitialized value of any type
– Numeric: 0 or 0.0, String: “”, …
– Reference: null?
Dim MyChar as DateMyDate = #11/15/2001 3:00:00 PM#
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Literals (4/4)
• Summary: All literals
Data type Literal
Boolean True, False
Char C
Date # #
Decimal D
Double Any floating point or R
Integer Any integer (~2,147,483,648) or I
Long Any integer (~9,223,372,036,854,775,807, outside of I) or L
Short S
Single F
String “ “
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Literals: Types (1/2)
• Fundamental Types
– Alias of .NET types
– No type-compatibility issues with other languages
– Base: Object
– Numeric: Short, Integer, Long, Single, Double, Decimal
– Others: Byte, Char, String
– Byte: no literal representation
Dim x As IntegerDim y As System.Int32If x.GetType() Is y.GetType() Then
Console.WriteLine(“They’re the same type.”)End If
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Literals: Types (2/2)
• Values types and Reference types
– Value types• All .NET types that derived from System.ValueType
• Structure and Enum
– Reference types• Object, String
• All types declared with Class, Interface, Delegate
• All .NET types that don’t derive from System.ValueType
• Custom Types
– VB.NET provides rich syntax
– For both new value types and new reference types
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Literals: Arrays (1/2)
• Declaration
– Array of above example has indexes 0,1,2,3, and 4
• Array type– Implicitly inherit from .NET framework’s Array type– Have methods: GetLowerBound, …– Upper bound of the array is dynamic
• Access array elements by the index
• Initial value of elements: default value of that type
Dim a(4) As Integer
For i=0 To 4 Console.WriteLine(a(i))Next
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Literals: Arrays (2/2)
• Initializing arrays
• Dynamically allocating arrays
Dim a() As String {“First”, “Second”, “Third”}Dim b(,) As Integer {{1,2}, {3,4}, {5,6}}
Dim a() As Integera = New Integer(4) {1, 2, 3, 4, 5}Dim b() As Integerb = New Integer(5) {}
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Literals: Type Conversions (1/3)
• Two Type Conversions
– Widening: Integer to Long, ...
– Narrowing: Long to Integer, ...• Possibility of data loss or incorrect result
• Implicit Narrowing Conversion
– When Option Strict On: Compile time error
– When Option Strict off: May have Runtime exception
Dim a As Integer = 5Dim b As Long = a
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms 잠깐 : Option Strict
• Option Strict On
– Compiler can help detect coding errorsbefore they become runtime errors
• Default: Off
• In Source code
• When compile
‘ At the top of a source fileOption Strict On
Vbc MySource.vb /optionstrict+
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Literals: Type Conversions (2/3)
• Explicit Conversion
– May have runtime exception, too
Conversion FunctionCBool CInt
CByte CLng
CChar CObj
CDate CSng
CDbl CStr
CDec
Dim a As Long = 5Dim b As Integer = CInt(a)
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Literals: Type Conversions (2/3)
• Object-reference Conversion
– Widening: a derived to a base
– Narrowing: a base to a derived
– Explicit Conversion: CType function
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Namespaces
• Namespaces: Prevent name (of type) clashes– Thousands of types are defined in the .NET framework
• Microsoft Recommendation– CompanyName.TechnologyName
– Microsoft.VisualBasic
Dim X As New FooBarCorp.SuperFoo2100.SomeClass()x.DoSomething()Dim y As new _ MegaBiz.ProductivityTools.WizardMaster.SomeClass()y.DoSomethingElse()
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Namespace: The Namespace statement
• Specifying a type’s namespace
• A source file may have multiple namespaces
• Second way: compile time
– Root namespace: Concatenation
• Just a convenience (How about Package in Java?)
– To the runtime, a type is NOT “in” a namespace
– More than one assembly can use a single namespace
Vbc src.vb /t:library /rootnamespace:MegaBiz.ProductivityTools.WizardMaster
Namespace MegaBiz.ProductivityTools.WizardMaster Public Class SomeClass End ClassEnd Namespace
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Namespace: The Imports statement
• The CLR deals with type in terms of their full names
• Offering shortcut
• If multiple imported namespaces have some name clashes
– It’s Okay
– Use the full name
• Just a convenience, too
– Does NOT set a reference to the assembly
Imports System.DrawingDim pt As Pointpt.X = 10pt.Y = 20
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Symbolic Constants
Public Const CarbonHalfLifeInYears As Double = 5730Public Shared Function RemainingCarbonMass (_ ByVal InitialMass As Double, _ ByVal Years As Long _) As Double
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Variables
• Variable: Identifier declared in a method
– Different from Field, Property
• Dim is short for dimension
• Type characters: Do NOT use for readability
Dim str As StringDim i%Dim d as Double = 10.0
Data type Type characterDecimal @
Double #
Integer %
Long &
Single !
String $
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Scope
• Scope: visibility of identifiers
• VB.NET has block (End, Loop, Next) scope
– Life of the block-level variable: method• Don’t count on this behavior: might change in later
Dim i As IntegerFor i=1 to 100 Dim j As Integer For j=1 to 100 ‘... NextNext‘j is not visible here
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms Access Modifiers
• Access Modifiers: for type and type members
Public Class SomeClass Public Sub DoSomething() ‘... End Sub Public Sub InternalHelperSub() ‘... End SubEnd Class
Access Modifier Accessible from
Friend The program in which it is declared
Private The context
Protected The own class or derived class
Protected Friend Protected or Friend
Public All
Syste
m P
rog
ram
mers
' A
ssocia
tion
for
Researc
hin
g C
om
pu
ter
Syste
ms To be continued
• 제목 : Visual Basic Study #2
• 발표 : 김기영 ([email protected])
• 일시 : 다음주 초• 내용
2. The Visual Basic .NET Language
• Assignment
• Operators and Expressions
• Statements
• Classes
• Interfaces