Post

 Resources 

Console

Home | Profile | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 VBGamer
 VBGamer
 VB6 and OOP
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Threshold
Squire

USA
44 Posts

Posted - Mar 27 2005 :  9:46:33 PM  Show Profile  Reply with Quote
I have been fooling with classes a bit more in VB6 and have found them quite limited compared to Java and other such true OOP languages.

Is it true that VB6 really doesn't have any inheritance other than interfacing? What little I've seen of interfacing looks disgusting. I make good use of polymorphism and inheritance (and even interfaces if VB would do it right).

What about a VB6 equivalent to an "instanceof" operator? I have been unable to find something to do the trick. The closes operator I've found is the "Is" operator, which doesn't at all work in the place of "instanceof."

Could someone who has decent VB6-OOP knowledge enlighten me on this? If nothing else, confirm my fears and save me all the testing to find out.

{I've been working with VB for many years, but I haven't really gotten into OOP until recently. I now know why I used to be a hater of OOP: VB6 doesn't do OOP correctly. (No public constants??? )}

Life is short. They say "don't waste it, have fun." They're right, don't waste it...but DO redefine "fun."

hotrodx
Squire

43 Posts

Posted - Mar 27 2005 :  10:52:20 PM  Show Profile  Reply with Quote
The plain Is operator checks if both variables point to the same pointer. To check the class type of a variable, you use TypeOf, for example:

Private Sub Form_Load()

Dim a As New Class2

If TypeOf a Is Class1 Then
MsgBox "I'm class1"
ElseIf TypeOf a Is Class2 Then
MsgBox "I'm class2"
End If

End Sub

If you just need to know the common type of a variable, use VarType, or use TypeName to get the String name of a type.

There's no inheritance in VB6. It's really a limitation of COM (I think). The only alternative is using delegation, which is really just making a wrapper of an existing class' methods.

Implementation of interfaces is easy if you know how to use the IDE effectively. VB6 does not automatically add all the methods upon typing "Implements xxx". You have to click the Drop down list on top of the code window to select the methods one-by-one (it's really like adding new events). However, unlike events in standard objects where others are optional, you have to implement every one of the methods(Friend and Private methods do not count because there's no point of implementing invisible methods). Think of Implements as a contract, and you'll be fine.

The unique thing in VB6's interface implementation is that you're not just implementing an interface-- you are implementing the methods of an actual class. The original source of interface can have it's own implementation.

No public constants? Put them in modules. Think of modules as classes with static methods. If you really must put it in classes, put it as a function or property get. No point of arguing the compiler difference of a constant and a variable. It's moot and academic. The point is that you have the same effect.

User defined types (ones made by you), can only be passed as parameters in methods if you created them in ActiveX DLLs.

Man, if you've been using "VB for many years" and haven't known this, how much will you fare when using other "true OOP languages" ? You got to read and read about whatever language you're using. If I'm reading the lines correctly, you are saying that VB held you back from the wonderful world of OOP.

If you really must, use VB.Net, ok?


Edited by - hotrodx on Mar 28 2005 02:11:50 AM
Go to Top of Page

Threshold
Squire

USA
44 Posts

Posted - Mar 28 2005 :  09:15:32 AM  Show Profile  Reply with Quote
Thanks for the info.

I did happen to know most of what you said. The only new thing you told me was about the TypeOf keyword. My original programming language was VB6, but I never truly understood OOP until I took a Java class in college recently. Finally having a grasp of OOP concepts, I tried to use VB6 the way I used Java. It didn't work. I personally found Inheritance and Polymorphism extremely useful, but I can't use them in VB6. In that way, VB did hold me back from the "wonderful world of OOP" because it doesn't really support it, in a complete sense of the word.

Yes, I have thought about using VB.NET, but I have hundreds of pages of code that I simply can't afford to write again.

OTHER true OOP languages? VB is not a true OOP language. It has some aspects of OOP, but not enough to be called "true."

I fared just fine with Java, so I'm not worried about it.

Public constants in classes are very useful. Declaring public constants in a module is not organized and means you can't use the same constant name more than once. If, for example, you wanted a series of classes to all have a similar constant name but have a different value in each class, public class constants would be useful. Declaring an accessor method would work, but it forces you to go out of your way to do something simple.

Anyway, , I'm not going to start another argument about how VB doesn't do OOP right. This site is full of loyal VBers and I'm not going to push any buttons.

Thanks again for the help!

Life is short. They say "don't waste it, have fun." They're right, don't waste it...but DO redefine "fun."

Edited by - Threshold on Mar 28 2005 09:25:38 AM
Go to Top of Page

hotrodx
Squire

43 Posts

Posted - Mar 28 2005 :  11:09:05 AM  Show Profile  Reply with Quote
Just a clarification, I didn't exactly say VB was OOPL. I merely quoted you on some of your lines, and I'm referring other "true OOP languages" as OTHER languages, which excludes VB6. I will categorically state that VB is NOT OOPL. VB6 doesn't do OOP because it isn't OOP to begin with. It does use objects, but VB6 is not fully buzzword-compliant.

What I'm trying to say is that VB6 can never hold someone from OOP, unless it is the ONLY language you are using. But even then, the question is begged whether there's always a need to do OOP (in the academic sense).

Enjoy learning Java. I'm kinda reading a Java book right now, and I'm enjoying it (Java 2 in Plain English). I'm also trying to learn VB.Net right now, but I really, really hate using Intellisense on bloated classes (due to inheritance). It feels less organized than it should be. So here I have the most sought after feature for VB (something that it never had in previous versions), and I'm beginning to hate it.


P.S. VB6 does have Polymorphism. One method is the commonly used late-binding. You use a generic <Object> as a placeholder of two different objects (in turns). You just have to make sure they have the same methods (or at least the ones that needed to be called). For example, a Printer object has a PSet method, so does a Form. So you can both pass these objects to an Object variable and call PSet.

The problem is that because it's late-bound, the object type is only determined in run-time, so you can't use Intellisense.

The other method is the afforementioned Implements keyword. Because it is early-bound, you can use Intellisense and the class becomes something of a doppleganger-- it can be either class at a time. You simply <Set> it to either class type. And early-bound also means it's faster. You can use this technique to override an existing class' methods, to be passed as a parameter expecting the class implemented.

What more of Polymorphism do you have in mind?


quote:

"Declaring public constants in a module is not organized and means you can't use the same constant name more than once. If, for example, you wanted a series of classes to all have a similar constant name but have a different value in each class, public class constants would be useful."


Not entirely true. Just like classes with shared(vb.net) or static(java), you supply the class name. In VB6, you supply the module name to resolve ambiguity. So module1 can have constant Schwing, and module2 can have constant Schwing. You simply call it module1.Schwing to get the module1's constant, and module2.Schwing to get module2's. This works with simulated methods, too.

You may need to add a letter or two if you want use constants in modules to work with classes. For example if the class name is Checkbooks, the module could be CheckbooksM.

Edited by - hotrodx on Mar 28 2005 11:52:51 AM
Go to Top of Page

Threshold
Squire

USA
44 Posts

Posted - Mar 28 2005 :  2:22:53 PM  Show Profile  Reply with Quote
Okay, I see what you're saying.

Yes, you're right about VB having Polymorphism, but without Inheritance, it isn't quite "true." For example, in VB6, I can't define a parent class of Vehicle with children of Car and Bike. What this means in terms of Polymorphism is that I can't declare a variable as Vehicle and restrict its references to only Vehicle, Car, and Bike. Likewise, I can't use the "If TypeOf" statement to see if an object is within a certain inheritance tree branch. So the following code would produce the message box "Not a vehicle."

Class Vehicle
Class Car is a Vehicle
Class Bike is a Vehicle

  
Dim Obj1 As New Car  
  
If TypeOf Obj1 Is Vehicle Then
MsgBox "Is a vehicle"  
Else
MsgBox "Not a vehicle"  
End If
  


Ahh...I didn't know that public variables with the same name but in different modules was allowed. I suppose that could make some things easier.

I agree with you and think it's safe to say that, though it contains some aspects of OOP, VB6 is NOT an OOPL.

Life is short. They say "don't waste it, have fun." They're right, don't waste it...but DO redefine "fun."

Edited by - Threshold on Mar 28 2005 2:27:06 PM
Go to Top of Page

hotrodx
Squire

43 Posts

Posted - Mar 28 2005 :  7:53:25 PM  Show Profile  Reply with Quote
On the contrary, if you Implement the methods of Vehicle within both Car and Bike, they'd both be of type Vehicle. It is NOT inheritance, but the Car class will be classified as both Car and Vehicle, while Bike will be classified as both Bike and Vehicle. Obviously, Car will not be a Bike, and vice versa.

As I see it, you are having formal training in Java. How did you learn VB? If assuming you just learned VB by yourself, then you can appreciate the ease of VB. But just knowing how to make some programs in VB is NOT knowing VB. In time and *with formal training*, you could be good in Java, but the same goes for VB. Just because VB is easy to learn doesn't mean you don't have to be properly trained with it.

To emphasize that VB6 is not OOPL is like emphasizing Kasparov isn't good at wrestling. Kasparov (in real life) might have known something in wrestling, but that is not what makes him stand out. But within a group of Wrestlers, the measure of a man will be his wrestling capabilities. But among groups of chess players, it would be his chess capabilities.
Go to Top of Page

Threshold
Squire

USA
44 Posts

Posted - Mar 28 2005 :  11:00:54 PM  Show Profile  Reply with Quote
Yes, I had formal training in Java, but not in VB. I learned VB on my own with books and tutorials.

Wonderful! I am very glad to hear that interfacing affects the "is a" relationship with classes in VB6. That makes my life much easier.

Thanks!


Life is short. They say "don't waste it, have fun." They're right, don't waste it...but DO redefine "fun."
Go to Top of Page

hotrodx
Squire

43 Posts

Posted - Mar 29 2005 :  06:14:44 AM  Show Profile  Reply with Quote
No prob. Good luck with Java.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
VBGamer © Go To Top Of Page
This page was generated in 0.12 seconds. Snitz Forums 2000

Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.