Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Class in VB

Posted on 1999-07-20
Medium Priority
Last Modified: 2010-04-30
What does mean of CLASS?
What is the features of CLASS?
How to use the Class in a FORM?
Kindly explain me with simple example.
Question by:GOPI
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 10

Accepted Solution

caraf_g earned 100 total points
ID: 1526060
A class module is very similar to a VB code module....

From a "coding" point of view there isn't much of a difference between the two, but there is a major difference with regards to how the code in either module type is used.

A Class is a little bit like a Type, if you create a class module and give it a name clsMyClass, you can use it in variable declarations

Dim objMyVariable As clsMyClass

But before you can use the variable, you will have to "instantiate" it.

Set objMyVariable = New clsMyClass

If you don't do this, you'll get the famous Error 91.

You can declare Public variables in your class

Public intMyInteger As Integer

And you can then use this with the "dot notation"

Dim objMyVariable As clsMyClass
Set objMyVariable = New clsMyClass
objMyVariable.intMyInteger = 5

So far a class is quite similar to a Type, but a class can do so much more. You can declare public subroutines and functions
Public Sub DoSomething(<some parameters here>)

objMyVariable.DoSomething <some parameters passed in>

Or functions
Public Function ReturnSomething(<some parameters here>) As Integer

intWork = objMyVariable.ReturnSomething(<some parameters passed in>)

So you can see a class as a Type with all the relevant subroutines and procedures thrown in. All neatly grouped together for ease of reading.

You can stop other modules from accessing a class' module level variables by declaring them as Private

Private intMySecretVariable As Integer

You cannot now do:
objMyVariable.intMySecretVariable = 5
The prompter won't even list intMySecretVariable, and the above line will not compile.

If you declare Public (or global) variables in a .Bas module they will be available to absolutely everything in the application. Sometimes this is exactly what you want, but more often than not it isn't.
By declaring the variables in the Class module you restrict their "scope" to a single "instance" of this class

Dim objMyVariable1 As clsMyClass
Dim objMyVariable2 As clsMyClass
Set objMyVariable1 = New clsMyClass
Set objMyVariable2 = New clsMyClass
objMyVariable1.intMyInteger = 5
objMyVariable2.intMyInteger = 6

If you had defined intMyInteger in a .BAS module, it could only be either 5 or 6, but now you have created two "instances" of your class clsMyClass and each instance has its own intMyInteger variable.

There is a lot more to learn about classes and what I've talked about here only scratches the surface. But do get into it, it is well worth it and one step towards adopting good programming practices.

Good luck

Expert Comment

ID: 1526061
You can replace the two lines...

Dim objMyVariable As clsMyClass
Set objMyVariable = New clsMyClass

... with...

Dim objMyVariable As New clsMyClass

This will create a new instance the very first time it's called (used).
LVL 10

Expert Comment

ID: 1526062
Yes, but it is very confusing.

For intCounter = 1 To 10
    Dim objX As New YourClass
    objX.SomeProperty = SomeValue
    SomeCollection.Add objX

'The above adds a single instance of a class to the collection 10 times, rather than 10 different instances.

Reason: The Dim statement is a compiler statement only. It does NOT get executed, it only declares the variable to VB and tells VB what to do when the variable is encountered.

The fact that the statement is located inside a loop is very confusing, and using the New keyword makes things worse.

Also -
You cannot have code conditioned on
If YourObjectVariable Is Nothing Then
because the moment YourObjectVariable is referenced, even if it was indeed Nothing, VB will instantiate it.

Also -
If your code erroneously sets the object variable to Nothing, your program happily keeps going rather than showing an error message, because of the same thing.

Finally -
If you use Dim ... As New ... VB must execute the test
"If the variable is nothing then instantiate it" every time the variable is referenced, which has a slightly detrimental effect on performance (ok, granted, not much).
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 1526063
Well I guess that's why they even in the help file sais that Dim statements should be placed in the beginning of the procedure/function or whatever. This problem has never been a problem for me since I know how to programm non-beginner-style. *hehe*
LVL 10

Expert Comment

ID: 1526064
I'm sure we both do, and I hope I'm not insulting GOPI by explaining this to such an extent. But I have seen many bugs in code based on this misconception, so I thought it would do no harm to mention it here.

Expert Comment

ID: 2691632
Can we use an OCX ( in my case winsock ) in this ?

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question