Page 2 of 2
Re: [BASIC] Object-Oriented Programming
Posted: Wed Feb 13, 2019 5:57 pm
by eminkovitch
Hello!
Now that I've coded successfully
using the object-oriented methodology, naturally a question arises
- can I export classes so that they can be shared between applications? It seems that copying and pasting a class into MyMacros / Standard library, where subs and functions are visible to all applications, does not work for classes. Can anyone share any insight on this?
Thanks in advance,
Eliot
Re: [BASIC] Object-Oriented Programming
Posted: Wed Feb 13, 2019 6:19 pm
by Zizi64
Can you upload your sample code here??
Re: [BASIC] Object-Oriented Programming
Posted: Wed Feb 13, 2019 6:25 pm
by eminkovitch
This is a class that works inside a CALC application, I am looking to make it available to all CALC applications:
Code: Select all
REM ****************************************************************************
REM *** Class Cell Annotations
REM ***
REM *** Purpose: (a) Creates comments in a selected CALC cell
REM *** (b) deletes a comment from selected CALC cell
REM ***
REM *** Parent Class: PracticeTime
REM *** Child Class(es): N/A
REM ***
REM ****************************************************************************
Option Compatible
Option ClassModule
Option Explicit
REM ============================================================================
REM === Private variables
private _AnnotationText as string
REM ============================================================================
REM === Constructor & Destructor
REM ===
private sub Class_Initialize()
_AnnotationText = getCellComment()
end sub
private sub Class_Terminate()
_AnnotationText = ""
end sub
REM ============================================================================
REM === Set private variables
Public Property Let AnnotationText (ByVal pValue As String)
_AnnotationText = pValue
End Property
REM ============================================================================
REM === Get private variables
Public Property Get AnnotationText As String
AnnotationText = _AnnotationText
End Property
REM ============================================================================
REM === Public Methods
REM ===
REM ----------------------------------------------------------------------
REM --- Set text, shape, transparency and color of _Annotation (comment)
REM --- for a selected cell
REM --- Project# P002m
REM ---
public Sub setCellComment
dim oDoc,oSheet,oCell,oCellAddr,oAnnotations,_
oAnnotation,oAnnotationShape as object
dim lenComment,widthBox as integer
oDoc = ThisComponent
oSheet= oDoc.getcurrentcontroller.activesheet
oCell = oDoc.getCurrentSelection
lenComment = len( _AnnotationText )
if lenComment > 0 then
select case lenComment
case > 501:
widthBox = 12000
case 251 to 500:
widthBox = 10000
case 101 to 250:
widthBox = 8000
case 51 to 100:
widthBox = 6000
case 0 to 50:
widthBox = 4000
end select
oCellAddr = oCell.getCellAddress
oAnnotations = oSheet.getAnnotations
'/Create a new _Annotation to the active cell
oAnnotations.insertNew(oCellAddr, _AnnotationText)
'/Get the newly created _Annotation for modify properties
oAnnotation = oCell.getAnnotation
'/Set it "allways visible":
oAnnotation.setIsVisible false
'/Set color, transparence, fill style, inner margin
oAnnotationShape = oAnnotation.getAnnotationShape
oAnnotationShape.FillBackground = True
oAnnotationShape.FillColor = RGB(251, 246, 99)
oAnnotationShape.FillStyle = 1 '(0: nothing; 1: solid color; 2: gradient; 3: lined)
oAnnotationShape.FillTransparence = 15
oAnnotationShape.TextLeftDistance = 200
oAnnotationShape.TextLowerDistance = 200
oAnnotationShape.TextRightDistance = 200
oAnnotationShape.TextUpperDistance = 200
'/Set size of the _Annotation
Dim aSize As New com.sun.star.awt.Size
asize.Height = 1000
asize.Width = widthBox
oAnnotationShape.Size = asize
oAnnotationShape.SizeProtect = False
'/Set absolute position of the _Annotation
'Dim apoint As New com.sun.star.awt.Point
'apoint.x = 1000
'apoint.y = 1000
'oAnnotationShape.setPosition apoint
else
deleteComments
end if
End Sub
REM ============================================================================
REM === Private Methods
REM ===
REM ----------------------------------------------------------------------
REM --- Read the _Annotation from selected cell
REM ---
private function getCellComment()
getCellComment = ThisComponent.getCurrentSelection().getAnnotation().string
end function
private sub deleteComments
dim document as object
dim dispatcher as object
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Flags"
args1(0).Value = "N"
dispatcher.executeDispatch(document, ".uno:Delete", "", 0, args1())
end sub
Re: [BASIC] Object-Oriented Programming
Posted: Thu Feb 14, 2019 1:34 am
by JeJe
If you mean you want the class instance to be available from another library - you can put a function in a neighbouring module in the library where the class module is. Something like:
function getClassModuleInstance()
dim classinstance as new yourclassname
getClassModuleInstance = classinstance
end function
then you can call it from code in another library with:
classinstance= getClassModuleInstance()
(this is already asked/discussed earlier in this thread)