dcsimg
December 11, 2016
Hot Topics:

Beginning Objects in VB - Part 2

  • November 19, 2002
  • By Sam Huggill
  • Send Email »
  • More Articles »

Carrying on from the super exciting class we made last week, lets take a look at making the SayHello method a bit more user friendly.

Firstly, lets allow the developer using our component to specify the message displayed by the message box (in case they got fed up of Hello World!)

We could just pass the message as an argument in the SayHello method, but that's not the point I'm trying to make!!

What we will do is to use a property inside the class, which the developer will set, and then call the SayHello method. The property must be public to allow our developer to access it, and we want it to be a string. Add this line of code to the General Declarations procedure of Class1 (if you've lost the plot here retrace your steps to the first part by clicking here)

Public Message As String

Simple eh? Good. Lets move on.

We now need to modify the SayHello method to use the Message variable in place of our lovely Hello World! message:

Public Sub SayHello()
    Msgbox Message
End Sub

Still not very dynamic is it? Perhaps we could allow the developer to set some of the attributes of the message box? Like the icon displayed? Great! For this I suggest that we use our own enumeration ("Frank, he said a word!" - No I haven't just said my first word, but an enum is pretty exciting!)

For demonstration purposes, we are going to use our own public enum (private enums aren't allowed as arguments) to store the message box icon styles, and have the developer set the icon as an argument in the SayHello method. Here is our enum (add it to the General Declarations procedure)

Public Enum eMsgIcons
    eCritical = 16
    eExclamation = 48
    eInformation = 64
    eQuestion = 32
End Enum

This is cool man!

Now all we need to do is to change the SayHello method a bit:

Public Sub SayHello(Icon As eMsgIcons)
    MsgBox Message, Icon
End Sub

Right! Quickly build the DLL and switch over to our Standard EXE project. Modify the Form_Load procedure to look like this:

Private Sub Form_Load()
    m_clsClass1.Message = "Hello Programmers!"
    m_clsClass1.SayHello eInformation
End Sub

Where's the Set m_clsClass1 thingy bit gone? Well, I decided that we would create the object as New from the program startup, so modify the General Declarations procedure to look like this:

Private m_clsClass1 As New Class1

Easy! Run the program, and you see a nice popup message box:

All that's left to do is to destroy the object when we close the program. So add the following code to the Form_Unload() event procedure:

Private Sub Form_Unload()
    Set m_clsClass1 = Nothing
End Sub

And there you have your first (well, actually your second) component program using a combination of methods, properties, enumerations and arguments!

I've got to dash now, the next part needs writing! But I'll see you same time next week for another tasty serving of hardcore object programming!

Quick, download the files before I remember to give the link!
Download the files!

Goodbye World!
- Sam





Page 4 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel