I.T. Discussion Community!
-Collapse +Expand
VB.Net
Search VB.Net Group:

Advanced
-Collapse +Expand VB.Net To/From
To/FromCODEGuides
-Collapse +Expand VB.Net Store
PRESTWOODSTORE

Prestwood eMagazine

July Edition
Subscribe now! It's Free!
Enter your email:

   ► KBVB.Net Knowledge Base  Print This    Code Snippet DB All Groups  

VB.Net Code Snippets Page

These Code Snippets are contributed by you (our online community members). They are organized by our knowledge base topics. Specifically, by the VB.Net sub-topics.

Contribute a Code Snippet
Expand All

47 VB.Net Language Code Snippets

Group: VB.Net Language


Topic: VB.Net

-Collapse +Expand 1. 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")
Posted By Mike Prestwood, Post #101343, KB Topic: VB.Net
-Collapse +Expand 2. 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
Posted By Mike Prestwood, Post #101498, KB Topic: VB.Net
-Collapse +Expand 3. VB.Net Empty String Check (String.IsNullOrEmpty)
 

The .Net framework offers a static method in the string class: String.IsNullOrEmpty.

Dim s As String 
  
's = "" 'Uncomment to test 2nd case.
  
If String.IsNullOrEmpty(s) Then
  MessageBox.Show("hello")
End If
Posted By Mike Prestwood, Post #102045, KB Topic: VB.Net
-Collapse +Expand 4. 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>
  //...
Posted By Mike Prestwood, Post #101355, KB Topic: VB.Net
-Collapse +Expand 5. VB.Net If Statement (If..ElseIf..Else..End If)
 

Same as VB classic.

If x Then
MessageBox.Show("hello")
ElseIf Not x Then
MessageBox.Show("goodbye")
Else
MessageBox.Show("what?")
End If
Posted By Mike Prestwood, Post #101388, KB Topic: VB.Net
-Collapse +Expand 6. 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))
Posted By Mike Prestwood, Post #101375, KB Topic: VB.Net
-Collapse +Expand 7. 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
Posted By Mike Prestwood, Post #101629, KB Topic: VB.Net



Topic: Tool Basics

-Collapse +Expand 8. VB.Net Assignment (=)
 

Languages Focus: Assignment

Common 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 Assignment

VB.Net uses = for it's assignment operator.

Dim Age As Integer
Dim FullName As String
  
FullName = "Randy Spitz"
Age = 38
Posted By Mike Prestwood, Post #101382, KB Topic: Tool Basics
-Collapse +Expand 9. VB.Net Comments (' or REM)
 

Languages Focus: Comments

Commenting 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.

 


Posted By Mike Prestwood, Post #101508, KB Topic: Tool Basics
-Collapse +Expand 10. 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
Posted By Mike Prestwood, Post #101709, KB Topic: Tool Basics
-Collapse +Expand 11. 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.

Posted By Mike Prestwood, Post #101910, KB Topic: Tool Basics
-Collapse +Expand 12. VB.Net Development Tools
 

Languages Focus: Development Tools

Primary development tool(s) used to develop and debug code.

VB.Net Development Tools

Microsoft 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.

Posted By Mike Prestwood, Post #101552, KB Topic: Tool Basics
-Collapse +Expand 13. VB.Net End of Statement (Return)
 

Languages Focus: End of Statement

In 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 Statement

A 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";
Posted By Mike Prestwood, Post #101692, KB Topic: Tool Basics
-Collapse +Expand 14. VB.Net Literals (quote)
 

General Info: Programming Literals

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: Literals

In 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 Literals

String 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
Posted By Mike Prestwood, Post #101531, KB Topic: Tool Basics
-Collapse +Expand 15. 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.

Posted By Mike Prestwood, Post #101726, KB Topic: Tool Basics
-Collapse +Expand 16. 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.

Posted By Mike Prestwood, Post #101651, KB Topic: Tool Basics
-Collapse +Expand 17. 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)
Posted By Mike Prestwood, Post #101595, KB Topic: Tool Basics
-Collapse +Expand 18. 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)

Posted By Mike Prestwood, Post #101570, KB Topic: Tool Basics



Topic: Language Basics

-Collapse +Expand 19. Associative Arrays in Visual Basic (a 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

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
Posted By Stephen Berry, Post #101220, KB Topic: Language Basics
-Collapse +Expand 20. 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"))
Posted By Mike Prestwood, Post #101520, KB Topic: Language Basics
-Collapse +Expand 21. VB.Net Comparison Operators (=, <>)
 

Same as VB Classic.

//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
Posted By Mike Prestwood, Post #101814, KB Topic: Language Basics
-Collapse +Expand 22. VB.Net Custom Routines (Sub, Function)
 

VB.Net is a fully OOP language so both Subs and Functions are methods of a class. A Sub does not return a value while a Function does.

Private Sub DoSomething(ByVal pMsg As String)
'...some code.
End Sub

Private Function GetAge(ByVal x As String) As Integer
GetAge = 7
End Function
Posted By Mike Prestwood, Post #101623, KB Topic: Language Basics
-Collapse +Expand 23. 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
Posted By Mike Prestwood, Post #101647, KB Topic: Language Basics
-Collapse +Expand 24. 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
Posted By Mike Prestwood, Post #101889, KB Topic: Language Basics
-Collapse +Expand 25. 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.

Posted By Mike Prestwood, Post #101468, KB Topic: Language Basics
-Collapse +Expand 26. 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.

Posted By Mike Prestwood, Post #101563, KB Topic: Language Basics



Topic: Language Details

-Collapse +Expand 27. 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.

Posted By Mike Prestwood, Post #101863, KB Topic: Language Details
-Collapse +Expand 28. 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.

Posted By Mike Prestwood, Post #101941, KB Topic: Language Details



Topic: OOP

-Collapse +Expand 29. 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
Posted By Mike Prestwood, Post #101348, KB Topic: OOP
-Collapse +Expand 30. 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
Posted By Mike Prestwood, Post #101742, KB Topic: OOP
-Collapse +Expand 31. 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
Posted By Mike Prestwood, Post #101414, KB Topic: OOP
-Collapse +Expand 32. 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.
Posted By Mike Prestwood, Post #101406, KB Topic: OOP
-Collapse +Expand 33. 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
Posted By Mike Prestwood, Post #101820, KB Topic: OOP
-Collapse +Expand 34. 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
Posted By Mike Prestwood, Post #101835, KB Topic: OOP
-Collapse +Expand 35. VB.Net Inheritance (Inherits ParentClass)
 

VB.Net uses the Inherits keyword followed by the parent class name. If you do not include Inherits, your class inherits from System.Object.

In the following example, a terminator T-600 is-an android. 

Public Class Android
End Class
 
Public Class T-600
  Inherits Android
End Class
Posted By Mike Prestwood, Post #101397, KB Topic: OOP
-Collapse +Expand 36. 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.

Posted By Mike Prestwood, Post #101451, KB Topic: OOP
-Collapse +Expand 37. 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
Posted By Mike Prestwood, Post #101443, KB Topic: OOP
-Collapse +Expand 38. 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
Posted By Mike Prestwood, Post #101761, KB Topic: OOP
-Collapse +Expand 39. 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()
Posted By Mike Prestwood, Post #101746, KB Topic: OOP
-Collapse +Expand 40. 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

Posted By Mike Prestwood, Post #101925, KB Topic: OOP
-Collapse +Expand 41. 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 F
CyborgName
End Get
 
Set(ByVal value)
F
CyborgName = value
End Set
End Property
End Class
Posted By Mike Prestwood, Post #101741, KB Topic: OOP
-Collapse +Expand 42. 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
Posted By Mike Prestwood, Post #101947, KB Topic: OOP
-Collapse +Expand 43. VB.Net Partial Classes (Partial)
 

VB.Net supports both partial classes and partial methods.

Partial Public Class Cyborg
Inherits System.Object
End Class
Posted By Mike Prestwood, Post #101857, KB Topic: OOP
-Collapse +Expand 44. VB.Net Polymorphism
 
-Collapse +Expand 45. 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
Posted By Mike Prestwood, Post #101429, KB Topic: OOP
-Collapse +Expand 46. 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)
Posted By Mike Prestwood, Post #101938, KB Topic: OOP
-Collapse +Expand 47. 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
Posted By Mike Prestwood, Post #101484, KB Topic: OOP
Follow PrestwoodBoards on: 


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech