• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Object Oriented VBA

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand the title of the forum is Visual Basic but as I've seen other people have posted Excel and macros, I suspect it extends to VBA. My question relates to writing VBA in an object oriented fashion.

For example, this shows Compostion (to make up for the lack of Inheritance in VBA):



Note in ClsVolume, I have:



I know it's a minor point, as both return the same (correct) result but would one be preferable over the other?

I am aware of the following:

   

though have not really seen it  / aware it's been applied in VBA.

My main obstacle lies in the preparation stage, ie BEFORE coding takes place. I don't know how to model the task.

   

(My example above of Area vs Volume is one simple example (I think) showng a base class (Area) and its derived class (Volume)).

I have read a lot of OO but the material tends to cover other languages. The closest I've come across is RubberDuck (https://rubberduckvba.com/) but I found it hard to understand.

The standard response for writing OO is that it's good for large projects and makes things scalable.

Well every program starts off small and since nobody knows if it'll grow bigger, surely that means all programs should be written in an OO way?

Can someone provide examples of a VBA program written in both styles, ie "the usual way" vs in an OO way, then point out why OO might be better?

Thanks



 
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does VBA even work anymore? VB itself has been dead for half an æon.

VB.Net may actually have some OOP support, since it's based on the same language environment as C#.net. But I haven't programmed for Windows since, well VB days.

Welcome to the Ranch!
 
dug doogie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
VBA is dying but not totally dead.

Regardless of the language used, how should one plan when writing an OO program?


 
Tim Holloway
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you might find that difficult. C and Python managed to become OOP, but only modifying the core language (C++ and Python's hacked "class" support).

You could kind of fake it by storing related data in a data structure, but that doesn't allow for member functions, so the best you could do would be to set up a standard function naming convention that includes the "object" "class" as part of the procedure name. The original C++ was a pre-processor that converted C++ to C and it did something similar, although the C++ front-end managed the actual OOP functionality.

If your primary concern for going OOP would be to aid in expandability and maintainability, though, sticking with a dead language and pretending OOP is a lot of work for a dead end. Keep the VBA as long as you can, use something that's actually OOP for new stuff.
 
dug doogie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand VBA is not fully OO and it's not the ideal language to learn OO.

The motivation is I have to support VBA code and sometimes, I have come across VBA code that has been written in a (convoluted) OO way, possibly because the programmer was origianlly C# conversant and thought VBA needed to be written in a similar fashion.

I have trawled through the internet and the best I've seen is these kind of info:

https://morsagmon.com/blog/object-oriented-programming-with-excel-vba-part-1/

https://www.youtube.com/watch?v=vUg-NH-Wqss

Unfortunately they don't go through the process in detail, ie the planning of the number of classes that will be required before coding starts.

Perhaps a high level UML diagram would have been more informative but how many VBA programmers use that?



 
Tim Holloway
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah. Planning and coding are two different things.

UML isn't much used these days, but if it helps you, go for it. I like the ArgoUML app myself, if it's still available and functioning.

Another popular technique involve using paper filecards, one per class.

I myself do a lot of stuff back-of-the-envelope, but as the process develops, I employ something like Knuth's Literate Programming style, where I write the comments that describe a class and what it does, then flesh out the class itself over time.
 
Don't listen to Steve. Just read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic