tags:

views:

2093

answers:

9
+7  Q: 

Classes in VB6

Yeah, I'm stuck maintaining some legacy software. Does anyone know of any good tutorials on creating classes and whatnot in VB6? My Google-fu seems to be failing me. :-/

+8  A: 

VB6 classes are pretty basic. A good way to get a feel for the syntax is to fire up the class designer Add-on. Using that to design a simple dummy class will teach you pretty much everything you need to know.

Off the top of my head, properties are declared like this:

  'Global variable inside the class
  private m_Name as string

  public property get Name() as string
    Name = m_Name
  end sub
  public property let Name(sval as string)
    m_name = sval
  end sub

You can remove either the get or let to make a property read-only or write-only.

Then you can access the properties in this fashion

dim myClass as new ClassName

myClass.Name = "Mark"
debug.print myClass.Name

Methods can be Public or Private. I don't remember the syntax for declaring events off the top of my head.

Mark Biek
Don't forget "Property Set" for any objects. "Set x = y" always gets me when I go back to VB6. "Public Event MyEventName(blah as String)" declares an event with a string that is passed to the event handler. MyEventName.Raise("boo!") will fire it.
Hamish Smith
+2  A: 

Remember that you don't have constructors, so you need to use the Class_Initialize() method. I've just gone back to a VB6 position and found this one of the most irritating things (been spoiled by C# the last few years, but the rewrite will be fun!)

AlexCuse
+6  A: 

I can help your google searches. You should start your searches with -".net" and that should drastically reduce all the .Net results.

Here are some tutorials that I found:

Tutorial: Basic introduction to classes

Tutorial: Advanced classes

OO Development Tutorial

bruceatk
+2  A: 

If you're happy reading books, Francesco Balena's book "Programming Microsoft Visual Basic 6.0" is excellent for experienced programmers who already know what variables are, etc, and just want to learn VB. You should be able to pick it up secondhand fairly cheaply somewhere like AbeBooks

MarkJ
+1  A: 

And remember that wen you using it you don't have inheritance, at least i've never found a way to do that, if you find it, please tell me, because i have the same problem that you do, maintaining old software.

for doing methods you declare as functions, using the modifier public or private

There's only interface inheritance (i.e. interfaces), no implementation inheritance.
Konrad Rudolph
You can simulate implementation inheritance with some work - eg see this link. ihttp://www.lhotka.net/Article.aspx?id=5f76a91c-5a75-49e1-9379-6d2807653b68
MarkJ
+2  A: 

Also, in case you are writing .DLLs that are to be exposed to other applications, be aware of how VB6 manages your classes as COM objects. Basically, for instance if you create a class named MyClass, VB6 also generates an interface named (underscore)MyClass, which is then used as the default interface for MyClass coClass.

This only gets interesting when consuming your classes from, say, C++, otherwise you can forget it.

petr k.
+2  A: 

As well as classes, something that's not very well known is that you can use interfaces in VB6. This makes some simple OO styles possible.

Simply, you define your interface like this:

Public Sub Method1()
End Sub

Public Property Get Prop1() As Boolean
End Property

(i.e. with empty method and property stubs), then the classes that implement the interface like this:

Public Sub InterfaceName_Method1()
    'implementation
End Sub

Public Property Get Prop1() As Boolean
    'implementation
End Property

Each interface and implementation needs to be in a separate code file.

For more detail, this web page is a good reference.

Edit: As MarkJ commented, it is possible to spoof up inheritance using aggregation. He gave quite a nice article here.

Ant
You can use interfaces and aggregation to get a poor man's implementation inheritance: http://www.lhotka.net/Article.aspx?id=5f76a91c-5a75-49e1-9379-6d2807653b68
MarkJ
Yeah it's a good point - I'll add that into this answer. Thanks :)
Ant
+3  A: 

The best reference I ever found for VB6 was by Dan Appleman. The second best was Hardcore Visual Basic by Bruce McKinney. The second one has the advantage that it's available online. Sounds like you're interested in Chapter 3: An Object Way of Basic.

Don Kirkby
+2  A: 

Presumably you've read the manual? As well as reference material there are useful overviews. The "Programming with objects" chapter of the "Programmers Guide" covers classes.

Some parts of the overview documentation are aimed at beginners, but you can skim those bits.

MarkJ