GOPI
asked on
Class in VB
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.
What is the features of CLASS?
How to use the Class in a FORM?
Kindly explain me with simple example.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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).
'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).
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*
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.
Can we use an OCX ( in my case winsock ) in this ?
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).