IT SOLUTIONS
Your full service technology partner! 
-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

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

   ► KBProgrammingVB.NetOOP   Print This     
  From the March 2016 Issue of Prestwood eMag
 
VB.Net OOP:
VB.Net Class..Object (Class..End Class..New)
 
Posted 16 years ago on 10/24/2008 and updated 1/28/2009
VB.Net Code Snippet:
 A flashcard from our VB.Net Flashcards Library
 A code snippet from our VB.Net Code Snippets Page

KB101406

Languages Focus: Class..Object

In short, a class is a data type, and an object is an instance of a class type. A class has methods (routines), properties (member variables), and a constructor. The current values of the properties is the current state of the object. The UML is one of the diagraming disciplines that allows you to document the various changing states of a series of objects.

VB.Net Class..Object

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

Syntax Example:

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.

Add a Member Field

Let's give our cyborg a name by using a public member field. Then, below, we will change our member field into a VB.Net class property.

Public Class Form1
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim T1 As New Cyborg
 
T1.Name = "Number 1"
T1.IntroduceYourself()
End Sub
End Class
 
Public Class Cyborg
Inherits Object
 
  Public Name
 
  Public Sub IntroduceYourself()
MessageBox.Show("Hi, my name is " + Name + ".")
End Sub
End Class

Member Fields vs Properties

The main difference between a raw member field (as in above) and a property is the formal use of accessor methods. Accessor methods (getters and setters) allow you to control data going into and out of a member field and is considered a best practice.

VB.Net Property Sample Code:

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim T1 As New Cyborg
    T1.Name = "Cameron"
T1.IntroduceYourself()
End Sub
End Class
 
Public Class Cyborg
Private FName As String
 
  Public Property Name()
Get
Return FName
End Get
 
Set(ByVal value)
FName = value
End Set
End Property
 
  Public Sub IntroduceYourself()
MessageBox.Show("Hi, my name is " + Name + ".")
End Sub
End Class

You can use this class, for example, on the click event of a button:

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim T1 As New Cyborg
    T1.Name = "Cameron"
T1.IntroduceYourself()
End Sub
End Class 

More Info


Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P183A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101406 Counter
17873
Since 10/24/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]