?
Solved

Class in VB

Posted on 1999-07-20
6
Medium Priority
?
184 Views
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.
0
Comment
Question by:GOPI
  • 3
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
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
0
 
LVL 7

Expert Comment

by:Vbmaster
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).
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1526062
Yes, but it is very confusing.

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

'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).
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 7

Expert Comment

by:Vbmaster
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*
0
 
LVL 10

Expert Comment

by:caraf_g
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.
0
 

Expert Comment

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

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

588 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