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

February 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 11 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 = P1215A1
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
13798
Since 10/24/2008
-
   Contact Us!
 
PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

2,416 People Online Now!!  
Sign In to see who's online now!  Not a member? Join now. It's free!
Show more stats...


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