1
我的Excel表創建一個包含各種圖層的地圖,顯示由線條,正方形,點和三角形組成的網絡。我有一個帶參數的每個創作功能,如使用類對象製作不同的形狀
cellLocation As Range '(takes a given cell location)
shapeType As String '(oval, triangle, rectangle)
Color '(red, black, whatever)
sizeFactor As Double '(factors the shape size as a function of cell's width)
我只是瞭解現在的類,但不知道是否班會在這種情況下是有用的,我怎麼可能用它們來簡化我的代碼,而不是具有6個不同參數等的功能。
本來我有這樣的功能:
Function CreateWell(cellRng As Range, wellName As String)
'creates a square of particular color, size, etc in the cellRng and names it wellName
Function CreateCompressor(cellRng As Range, compName As String)
'creates an oval of particular color, size , etc. similar to other func.
然後,因爲我的這些其中唯一的變化是顏色,大小,形狀等我試圖使整體功能約5:
Function CreateShape(cellRng As Range, shpName As String, _
shpColor As String, shpSize as double, shpType As string)
但這似乎是混亂的(太多的論據)。僱用類如何清理這種類型的代碼?
我會說不。原因是VBa/VB6在繼承方面沒有真正的類支持,並且對於你正在做的事情,使用泛型函數來做到這一點很好。此外,也許看看可選參數,所以一切都不必傳入。 – Jeremy
@傑瑞米感謝您的洞察力。你對類繼承有什麼意義,我對此並不熟悉。 – teepee
你可以查看這篇文章了。 https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) – Jeremy