VB.Net Flashcards Library

These FlashCards are contributed by you (our online community members). They are organized by our knowledge base topics. Specifically, by the VB.Net sub-topics.
|
48 VB.Net Language FlashCards
Group: VB.Net Language
Topic: VB.Net
VB.Net Case Sensitivity (No)
|
|
VB.Net is not case sensitive. If you type any other case for commands or variables, VB.Net will change it to the accepted or defined case. For example, if you type messagebox.show it is converted to MessageBox.Show.
|
|
|
The following code works: MessageBox.Show("hello")
|
|
|
|
|
VB.Net Code Blocks (End Xxx)
|
|
VB.Net, like VBClassiccode blocks, are surrounded by statement ending keywords that all use End such as End Class, End Sub, End Function, End If, and End If.
|
|
|
Public Class Dog
End Class Protected Sub MySub End Sub Public Shared Function MySharedFunction() As Integer MySharedFunction = 12345 End Function If x Then End If
|
|
|
|
|
|
|
Dim s As String 's = "" 'Uncomment to test 2nd case. If String.IsNullOrEmpty(s) Then MessageBox.Show("hello") End If
|
|
|
|
|
VB.Net File Extensions
|
|
Common source code file extensions include:
- .SLN - Solution File. Contains solution specific information such as links to the projects within this solution.
- .VBPROJ - VB.Net Project File. Contains project specific information.
- .VB -VB.Net source file.
- .Designer.VB -VB.Net form file (a text resource file).
|
|
|
//Sample code snippet from the .vbproj project file: <ItemGroup> <Compile Include="Cyborg.vb" /> <Compile Include="Form1.vb"> <SubType>Form</SubType> </Compile> //...
|
|
|
|
|
|
|
If x Then MessageBox.Show("hello") ElseIf Not x Then MessageBox.Show("goodbye") Else MessageBox.Show("what?") End If
|
|
|
|
|
VB.Net Left of Substring (Left or Substring)
|
|
The above usage of Left and Substring are equivalent.
Left is a traditional VB approach popular with developers moving from VB Classic to VB.Net. Substring is considered the .Net way of doing string manipulation.
|
|
|
Dim FullName FullName = "Prestwood" Console.WriteLine("Hello " + Left(FullName, 4)) Console.WriteLine("Hello " + FullName.Substring(0, 4))
|
|
|
|
|
VB.Net Parameters (ByVal, ByRef)
|
|
By Reference or Value For parameters, you can optionally specify ByVal or ByRef. ByVal is the default if you don't specify which is changed from VB Classic (in VB Classic, ByRef was the default).
|
|
|
Private Function Add(ByRef a As Integer, _ ByRef b As Integer) As Integer Add = a + b End Function
|
|
|
|
|
Topic: Tool Basics
VB.Net Assignment (=)
|
Languages Focus: AssignmentCommon assignment operators for languages include =, ==, and :=. An assignment operator allows you to assign a value to a variable. The value can be a literal value like "Mike" or 42 or the value stored in another variable or returned by a function. VB.Net AssignmentVB.Net uses = for it's assignment operator.
|
|
|
Dim Age As Integer Dim FullName As String FullName = "Randy Spitz" Age = 38
|
|
|
|
|
VB.Net Comments (' or REM)
|
Languages Focus: CommentsCommenting code generally has three purposes: to document your code, for psuedo coding prior to coding, and to embed compiler directives. Most languages support both a single line comment and a multiple line comment. Some languages also use comments to give instructions to the compiler or interpreter.
VB.Net Comments Commenting Code VB.Net, like all the VB-based languages, uses a single quote (') or the original class-style basic "REM" (most developers just use a quote). VB.Net does NOT have a multiple line comment.
|
|
|
'Single line comment.
REM Old school single line comment.
|
|
|
|
|
VB.Net Constants (Const kPI Double = 3.1459)
|
|
In VB.Net, you define constants with the Const keyword.
All constants are part of a class (no global constants) but you can make a constant public and have access to it using ClassName.ConstantName so long as you have added the class to the project. This works even without creating the class as if the public constants were static, but you cannot use the Shared keyword.
Constants must be of an integral type (sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, or string), an enumeration, or a reference to null.
|
|
|
Public Class Convert Inherits System.Object Public Const kName As String = "Mike" Private Const kPI Double = 3.1459 //Declare two or more on same line too: Const kFeetToMeter = 3.2808, kMeterToFeet = 0.3048 End Class
|
|
|
|
|
VB.Net Deployment Overview
|
|
VB.Net projects require the .Net framework and any additional dependencies you've added such as Crystal Reports.
In Visual Studio.Net, you can create a Setup and Deployment project by using any of the templates available on the New Project dialog (Other Project Types).
In addition, VB.Net projects also support ClickOnce which brings the ease of Web deployment to Windows Forms and console applications. To get started, right click on your solution in the Solution Explorer, click Properties then select the Security tab.
In addition, you can use any of the many free and commercially available installation packages.
|
|
|
|
VB.Net Development Tools
|
Languages Focus: Development ToolsPrimary development tool(s) used to develop and debug code.
VB.Net Development ToolsMicrosoft Visual Basic Express Editions (as in Visual Basic 2008 Express Edition) and the full version of Microsoft Visual Studio.Net are the current primary tools. VB.Net is not compatible with VB Classic.
|
|
|
|
VB.Net End of Statement (Return)
|
Languages Focus: End of StatementIn coding languages, common End of statement specifiers include a semicolon and return (others exist too). Also of concern when studying a language is can you put two statements on a single code line and can you break a single statement into two or more code lines.
VB.Net End of StatementA return marks the end of a statement and you cannot combine statements on a single line of code. You can break a single statement into two or more code lines by using a space and underscore " _".
|
|
|
Console.WriteLine("Hello1") Console.WriteLine("Hello2") Console.WriteLine("Hello3") 'The following commented code 'on a single line does not work... 'Console.WriteLine("Hello4") Console.WriteLine("Hello5") 'Two or more lines works too with a space+underscore: Console.WriteLine _ "Hello6";
|
|
|
|
|
VB.Net Literals (quote)
|
|
A value directly written into the source code of a computer program (as opposed to an identifier like a variable or constant). Literals cannot be changed. Common types of literals include string literals, floating point literals, integer literals, and hexidemal literals. Literal strings are usually either quoted (") or use an apostrophe (') which is often referred to as a single quote. Sometimes quotes are inaccurately referred to as double quotes. Languages Focus: LiteralsIn addition to understanding whether to use a quote or apostrophe for string literals, you also want to know how to specify and work with other types of literals including floating point literals. Some compilers allow leading and trailing decimals (.1 + .1), while some require a leading or trailing 0 as in (0.1 + 0.1). Also, because floating point literals are difficult for compilers to represent accurately, you need to understand how the compiler handles them and how to use rounding and trimming commands correctly for the nature of the project your are coding. VB.Net LiteralsString literals are quoted as in "Prestwood". If you need to embed a quote use two quotes in a row.
To specify a floating point literal between 1 and -1, preceed the decimal with a 0. If you don't, the compiler will auto-correct your code and place a leading 0. It will change .1 to 0.1 automatically. Trailing decimals are not allowed.
|
|
|
Console.WriteLine("Hello") Console.WriteLine("Hello ""Mike"".") 'Does VB.Net evaluate this simple 'floating point math correctly? No! If (.1 + .1 + .1) = 0.3 Then MsgBox "Correct" Else MsgBox "Not correct" End If
|
|
|
|
|
VB.Net Overview and History
|
|
Language Overview: VB.Net is an OOP language (no global functions or variables). You code using a fully OOP approach (everything is in a class).
Target Platforms: VB.Net is most suitable for creating any type of application that runs on the .Net platform. This includes desktop business applications using WinForms and websites using WebForms.
|
|
|
|
VB.Net Report Tools Overview
|
|
Microsoft includes ReportViewer Starting with Visual Studio 2005. You can even surface this .Net solution in your VB Classic application if you wish. For WebForm applications the client target is the browser (a document interfaced GUI), a common solution is to simply output an HTML formatted page with black text and a white background (not much control but it does work for some situations). For WinForm applications, Crystal Reports is still very popular with VB.Net developers because it has been bundled with Visual Basic since VB 3, it's overall popularity, and compatibility with many different development tools.
|
|
|
|
VB.Net String Concatenation (+ or &)
|
|
Most (many?) developers recommend using "+" because that's what C# uses but "&" is also available and many VB Classic and VBA/ASP developers prefer it. My preference is to use the & because it offers implicit type casting.
|
|
|
Dim FullName Dim Age //You can use + for strings. FullName = "Prestwood" Console.WriteLine("Hello " + FullName) //For implicit casting, use & Age = 35 Console.WriteLine(FullName & " is " & Age & " years old.") 'Implicit casting of numbers. ' 'This works: MessageBox.Show(3.3) 'This fails: 'MessageBox.Show("" + 3.3)
'This works: MessageBox.Show("" + CStr(3.3)) 'Implicit casting &. This also works: MessageBox.Show("" & 3.3)
|
|
|
|
|
VB.Net Variables (Dim x As Integer=0)
|
|
Variables are case sensitive but VS.Net will auto-fix your variable names to the defined case. You can declare variables in-line wherever you need them and declarative variable assignment is supported.
|
|
|
Dim FullName As String Dim Age As Integer Dim Weight As Double FullName = "Mike Prestwood" Age = 32 Weight = 154.4 'Declaritive variable assignment: Dim Married As String = "Y" MsgBox(Married)
|
|
|
|
|
Topic: Language Basics
|
|
Imports System.Collections.Generic
Protected Sub Button1_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Button1.Click Dim States As New Dictionary(Of String, String) States.Add("CA", "California") States.Add("NV", "Nevada") States.Add("OR", "Oregon")
MsgBox(States("NV")) End Sub
|
|
|
|
|
VB.Net Associative Array (Dictionary)
|
|
An associative array links a set of keys to a set of values. In Visual Basic, associative arrays are implemented as Dictionaries.
This code produces a message box saying "Nevada."
|
|
|
//Imports System.Collections.Generic � Dim States As New Dictionary(Of String, String) States.Add("CA", "California") States.Add("NV", "Nevada")
MsgBox(States("NV"))
|
|
|
|
|
|
|
//Does VB.Net evaluate the math correctly? No! If 0.1 + 0.1 + 0.1 = 0.3 Then MessageBox.Show("correct") Else MessageBox.Show("not correct") End If
|
|
|
|
|
|
|
Private Sub DoSomething(ByVal pMsg As String) '...some code. End Sub Private Function GetAge(ByVal x As String) As Integer GetAge = 7 End Function
|
|
|
|
|
VB.Net Exception Trapping (Try...Catch...Finally)
|
|
A common usage of exception handling is to obtain and use resources in a "try-it" block, deal with any exceptions in an "exceptions" block, and release the resources in some kind of "final" block which executes whether or not any exceptions are trapped.
|
|
|
Try Dim y As Integer = 0 y = 1 / y Catch MessageBox.Show("you cannot divide by zero")
End Try
|
|
|
|
|
VB.Net Logical Operators
|
|
VB.Net logical operators:
| And |
and, as in this and that |
| Or |
or, as in this or that |
| Not |
Not, as in Not This |
| Xor |
either or, as in this or that but not both |
|
|
|
'Given expressions a, b, c, and d: If Not (a and b) and (c or d) Then 'Do something. End If
|
|
|
|
|
VB.Net Overloading (Overloads, or implicit)
|
|
VB.Net supports both method and operator overloading.
For method overloading, you either use implicit overloading (no special syntax like C#) or use the Overloads keyword. If you use the Overloads keyword, all overloaded methods with the same name in the same class must include the Overloads keyword.
|
|
|
|
VB.Net Unary Operators
|
|
An operation with only one operand (a single input). The following are the�VB.Net unary operators: +, -, isFalse, isTrue, and Not. A unary operator method can return any type but takes only one parameter, which must be the type of the containing class.
|
|
|
|
Topic: Language Details
VB.Net Inlining (Automatic)
|
|
In VB.Net, inlining is automatically done for you by the JIT compiler for all languages and in general leads to faster code for all programmers whether they are aware of inlining or not.
|
|
|
|
VB.Net Pointers (None)
|
|
VB.Net doesn't support pointers. The closest it comes is IntPtr which you use to get pointer handles on windows, files, etc.
C# does have better support for pointers and C++/CLI has extensive support. One solution when it's really needed in VB.Net is to code in C# or C++/CLI and add it to your project.
However, VB.Net does support references.
|
|
|
|
Topic: OOP
VB.Net Abstraction (MustInherit, MustOverride, Overrides)
|
|
VB.Net supports abstract class members and abstract classes using the MustInherit and MustOverride modifiers.An abstract class is indicated with a MustInherit modifier and is a class with one or more abstract members and you cannot instantiate an abstract class. However, you can have additional implemented methods and properties. An abstract member is either a method (implicitly virtual), property, indexer, or event in an abstract class. You can add abstract members ONLY to abstract classes using the MustOverride keyword. Then you override it in a descendant class with Overrides.
|
|
|
Public MustInherit Class Cyborg Public MustOverride Sub Speak(ByVal pMessage As String) End Class Public Class Series600
Inherits Cyborg Public Overrides Sub Speak(ByVal pMessage As String)
MessageBox.Show(pMessage) End Sub End Class
|
|
|
|
|
VB.Net Access Modifiers
|
|
In VB.Net, you specify each class and each class member's visibility with an access modifier preceding class or member. The VB.Net access modifiers are the traditional Public, Protected, and Private plus the two additional .Net modifiers Friend and Protected Friend.
Friend indicates members are accessible from types in the same assembly. Protected Friend indicates members are accessible from types in the same assembly as well as descendant classes. OO purist might object to Friend and Protected Friend and I suggest you avoid them until you both fully understand them and have a need that is best suited by them.
|
|
|
Public Class Cyborg Inherits Object Private FName As String End Class
|
|
|
|
|
VB.Net Base Class (System.Object)
|
|
In VB.Net, the Object keyword is an alias for the base System.Object class and is the single base class all classes ultimately inherit from.
|
|
|
'Specify both namespace and class: Public Class Cyborg Inherits System.Object End Class 'Use Object alias for System.Objct: Public Class Cyborg Inherits Object End Class 'Default when not specified is System.Object: Public Class Cyborg
End Class
|
|
|
|
|
VB.Net Class..Object (Class..End Class..New)
|
|
Declare and implement VB.Net classes after the form class or in their own .vb files. Unlike VB Classic, you can have more than one class in a .vb class file (VB classic uses .cls files for each class).
|
|
|
Class definition: Public Class Cyborg Inherits Object Public Sub IntroduceYourself() MessageBox.Show("Hi, I do not have a name yet.") End Sub End Class
Some event like a button click: Dim T1 As New Cyborg T1.IntroduceYourself() //No need to clean up with managed classes. //The garbage collector will take care of it.
|
|
|
|
|
VB.Net Constructors (New)
|
|
A sub named New. You can overload the constructor simply by adding two or more New subs with various parameters. Public Class Cyborg Public CyborgName As String Public Sub New(ByVal pName As String) CyborgName = pName End Sub End Class
|
|
|
Public Class Cyborg Public CyborgName As String Public Sub New(ByVal pName As String) CyborgName = pName End Sub End Class
|
|
|
|
|
VB.Net Finalizer (Finalize())
|
|
Use a destructor to free unmanaged resources. A destructor is a method with the same name as the class but preceded with a tilde (as in ~ClassName). The destructor implicity creates an Object.Finalize method (you cannot directly call nor override the Object.Finalize method).
In VB.Net you cannot explicitly destroy an object. Instead, the .Net Frameworks garbage collector (GC) takes care of destroying all objects. The GC destroys the objects only when necessary. Some situations of necessity are when memory is exhausted or you explicitly call the System.GC.Collect method. In general, you never need to call System.GC.Collect.
|
|
|
Class Cyborg � Protected Overrides Sub Finalize() ��� 'Free non-managed resources here. ��� MyBase.Finalize() � End Sub End Class
|
|
|
|
|
|
|
In the following example, a terminator T-600 is-an android. Public Class Android End Class Public Class T-600 Inherits Android End Class
|
|
|
|
|
VB.Net Inheritance-Multiple (Not Supported)
|
|
VB.Net does not support multiple implementation inheritance. Each class can have only one parent class (a single inheritance path). In VB.Net, you can use multiple interface usage to design in a multiple class way horizontally in a class hierarchy.
|
|
|
|
VB.Net Interfaces (Interface, Implements)
|
|
With VB.Net you define an interface with the Interface keyword and use it in a class with the Implements keyword. In the resulting class, you implement each property and method and add Implements Interface.Object to each as in: Sub Speak(ByVal pSentence As String) Implements IHuman.Speak MessageBox.Show(pSentence) End Sub
|
|
|
Public Interface IHuman 'Specify interface methods and properties here.
End Interface
Public Class Cyborg Inherits System.Object End Class
Public Class CyborgHuman Inherits Cyborg Implements IHuman 'Implement interface methods and properties here.
End Class
|
|
|
|
|
VB.Net Member Field
|
|
In VB.Net you can set the visibility of a member field to any visibility: private, protected, public, friend or protected friend.
You can intialize a member field with a default when declared. If you set the member field value in your constructor, it will override the default value.
Finally, you can use the Shared modifier (no instance required) and ReadOnly modifier (similar to a constant).
|
|
|
Public Class Cyborg Private FSerialNumber As String = "A100" Public CyborgName As String Public CyborgAge As Integer Public Shared ReadOnly SeriesID As Integer = 100 End Class
|
|
|
|
|
VB.Net Member Method (Sub, Function)
|
|
VB.Net uses the keywords sub and function. A sub does not return a value and a function does. Many programmers like to use the optional call keyword when calling a sub to indicate the call is to a procedure.
|
|
|
Class definition: Public Class Cyborg � Inherits Object � Public Sub IntroduceYourself() ��� MessageBox.Show("Hi, I do not have a name yet.")� End Sub End Class
Some event like a button click: Dim T1 As New Cyborg T1.IntroduceYourself()
|
|
|
|
|
VB.Net Member Modifiers
|
|
The method modifiers include MustOverride, NotOverridable, Overridable, Overrides. Specify VB.Net member modifiers as follows:
Public Overrides Function SomeFunction() As Double
The field modifiers include ReadOnly and Shared. Specify field modifiers as follows:
Public ReadOnly SomeField As String
|
|
|
|
VB.Net Member Property (property, get, set)
|
|
VB.Net uses a special property keyword along with special get and set methods to both get and set the values of properties. For a read-only property, leave out the set method. The value keyword is used to refer to the member field. Properties can make use of any of the access modifiers (private, protected, etc).
My preference for VB.Net code is to start member fields with "F" ("FName" in our example) and drop the "F" with properties that manage member fields ("Name" in our example).
|
|
|
Public Class Cyborg Private FCyborgName As String Public Property CyborgName() Get Return FCyborgName End Get Set(ByVal value) FCyborgName = value End Set End Property End Class
|
|
|
|
|
VB.Net Overriding (Overridable, Overrides)
|
|
In VB.Net, you specify a virtual method with the Overridable keyword in a parent class and extend (or replace) it in a descendant class using the Overrides keyword.
Use the base keyword in the descendant method to execute the code in the parent method, i.e. base.SomeMethod().
|
|
|
Public Class Robot Public Overridable Sub Speak() MessageBox.Show("Robot says hi") End Sub End Class Public Class Cyborg Inherits Robot Public Overrides Sub Speak() MessageBox.Show("hi") End Sub End Class
|
|
|
|
|
|
|
Partial Public Class Cyborg Inherits System.Object End Class
|
|
|
|
|
VB.Net Prevent Derivation (NotInheritable, NotOverridable)
|
|
With VB.Net, use the NotInheritable keyword to prevent a class from being inherited from and use the NotOverridable keyword to prevent a method from being overridden.
A method marked NotOverridable must override an ancestor method. If you mark a class NotInheritable, all members are implicitly not overridable so the NotOverridable keyword is not legal.
|
|
|
Public Class Machine
Public Overridable Sub Speak(ByRef pSentence As String) MessageBox.Show(pSentence) End Sub End Class Public Class Robot Inherits Machine Public NotOverridable Overrides Sub Speak(ByRef pSentence As String) MessageBox.Show(pSentence) End Sub End Class Public NotInheritable Class Cyborg Inherits Robot End Class
|
|
|
|
|
VB.Net Self Keyword (Me)
|
|
To refer to the current instance of a class or structure, use the Me keyword. Me provides a way to refer to the specific instance in which the code is currently executing. It is particularly useful for passing information about the currently executing instance.
The Me keyword is also used as a modifier of the first parameter of an extension method.
You cannot use Me with static method functions because static methods do not belong to an object instance. If you try, you'll get an error.
|
|
|
Sub SetBackgroundColor(FormName As Form) //FormName.BackColor = ...some color End Sub //Pass Me. SetBackgroundColor(Me)
|
|
|
|
|
VB.Net Shared Members (Shared)
|
|
VB.Net supports both static members and static classes (use the keyword Shared). You can add a static method, field, property, or event to an existing class.
You can designate a class as static and the compiler will ensure all methods in that class are static. You can add a constructor to a static class to initialize values.
The CLR automatically loads static classes with the program or namespace.
|
|
|
Public Shared Function MySharedFunction() As Integer MySharedFunction = 12345 End Function
|
|
|
|
|
Topic: WebForms (ASP.Net)
Q&A: Run a Program in VB.Net
|
|
Question:
How do you launch a Windows application in VB.Net? How do I open the default browser to a specific URL?
|
|
|
Answer:
'Launch a Windows application. System.Diagnostics.Process.Start("notepad.exe") 'Or just... Process.Start("calc.exe") 'Open a website with the default browser. System.Diagnostics.Process.Start("http://www.prestwood.com")
|
|
|
|
|
|
|