0

I would like to obtain this set of properties with 1 class + 2 chained-classes:

mytable.EntireRange
mytable.DataBodyRange
mytable.HorizontalHeaderRange
mytable.VerticalHeaderRange

mytable.col(strTitle).num
mytable.col(strTitle).firstDataCell
mytable.col(strTitle).lastDataCell

mytable.row(strTitle).num
mytable.row(strTitle).firstDataCell
mytable.row(strTitle).lastDataCell

What's the best way to organize (and name) these classes and properties?


My current solution to add the chained properties is:
- I named the three classes "mytable", "mytable_col", "mytable_row" to keep a visible relationship in the Project Explorer
- inside "mytable", I added this:

Property Get col(strTitle As String) As mytable_col
    Set col = New mytable_col
    col.Initialize = Me ' passes the parent object
 End Property
Property Get row(strTitle As String) As mytable_row
    Set row = New mytable_row
    row.Initialize = Me ' passes the parent object
 End Property

Now... given that the three properties ("num", "firstDataCell", "lastDataCell") of the two parallel chained classes "col" and "row" share the same logic with little variations, I'd prefer to not put their complete code separated in 6 properties, three in the "row" class and three in the "col".

I ideally would to create just 3 properties/functions (passing a "row" or "col" as argument for the variations) instead than six.
How should I do? Is it possible, without to put them in an additional module?

BigBen
  • 46,229
  • 7
  • 24
  • 40
6diegodiego9
  • 503
  • 3
  • 14
  • Two requests: 1) When using the VBA tag, can you also use the appropriate application, e.g. excel? 2) You've received some well crafted answers, such as [this one](https://stackoverflow.com/a/58101500/9245853) or [this one](https://stackoverflow.com/a/57894889/9245853), to previous questions. Can you go ahead and accept them by clicking the check next to them, if they've satisfactorily answered your issue? It is a token of recognition to the answerer as well as a sign that the question is closed. Much appreciated (note, I didn't provide either answer but both are deserving of recognition). – BigBen Sep 25 '19 at 17:12
  • Can you share the complete code that you have so far, or at least a minimal example so we don't have to recreate it? – PeterT Sep 25 '19 at 18:05
  • "I named the three classes "mytable", "mytable_col", "mytable_row" to keep a visible relationship in the Project Explorer" - exactly what is wrong with `Table`, `TableColumn` and `TableRow`? Every single class you ever come across in VBA code, is `PascalCase`, as is every single one of its members. `lower_snake_case` not only looks weird and clashes with everything else, it will cause compiler errors if/when you start dealing with interfaces. – Mathieu Guindon Sep 25 '19 at 18:26
  • 1
    That said this question would be more appropriate on [codereview.se], **if** you actually included the whole code. – Mathieu Guindon Sep 25 '19 at 18:31
  • 1
    `row.Initialize = Me` looks very suspicious (no `Set` keyword for an object assignment?). Should probably be `row.Initialize Me`, but impossible to tell without seeing the actual code. – Mathieu Guindon Sep 25 '19 at 18:42
  • Thanks Mathieu, I didn't know Code Review. I will try to reorganize the question for that website or to split the question into multiple simpler conceptual questions here. – 6diegodiego9 Oct 05 '19 at 14:42

0 Answers0