슬라이드 1

Download Report

Transcript 슬라이드 1

System Programmers' Association for Researching Computer Systems
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
System Programmers' Association for Researching Computer Systems
세미나 안내
• 이 세미나는 같이 공부 하는 세미나
– 발표자도 잘 모름, 먼저 조금 봤을 뿐
• 질문은 바로 바로
– 발표자 말을 끊고서라도 바로 바로!
– 답변이 길면 끝난 후에
System Programmers' Association for Researching Computer Systems
타이포그래피 안내
• 글자 색
– 중요한 것
– 알아야 할 용어
– 잘 이해가 안 된 것
– 궁금한 것
• Code uses this font, Lucida Console
‘ Source Code are in this box
Execution Command are in this box
표
머리
이것저것
표는 이런 색으로 그림
System Programmers' Association for Researching Computer Systems
목차
•
Same as the Textbook
1.
Introduction
•
What Is the Microsoft .NET
framework?
2. The Visual Basic .NET Language
•
Source Files
•
Identifiers
•
Keywords
•
What Is Visual Basic .NET?
•
Literals
•
An Example Visual Basic .NET
Program
•
Namespaces
•
Symbolic Constants
•
Variables
•
Scope
•
Access Modifiers
•
Assignment
•
Operators and Expressions
•
To be continued
System Programmers' Association for Researching Computer Systems
Introduction
• VB .NET
– Is fully object-oriented
– Have full access to the .NET framework
– Application run within .NET common language runtime
System Programmers' Association for Researching Computer Systems
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
System Programmers' Association for Researching Computer Systems
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)
System Programmers' Association for Researching Computer Systems
Hello, World
Imports System
Public Module Hello
Public Sub Main
Console.WriteLine("hello, world")
End Sub
End Module
• Edit: Editor such as Window’s Notepad (Vim)
• Save: Hello.vb (Any filename ends with .vb)
• Compile:
vbc Hello.vb
• Execute:
Hello.exe
Show!
System Programmers' Association for Researching Computer Systems
Hello, World: 첫인상 (교재 외)
• ‘;’ 없군
– 줄 단위 파싱
– 한 줄로 해야 할 것을 두 줄에 나누어 쓸 때는 ‘_’ 사용
• ‘{’ 없네
– End (Next, Loop, …)
– 인덴테이션은 상관 없음 (Python?)
• Java 비슷하군
– Imports
– 클래스명 첫 문자를 대문자로 (하지만 메소드 명까지도 대문자네)
System Programmers' Association for Researching Computer Systems
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 Sub
End Module
System Programmers' Association for Researching Computer Systems
Hello, World: The Main
• Main
– 1 Main in 1 Application
• More than 1: compiler option:
/main:location
• Main Code in Class:
– Shared = Static in Java
Imports System
Public Class Hello
Public Shared Sub Main
Console.WriteLine("hello, world")
End Sub
End Class
System Programmers' Association for Researching Computer Systems
Source Files
• All source files: .vb extension
• The source files of declarations: unimportant
System Programmers' Association for Researching Computer Systems
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
System Programmers' Association for Researching Computer Systems
Keywords
• Keywords: words with special meaning in a VB .NET
Keyword
Description
AddHandlerOf
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)
…
System Programmers' Association for Researching Computer Systems
Literals (1/4)
• Literals: representation of values
• Numeric
x = y * 3.14
xd = y * 3.14D
– Integer types
• Integer (Default, ~2147483648)
• Long (~9223372036854775807)
• Byte, Short
– Floating Point types
• Single, Double (Default), Decimal
System Programmers' Association for Researching Computer Systems
Literals (2/4)
• String
Sonsole.WriteLine(“So then Dave said, “”hello, world””.”)
– Quotation mark in String Literals: type twice
• Character
Dim MyChar as Char
MyChar = “A”c
– Strings and Characters are distinct types
– Next code: compile time error if Option Strict is On:
‘ Wrong
Dim MyChar as Char
MyChar = “A”
System Programmers' Association for Researching Computer Systems
Literals (3/4)
• Date
Dim MyChar as Date
MyDate = #11/15/2001 3:00:00 PM#
– Format: m/d/yyyy
• always (regardless of regional setting)
• Nothing
– Nothing: uninitialized value of any type
– Numeric: 0 or 0.0, String: “”, …
– Reference: null?
System Programmers' Association for Researching Computer Systems
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
“ “
System Programmers' Association for Researching Computer Systems
Literals: Types (1/2)
• Fundamental Types
– Alias of .NET types
– No type-compatibility issues with other languages
Dim x As Integer
Dim y As System.Int32
If x.GetType() Is y.GetType() Then
Console.WriteLine(“They’re the same type.”)
End If
– Base: Object
– Numeric: Short, Integer, Long, Single, Double, Decimal
– Others: Byte, Char, String
– Byte: no literal representation
System Programmers' Association for Researching Computer Systems
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
System Programmers' Association for Researching Computer Systems
Literals: Arrays (1/2)
• Declaration
Dim a(4) As Integer
– 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
For i=0 To 4
Console.WriteLine(a(i))
Next
• Initial value of elements: default value of that type
System Programmers' Association for Researching Computer Systems
Literals: Arrays (2/2)
• Initializing arrays
Dim a() As String {“First”, “Second”, “Third”}
Dim b(,) As Integer {{1,2}, {3,4}, {5,6}}
• Dynamically allocating arrays
Dim
a =
Dim
b =
a()
New
b()
New
As Integer
Integer(4) {1, 2, 3, 4, 5}
As Integer
Integer(5) {}
System Programmers' Association for Researching Computer Systems
Literals: Type Conversions (1/3)
• Two Type Conversions
– Widening: Integer to Long, ...
Dim a As Integer = 5
Dim b As Long = a
– 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
System Programmers' Association for Researching Computer Systems
잠깐: Option Strict
• Option Strict On
– Compiler can help detect coding errors
before they become runtime errors
• Default: Off
• In Source code
‘ At the top of a source file
Option Strict On
• When compile
Vbc MySource.vb /optionstrict+
System Programmers' Association for Researching Computer Systems
Literals: Type Conversions (2/3)
• Explicit Conversion
Dim a As Long = 5
Dim b As Integer = CInt(a)
– May have runtime exception, too
Conversion Function
CBool
CInt
CByte
CLng
CChar
CObj
CDate
CSng
CDbl
CStr
CDec
System Programmers' Association for Researching Computer Systems
Literals: Type Conversions (2/3)
• Object-reference Conversion
– Widening: a derived to a base
– Narrowing: a base to a derived
– Explicit Conversion: CType function
System Programmers' Association for Researching Computer Systems
Namespaces
• Namespaces: Prevent name (of type) clashes
– Thousands of types are defined in the .NET framework
Dim X As New FooBarCorp.SuperFoo2100.SomeClass()
x.DoSomething()
Dim y As new _
MegaBiz.ProductivityTools.WizardMaster.SomeClass()
y.DoSomethingElse()
• Microsoft Recommendation
– CompanyName.TechnologyName
– Microsoft.VisualBasic
System Programmers' Association for Researching Computer Systems
Namespace: The Namespace statement
• Specifying a type’s namespace
Namespace MegaBiz.ProductivityTools.WizardMaster
Public Class SomeClass
End Class
End Namespace
• A source file may have multiple namespaces
• Second way: compile time
Vbc src.vb /t:library
/rootnamespace:MegaBiz.ProductivityTools.WizardMaster
– 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
System Programmers' Association for Researching Computer Systems
Namespace: The Imports statement
• The CLR deals with type in terms of their full names
• Offering shortcut
Imports System.Drawing
Dim pt As Point
pt.X = 10
pt.Y = 20
• 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
System Programmers' Association for Researching Computer Systems
Symbolic Constants
Public Const CarbonHalfLifeInYears As Double = 5730
Public Shared Function RemainingCarbonMass (_
ByVal InitialMass As Double, _
ByVal Years As Long _
) As Double
System Programmers' Association for Researching Computer Systems
Variables
• Variable: Identifier declared in a method
– Different from Field, Property
Dim str As String
Dim i%
Dim d as Double = 10.0
• Dim is short for dimension
• Type characters: Do NOT use for readability
Data type
Type character
Decimal
@
Double
#
Integer
%
Long
&
Single
!
String
$
System Programmers' Association for Researching Computer Systems
Scope
• Scope: visibility of identifiers
• VB.NET has block (End, Loop, Next) scope
Dim i As Integer
For i=1 to 100
Dim j As Integer
For j=1 to 100
‘...
Next
Next
‘j is not visible here
– Life of the block-level variable: method
• Don’t count on this behavior: might change in later
System Programmers' Association for Researching Computer Systems
Access Modifiers
• Access Modifiers: for type and type members
Public Class SomeClass
Public Sub DoSomething()
‘...
End Sub
Public Sub InternalHelperSub()
‘...
End Sub
End 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
System Programmers' Association for Researching Computer Systems
To be continued
•
제목: Visual Basic Study #2
•
발표: 김기영 ([email protected])
•
일시: 다음주 초
•
내용
2. The Visual Basic .NET Language
•
Assignment
•
Operators and Expressions
•
Statements
•
Classes
•
Interfaces