4

I apologize in advance if this is a "downvotable" question but I really need help in understanding how to know what to make classes out of in a VBA project.

NOTE: I am not asking how to code a class. I am asking how to determine what to make a class for.

Example

I want to draw shapes on a PowerPoint slide. 2 of the 3 kinds of shapes I need to draw have the same properties and methods. Would I create one class called CShape or 3 classes: CCircle, CSquare, CRectangle? Furthermore, lines are considered shapes in PowerPoint. Should I add a CLine class or just lump everything together in CShape?

I have searched Google for Methodologies on determining classes in VBA and the like but can not find anything. Does anybody out there have a method or way they go about determining how to create classes for their projects (and the reasons behind it)?

Also, I am just getting into classes and interfaces and this is the first project I am using them in. Thanks!

UPDATE

I did find this: When to use a Class in VBA?

Brian
  • 2,078
  • 1
  • 15
  • 28
  • I would recommend to start using some .net language like e.g. c# where you need classes all the time and this way you will 'understand' and learn what they are good for and how to identify them. Then when you move back to VBA you will already 'know'. – Daniel Dušek Dec 02 '17 at 21:59
  • @dee Thank you! I actually did buy a book entitled _The C# Player's Guide Second Edition_ and started reading it again. Maybe that will give me some insight on how to make class decisions. – Brian Dec 07 '17 at 11:19

3 Answers3

2

When you work with code that can be reusable, this is the perfect time to use classes.

You need to create classes that have procedure to handle errors and possible wrong entries. As much robust you can create your code, your class will work as you expect. I worked with PowerPoint and build some classes to manipulate document properties, another to manipulate slides, another to resize shapes.

You can find several classes examples from Chip Pearson site and you can figure out how to make your own classes.

http://www.cpearson.com/Downloads/Downloads.aspx

Hope this helps.

Community
  • 1
  • 1
1

VBA (which is almost the same as Visual Basic 6) doesn't support inheritance.

So you have some options, using Interfaces would the normal way to deal with this, so probably three classes each on which implements the IDrawable interface. You may want to have a base class that deals with these features and have the outer classes call down to the base.

IDrawable might have public members for drawing, position, color and penwidth with the other parameters being part of the individual classes.

In particular you should read up on the Liskov substitution principle. I've seen examples of it using rectangles and squares as examples of what not to do.

See this older question for example Is deriving square from rectangle a violation of Liskov's Substitution Principle?

PeterI
  • 472
  • 3
  • 17
  • I actually do have an interface called `IShape` with a method called `Draw`. But it is difficult to reach the other properties of each class that are not a part of the interface when looping through a collection of different objects. More generally, I just want to know if there is a method for deciding on classes. It may be too subjective of a question. – Brian Nov 29 '17 at 13:44
0

I have been using VBA for over 20 years, and made very little use of class modules.
The most useful cases for me were to build classes representing some structured spreadsheets or complex text files (with logical rows spanning over several physical lines) you may have to query or browse.
You can then implement a .MoveNext method and some properties like .EoF, .Price, .Rate to read them sequentially, being able to reuse that class everywhere you use that specific input file 'layout', and having the complex logic encapsulated in the class.

iDevlop
  • 24,841
  • 11
  • 90
  • 149