Murray State Information Systems



Visual Basic .NETMi Gyeong GwakCSC 415 – Programming LanguagesDr. William F. LyleNovember 4, 2014General Information of Visual Basic .NETVisual Basic is an object-oriented and event-driven programming language for developing applications designed to run in Microsoft Windows and teaching purposes that derives from BASIC (Beginner's All-purpose Symbolic Instruction Code). It is tightly integrated with the .NET initiative which is “a collection of Web services developed by Microsoft Corporation that are intended to reposition the company as a provider of Internet-distributed services, including software maintenance and upgrades and transparent access to one’s data, files, and software from any type of device at any location” (Webster’s New World & Trade). BASIC was invented by two mathematicians, John Kemeny and Thomas Kurtsz, at Dartmouth College in 1964 in order to provide a simple programming language for all students. When it was first developed, computers were expensive and difficult to use, and only a few computer languages were in existence, such as FORTRAN and ALGOL (Encyclopedia of Computer Science). Moreover some computers required the use of small strips of paper or punched cards in order to program. The first BASIC programs ran on the General Electric 225, but then it started to spread by G.E. to other machines around 1970. The committee of American National Standard Institute (ANSI) started working on two standards: minimal BASIC and Standard BASIC. In 1975, Bill Gates and Paul Allen created interpreted BASIC in order to run in limited memory situations and for making debugging easier (Marconi 1). In the late 1980s, graphical user interfaces (GUIs) and Microsoft Windows became popular, but it was difficult to create Window applications. Microsoft revived BASIC in 1991 using its simplicity and general syntax by introducing Visual Basic. “In the years since, Microsoft has continued to improve Visual Basic…These renovations include making BASIC object oriented and fully event driven, and overcoming the limitations of being interpreted, allowing programmers to generate a compiled executable code” (). Visual Basic supports most constructions of common programming languages currently in use, and some essential features will be introduced in this report: names, bindings, scopes, data types, operators, control flow, objects, and error types.Overview of Visual Basic .NET Design, Syntax, and SemanticsNamesVisual Basic .NET uses a similar name format with other programming languages. A name must begin with an alphabetical character or an underscore and must be less than 1023 characters long. If it starts with an underscore, it must include at least one alphabetical character or decimal digit. Also, it is case-insensitive which means that two names that differ only in alphabetic case usage and they are treated as the same name. There are two kind of keywords: reserved and unreserved. Reserved keywords cannot be used as names for elements such as variables or procedures. However, they can be enclosed by brackets ([ ]) and used as escaped names. Unreserved keywords can be used as names for elements. However, using any keywords as names is not recommended, “because it can make your code hard to read and can lead to subtle errors that can be difficult to find” (Microsoft Developer Network).BindingsA binding in Visual Basic is an association between two object variables. Early binding means that an object is assigned to a specific object type. Late binding means that an object is assigned to an object that can hold references to any object. Early bound objects have more advantages because they “allow the compiler to allocate memory and perform other optimizations before an application executes… it enables useful features such as automatic code completion and Dynamic Help… reduces the number and severity of run-time errors” (Microsoft Developer Network). The following code is an example of an early bound object and a line starting with the comment symbol (‘), designating that line as a comment:' Create a variable to hold a new object.?Dim FS As System.IO.FileStream' Assign a new object to the variable.FS = New System.IO.FileStream("C:\tmp.txt", System.IO.FileMode.Open)ScopesVisual Basic includes block scope, procedure scope, module scope, and namespace scope. “A block is a set of statements enclosed within initiating and terminating declaration statements, such as the following” (Microsoft Developer Network):Do?and?LoopFor?[Each] and?NextIf?and?End IfSelect?and?End SelectSyncLock?and?End SyncLockTry?and?End TryWhile?and?End WhileWith?and?End With The following block of code is an example of using If and End If statements, and the integer variable cube cannot be referred to out of the block.If n < 1291 Then Dim cube As Integer cube = n ^ 3End IfA variable declared within a procedure cannot be referred outside of the procedure, and it is also known as local variable. The module scope means that a declared variable within modules, classes, and structures cannot be referred to outside of those structures. The Private modifier makes a variable accessible to every procedure in that module. For example, the string variable strMsg defined in the module can be used in all the procedures in the module:' Put the following declaration at module level (not in any procedure).Private strMsg As String' Put the following Sub procedure in the same module.Sub initializePrivateVariable() strMsg = "This variable cannot be used outside this module."End Sub' Put the following Sub procedure in the same module.Sub usePrivateVariable() MsgBox(strMsg)End SubAny variable declared using the Friend or Public keyword in a module scope is available throughout the namespace in which the variable is declared. Using a local variable would be good for a temporary calculation since it can avoid name conflicts. Minimizing scope helps reduce memory consumption and erroneously referring to the wrong variable (Microsoft Developer Network). Data TypesVisual Basic provides numeric, character, miscellaneous, and composite data types. Numeric data types can be divided as integral and non-integral types. Integral types representing integers like whole numbers include signed integral types and unsigned integral types. The signed integral types can represent negative integers and they are SByte (8-bit), Short (16-bit), Integer (32-bit), and Long (64-bit). The unsigned integral types represent only non-negative integers and they are Byte (8-bit), UShort (16-bit), UInteger (32-bit), and ULong (64-bit). Non-integral types represent numbers that have both integer and fractional parts and includes Decimal (128-bit fixed point), Single (32-bit floating point), and Double (64-bit floating point). Character data types include Char type which holds a single character (16-bit Unicode) and String type which holds indefinite number of characters. The default value of Char is the 0 code point and Char() allows holding multiple characters as an array. The default value of String is null. Char and String can be converted to an Integer using the Asc or AscW function, and an Integer value can be converted to a Char using the Chr or ChrW function. Unicode Characters defined as “The first 128 code points (0–127) of Unicode correspond to the letters and symbols on a standard U.S. keyboard. These first 128 code points are the same as those the ASCII character set defines. The second 128 code points (128–255) represent special characters, such as Latin-based alphabet letters, accents, currency symbols, and fractions” (Microsoft Developer Network).Miscellaneous data types which are not oriented numbers or characters include Boolean, Date, and Object type. Boolean type is an unsigned value that contains two-state values either True or False. Date type “is a 64-bit value that holds both date and time information. Each increment represents 100 nanoseconds of elapsed time since the beginning (12:00 AM) of January 1 of the year 1 in the Gregorian calendar” (Microsoft Developer Network). Object type (32-bit) points to an object instance which includes value types (Integer, Boolean, and structure instances) and reference types (String, Form, and array instances). Object type allows the programmers to store data of any data type, even though it causes the application to take more execution time (Microsoft Developer Network).Composite data type assembles items of different types such as structures, arrays, and classes. A structure is user-defined type (UDT) that was supported in the previous versions of Visual Basic. Moreover, “structures can expose properties, methods, and events. A structure can implement one or more interfaces, and you can declare individual access levels for each field. Structures are useful when you want a single variable to hold several related pieces of information” (Microsoft Developer Network). This is an outline of the declaration of a structure:[Public | Protected | Friend | Protected Friend | Private] Structure structname {Dim | Public | Friend | Private} member1 As datatype1 ' ... {Dim | Public | Friend | Private} memberN As datatypeNEnd StructureArray is a set of data that has a same name, but are separated by index values. Array in Visual Basic can have up to 32 dimensions, and multiple dimensions can be represented by separating the highest index value of each dimension by commas. The following example is declaring a three-dimensional array:Dim airTemperatures(99, 99, 24) As SingleProgrammers can represent nest values using braces ({}) within braces. The following example is initializing a multidimensional array variable by using array literals:Dim numbers = {{1, 2}, {3, 4}, {5, 6}}Dim customerData = {{"City Power & Light", ""}, {"Wide World Importers", ""}, {"Lucerne Publishing", ""}}' You can nest array literals to create arrays that have more than two ?' dimensions.?Dim twoSidedCube = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}All classes cannot be composed with a single data type. If one class instance variable is assigned to another, they have to have the same data type and same class instance in memory. More information will be available in Objects section. Constants and Enumerations which are a set of related constants are available in Visual Basic. Declaring several constants with the Const statement is shown below:Const conPi = 3.14159265358979Public?Const conMaxPlanets As?Integer = 9Const conReleaseDate = #1/1/1995#Const conPi2 = conPi * 2The following example is declaring an enumeration:Public?Enum MyEnum As?Byte Zero One TwoEnd?EnumType characters and type conversion keywords in Visual Basic are shown in Appendix I and II.OperatorsVisual Basic provides arithmetic, comparison, concatenation, and logical operators. Arithmetic operations use addition, subtraction, negation, multiplication, division, exponentiation operator just like other common programming languages. A bit-shift operation achieves arithmetic shift on a bit pattern of a data using the >> operator or << operator. Shifting an Integer value is shown below: Dim lResult, rResult As?Integer?Dim pattern As?Integer = 12' The low-order bits of pattern are 0000 1100.lResult = pattern << 3' A left shift of 3 bits produces a value of 96.rResult = pattern >> 2' A right shift of 2 bits produces value of parison Operators for numeric values are the same as in other programming languages, except for using the < > operator for expressing the inequality of two expressions. The Like operator is used to compare strings and an example is shown below:testCheck = "F"?Like?"[A-Z]"?' The following statement returns False (does "F" NOT occur in the ?' set of characters from "A" through "Z"?)Comparing objects requires the Is operator or IsNot operator and an example is shown below:Dim a As?New classA()Dim b As?New classB()If a IsNot b Then? ' Insert code to run if a and b point to different instances.?End?IfComparing object type uses the Type…Is expression, and the syntax is as follows:TypeOf <objectexpression> Is <typename>Two concatenation operators, the + and &, combine multiple strings into a single string. However, the + operator has the complex functions of adding numeric operands, concatenating, signaling a compiler error, or throwing a run-time exception. Logical operators includes the Not operator that performs logical negation, And operator that performs logical conjunction, Or operator that performs logical disjunction, and Xor operator that performs logical exclusion. For example,Dim a, b, c, d, e, f, g As?Booleana = 23 > 14 And 11 > 8b = 14 > 23 And 11 > 8' The preceding statements set a to True and b to False.c = 23 > 14 Or 8 > 11d = 23 > 67 Or 8 > 11' The preceding statements set c to True and d to False.e = 23 > 67 Xor 11 > 8f = 23 > 14 Xor 11 > 8g = 14 > 23 Xor 8 > 11' The preceding statements set e to True, f to False, and g to False.These logical operators are expressed as a letter instead of using special characters as is done in other programming languages. Short-circuiting logical operations contain the AndAlso operator and the OrElse operator. Bitwise operations compare each binary bit of two integral values. The And, Or, and Xor operators also used for bitwise operations (Microsoft Developer Network). Control FlowControl structures regulate the flow of a program’s execution. There are several types of control structures. Decision Structures includes the If…Then…Else construction, Select…Case construction, and Try…Catch…Finally construction. The following illustration expresses the flow of decision structure. Here is an example of If…Then…Else statement:Dim count As?Integer = 0Dim message As?String?If count = 0 Then message = "There are no items."?ElseIf count = 1 Then message = "There is 1 item."?Else message = "There are " & count & " items."?End?IfVisual Basic loop structures make the program run a block of code repetitively. The following illustration show the flow of a loop structure:There are the While…End While construction, Do…Loop construction, For…Next construction, and For Each…Next construction for the loop structures. Here is an example of a For Each…Next statement:' Create a list of strings by using a?' collection initializer.?Dim lst As?New List(Of?String) _ From {"abc", "def", "ghi"}' Iterate through the list.?For?Each item As?String?In lst Debug.Write(item & " ")NextDebug.WriteLine("")'Output: abc def ghi In addition, the Using…End Using construction allows programmers to acquire a resource such as an SQL connection, and the With…End With construction allows programmers to specify an object reference once and then be accessible in a series of statements. Nested control structures are also available in Visual Basic (Microsoft Developer Network).ObjectsVisual Basic .NET is object-oriented programming language. An object can be considered as a unit or prefabricated building block of data combination. Objects in Visual Basic can be used as controls, forms, and objects from other applications. Each object is defined by a class and an instance of the class. Multiple instances can be created within a class, but their variables and properties can be changed independently. A member of an object is accessed by separating the name of the object variable and the name of the member by a period (.) just like:warningLabel.Text = "Data not saved"Fields which is a member variable and properties are information of an object. An object can perform an action by a method such as Add to add a new entry or Start to begin a timer object. An object can recognize an action by an event such as clicking the mouse or pressing a key. An instance member belongs to a particular instance, while a shared member is able to belong to the class as whole (Microsoft Developer Network). Objects can be related to each other either in a hierarchical relationship or containment relationship. A hierarchical relationship is built when classes are derived from the class that they are based on. The Containment relationship means that a container object logically encapsulates other objects, such as collections (Microsoft Developer Network).Visual Basic allows a derived class to inherit the fields, properties, methods, events, and constants defined in the base class. The Inheritance Modifier includes the Inherits statement, NotInheritable modifier that is prevented from using a class as a base class, and MustInherit modifier that allows a class as a base class only. An inherited property or method that behaves differently in the derived class is overridden. The Overridable, Overrides, NotOverridable, and MustOverride modifiers are used to control overrides. The MyBase keyword with a variable is used to refer to the base class, and the MyClass keyword with a variable is used to refer to the current instance of a class (Microsoft Developer Network). Error TypesErrors or exceptions happen within three categories: syntax errors, run-time errors, and logic errors. Syntax errors are the most common type of errors, but the code editor helps reduce syntax errors in Visual Basic. Run-time errors occur after compiling the program and arise mostly when carrying out the Open function. Logic errors appear often when unwanted or unexpected results are received by the user (Microsoft Developer Network). Evaluation of Visual Basic .NETReadabilityVisual Basic .NET programs are very readable and understandable. Most of the code examples shown above have an English-like appearance. Particularly, keywords that control structures and data types are using a clear language and this helps the readers determine the role of a variable easily. Also, the initial and terminal statements of control structures are simple enough to facilitate easy understanding of when a block starts and ends and any repetition defined in the program. WritabilityVisual Basic .NET programs are also writable. It is a visual programming language and is used in an Integrated Development Environment (IDE). It is very simple to write names and controls using the property box of the IDE and to create a GUI Window form applications by using drag-and-drop. Visual Basic’s control structure is intuitive, but the programmer needs to be careful to type a correct termination for each initialization. Variables can be declared by using the Dim statement, a name and a data type and can be as simple as:Dim My_Counter As IntegerVisual Basic .NET code is expressed like English, and programmers can easily write and understand a program.ReliabilityVisual Basic .NET includes integer overflow checking by default. Programmers can control type checking by using the Option Strict statement at the beginning of the code. Setting Option Strict will force early binding and perform effectively (Microsoft Developer Network). Structured exception handling tests specific piece of the code and will be excepted if any exceptions occur. It can be done by the Try…Catch…Finally control structure and is more reliable than unstructured exception which can be done by either of the On Error Go To statement, the Resume statement, or the Error statement. CostThe Visual Studio IDE is used for developing Visual Basic .NET. Two main editions of IDE are currently Microsoft Visual Studio 2013, which costs around $300 to purchase, and Visual Studio Express Edition 2013, which is available for free. Migration from Visual Basic 6.0 to .NET also can be done by Microsoft’s partners for free. ArtinSoft’s Visual Basic Upgrade Companion, Great Migrations Studio, and VB Migration Partner tool are available in order to do migration at no cost. Appendix I. Type Characters in Visual BasicIdentifier Type CharactersIdentifier type characterData typeExample%IntegerDim L%&LongDim M&@DecimalConst W@ = 37.5!SingleDim Q!#DoubleDim X#$StringDim V$ = "Secret"Literal Type CharactersDefault Literal TypesTextual form of literalDefault data typeExampleNumeric, no fractional partInteger2147483647Numeric, no fractional part, too large for?IntegerLong2147483648Numeric, fractional partDouble1.2Enclosed in double quotation marksString"A"Enclosed within number signsDate#5/17/1993 9:32 AM#Forced Literal TypesLiteral type characterData typeExampleSShortI = 347SIIntegerJ = 347ILLongK = 347LDDecimalX = 347DFSingleY = 347FRDoubleZ = 347RUSUShortL = 347USUIUIntegerM = 347UIULULongN = 347ULCCharQ = "."CHexadecimal and Octal LiteralsNumber basePrefixValid digit valuesExampleHexadecimal (base 16)&H0-9 and A-F&HFFFFOctal (base 8)&O0-7&O77Appendix II. Type Conversion Keywords in Visual BasicType conversion keywordConverts an expression to data typeAllowable data types of expression to be convertedCBoolBoolean Data Type (Visual Basic)Any numeric type (including?Byte,?SByte, and enumerated types),?String,?ObjectCByteByte Data Type (Visual Basic)Any numeric type (including?SByte?and enumerated types),?Boolean,?String,ObjectCCharChar Data Type (Visual Basic)String?,?ObjectCDateDate Data Type (Visual Basic)String?,?ObjectCDblDouble Data Type (Visual Basic)Any numeric type (including?Byte,?SByte, and enumerated types),?Boolean,?String,ObjectCDecDecimal Data Type (Visual Basic)Any numeric type (including?Byte,?SByte, and enumerated types),?Boolean,?String,ObjectCIntInteger Data Type (Visual Basic)Any numeric type (including?Byte,?SByte, and enumerated types),?Boolean,?String,ObjectCLngLong Data Type (Visual Basic)Any numeric type (including?Byte,?SByte, and enumerated types),?Boolean,?String,ObjectCObjObject Data TypeAny typeCSByteSByte Data Type (Visual Basic)Any numeric type (including?Byte?and enumerated types),?Boolean,?String,ObjectCShortShort Data Type (Visual Basic)Any numeric type (including?Byte,?SByte, and enumerated types),?Boolean,?String,ObjectCSngSingle Data Type (Visual Basic)Any numeric type (including?Byte,?SByte, and enumerated types),?Boolean,?String,ObjectCStrString Data Type (Visual Basic)Any numeric type (including?Byte,?SByte, and enumerated types),?Boolean,?Char,Char?array,?Date,?ObjectCTypeType specified following the comma (,)When converting to an?elementary data type?(including an array of an elementary type), the same types as allowed for the corresponding conversion keywordWhen converting to a?composite data type, the interfaces it implements and the classes from which it inheritsWhen converting to a class or structure on which you have overloaded?CType, that class or structureCUIntUInteger Data TypeAny numeric type (including?Byte,?SByte, and enumerated types),?Boolean,?String,ObjectCULngULong Data Type (Visual Basic)Any numeric type (including?Byte,?SByte, and enumerated types),?Boolean,?String,ObjectCUShortUShort Data Type (Visual Basic)Any numeric type (including?Byte,?SByte, and enumerated types),?Boolean,?String,ObjectWorks Cited"Visual Basic."?Webster's New World&Trade; Computer Dictionary. Hoboken: Wiley, 2003. Credo Reference. Web. 22 September 2014.".Net."?Webster's New World&Trade; Computer Dictionary. Hoboken: Wiley, 2003. Credo Reference. Web. 13 October 2014."Basic."?Encyclopedia of Computer Science. Eds. Edwin D. Reilly, Anthony Ralston, and David Hemmendinger. Hoboken: Wiley, 2003. Credo Reference. Web. 13 October 2014.Marconi, Andrea. "History of BASIC." . Web. 21 Oct. 2014. < of BASIC.pdf>.Hughes, Stephen, and John Daintith. "Visual Basic." . HighBeam Research, 1 Jan. 2002. Web. 21 Oct. 2014."Visual Basic Language Features." Visual Basic Language Features. Web. 21 Oct. 2014. <;.":anevaluation - Kirablowing." :anevaluation - Kirablowing. Web. 21 Oct. 2014. <;. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download