Another quick VB6 Question

I am a little bit behind the 8 ball here. The VB coding I have done has been with sweat and tears. I’ve had no training in it, but I’ve written a few thousand lines of code. So there are some BIG holes in my knowledge.

When you instantiate an object, what’s the difference between

Dim objTest as oneObject

-and-

Dim objtest as New otherObject

Another question -

The Implements statement. Some of the code that I have cut and pasted out of help files use the Implements object statement. Then you have to go through and define all the properties and methods in the .cls file.

You can also add Sub procedures and functions to the .cls. Is the reason you use Implement is that you can add these other routines to the object/.cls file?

Thanks.

The first line creates a variable named objTest which can hold a reference to an object of type oneObject. This assumes you have a class file somewhere named oneObject. After that line has executed, you still can’t use objTest for anything useful, because it doesn’t actually point to anything. That’s where the New keyword comes in.

The second line creates an instance of an otherObject object, then creates a variable named objtest that can hold a reference to an object of type otherObject, and then sets that reference to point to the object it created. So now you can call methods on objtest to do things.

The second line is just a shortcut way of writing


Dim objtest as otherObject
Set objtest = New otherObject

I’m pretty sure that was more confusing than I meant it to be.

The Implements keyword is how VB handles polymorphism. Polymorphism is pretty much the central component of object oriented programming, and may be a little involved to get into here, but basically it comes down to: you can define an interface that guarantees that an object will have certain methods you can call. Then you can create objects that all do different things, but implement that interface. So then you can have a single piece of code that just has to know about the interface, but you can throw all these different kinds of objects at it and it will still be able to deal with them.

If your projects are getting big enough that you’re looking at OOP techniques, you may want to consider teaching yourself a real OO language like Java or C++ before digging into VB’s version. I’ve not worked with .NET, but I cut my teeth on VB5-6 and it taught me some really bad habits that I’m busy purging myself of. You’ll be happier down the road if you get a look at OOP through something else first.

Ahh, rambling.

Now I wait for gurus more advaced than I to come and walk all over my simple explanation…

Eegba’s pretty much covered it.

I’ll add a couple of things though:
In VB6 (but not in .NET) using Dim x As New [Class] is generally considered bad practice. It doesn’t function quite how you’d expect.

It doesn’t create the instance of the class at the point where the variable is declared. It only creates the instance the first time you try to use the variable you declared.

Now, because program flow can be a complex business, rather than working out the first place the object is referenced in the code VB will check the variable every time it’s used and create the instance if it hasn’t done so yet. All these extra checks slow the program down. For the sake of an extra line of code it’s better to control this stuff yourself than eat the overhead.

VB’s OO stuff is often reviled, but in some ways it’s a good way to start learning about the techniques since it’s a pretty clean and simple implementation. It’s true it can lead you into some bad habits, and can sometimes over-protect you, but it’s not all bad. If you’re just going to be using VB then it’s probably not worth your while to go and spend weeks (or, in the case of C++, probably months) learning another language so you can understand the magnitude of the things you can’t do.

On the other hand, if you’ve the time and a training budget, Eegba’s right to say that it will help you understand the methodology better.

Polymorphism’s an important concept and worth playing around with in a couple of small side-projects until you’re comfortable with it.

Thank you both for responding. The muddy water is clearing a little. I don’t have VB at home so I can’t do any experimenting, but I will revisit the post on Monday and do some testing.

I’m sorry I did not respond sooner. Last night I tried to change SDMB contact email information and got locked out. Lynn Bondoni figured out what was going on and got me fixed up.

Thanks again

If you want to play around a bit on your own time you can still get the VB5 Control Creatioin Edition from Microsoft. It’s basically a cut-down version of VB5 that can only create ActiveX controls, but it’s enough to mess about in and try a few things out. It’s free, so you don’t need to worry about licensing or anything.

Download it here: http://msdn.microsoft.com/vbasic/downloads/tools/cce/default.aspx