Presentation is loading. Please wait.

Presentation is loading. Please wait.

S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Visual Basic Study #1 April 01, 2004 Nam, Sedong

Similar presentations


Presentation on theme: "S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Visual Basic Study #1 April 01, 2004 Nam, Sedong"— Presentation transcript:

1 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Visual Basic Study #1 April 01, 2004 Nam, Sedong (dgtgrade@sparcs.org)dgtgrade@sparcs.org Visual Basic Study Group SPARCS (http://sparcs.org) / KAIST (http://www.kaist.ac.kr)http://sparcs.orghttp://www.kaist.ac.kr 그림 : http://www.brotherblotz.com/interlaken_materials.htm

2 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 세미나 안내 이 세미나는 같이 공부 하는 세미나 – 발표자도 잘 모름, 먼저 조금 봤을 뿐 질문은 바로 바로 – 발표자 말을 끊고서라도 바로 바로 ! – 답변이 길면 끝난 후에

3 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 타이포그래피 안내 글자 색 – 중요한 것 – 알아야 할 용어 – 잘 이해가 안 된 것 – 궁금한 것 Code uses this font, Lucida Console 표머리 이것저것표는 이런 색으로 그림 ‘ Source Code are in this box Execution Command are in this box

4 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 목차 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

5 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Introduction VB.NET –Is fully object-oriented –Have full access to the.NET framework –Application run within.NET common language runtime

6 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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

7 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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)

8 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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 Sub End Module vbc Hello.vb Hello.exe

9 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Hello, World: 첫인상 ( 교재 외 ) ‘ ; ’ 없군 – 줄 단위 파싱 – 한 줄로 해야 할 것을 두 줄에 나누어 쓸 때는 ‘ _ ’ 사용 ‘ { ’ 없네 –End ( Next, Loop, … ) – 인덴테이션은 상관 없음 (Python?) Java 비슷하군 –Imports – 클래스명 첫 문자를 대문자로 ( 하지만 메소드 명까지도 대문자네 )

10 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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

11 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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 Sub End Class

12 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Source Files All source files:.vb extension The source files of declarations: unimportant

13 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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

14 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Keywords Keywords: words with special meaning in a VB.NET KeywordDescription 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) …

15 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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.14 xd = y * 3.14D

16 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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 Char MyChar = “A”c ‘ Wrong Dim MyChar as Char MyChar = “A”

17 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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 Date MyDate = #11/15/2001 3:00:00 PM#

18 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Literals (4/4) Summary: All literals Data typeLiteral Boolean True, False CharC Date# DecimalD 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 ShortS SingleF String“

19 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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 Integer Dim y As System.Int32 If x.GetType() Is y.GetType() Then Console.WriteLine(“They’re the same type.”) End If

20 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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

21 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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

22 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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 Integer a = New Integer(4) {1, 2, 3, 4, 5} Dim b() As Integer b = New Integer(5) {}

23 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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 = 5 Dim b As Long = a

24 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 잠깐 : Option Strict Option Strict On –Compiler can help detect coding errors before they become runtime errors Default: Off In Source code When compile ‘ At the top of a source file Option Strict On Vbc MySource.vb /optionstrict+

25 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Literals: Type Conversions (2/3) Explicit Conversion –May have runtime exception, too Conversion Function CBoolCInt CByteCLng CCharCObj CDateCSng CDblCStr CDec Dim a As Long = 5 Dim b As Integer = CInt(a)

26 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Literals: Type Conversions (2/3) Object-reference Conversion –Widening: a derived to a base –Narrowing: a base to a derived –Explicit Conversion: CType function

27 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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()

28 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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 Class End Namespace

29 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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.Drawing Dim pt As Point pt.X = 10 pt.Y = 20

30 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Symbolic Constants Public Const CarbonHalfLifeInYears As Double = 5730 Public Shared Function RemainingCarbonMass (_ ByVal InitialMass As Double, _ ByVal Years As Long _ ) As Double

31 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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 String Dim i% Dim d as Double = 10.0 Data typeType character Decimal@ Double# Integer% Long& Single! String$

32 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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 Integer For i=1 to 100 Dim j As Integer For j=1 to 100 ‘... Next ‘j is not visible here

33 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 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 ModifierAccessible from FriendThe program in which it is declared PrivateThe context ProtectedThe own class or derived class Protected FriendProtected or Friend PublicAll

34 S ystem P rogrammers' A ssociation for R esearching C omputer S ystems To be continued 제목 : Visual Basic Study #2 발표 : 김기영 (clowcard@sparcs.org)clowcard@sparcs.org 일시 : 다음주 초 내용 2.The Visual Basic.NET Language Assignment Operators and Expressions Statements Classes Interfaces


Download ppt "S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Visual Basic Study #1 April 01, 2004 Nam, Sedong"

Similar presentations


Ads by Google