MOCKSTACKS
EN
Questions And Answers

More Tutorials









VBA Object Oriented

Abstraction


Abstraction levels help determine when to split things up.

Abstraction is achieved by implementing functionality with increasingly detailed code. The entry point of a macro should be a small procedure with a high abstraction level that makes it easy to grasp at a glance what's going on:

Public Sub DoSomething()
 With New SomeForm
 Set .Model = CreateViewModel
 .Show vbModal
 If .IsCancelled Then Exit Sub
 ProcessUserData .Model
 End With
End Sub

The DoSomething procedure has a high abstraction level: we can tell that it's displaying a form and creating some model, and passing that object to some ProcessUserData procedure that knows what to do with it - how the model is created is the job of another procedure:

Private Function CreateViewModel() As ISomeModel
 Dim result As ISomeModel
 Set result = SomeModel.Create(Now, Environ$("UserName"))
 result.AvailableItems = GetAvailableItems
 Set CreateViewModel = result
End Function

The CreateViewModel function is only responsible for creating some ISomeModel instance. Part of that responsibility is to acquire an array of available items - how these items are acquired is an implementation detail that's abstracted behind the GetAvailableItems procedure:

Private Function GetAvailableItems() As Variant
 GetAvailableItems = DataSheet.Names("AvailableItems").RefersToRange
End Function

Here the procedure is reading the available values from a named range on a DataSheet worksheet. It could just as well be reading them from a database, or the values could be hard-coded: it's an implementation detail that's none of a concern for any of the higher abstraction levels.


Conclusion

In this page (written and validated by ) you learned about VBA Object Oriented . What's Next? If you are interested in completing VBA tutorial, your next topic will be learning about: VBA Creating a Custom Class.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.