Domino 9 und frühere Versionen > ND6: Entwicklung
Feldinhalt per Script mit Replace verändern
Glombi:
Du hast meine Dimensionierungen weggelassen ;)
Dim oldname(0) As String
Dim newname(0) As String
Was Replace mit Arrays veranstaltet sagt die Hilfe:
Replace(sourceArray as Variant, findArray as Variant, replacementArray as Variant[, start as Integer[, count as Integer[, compMethod as Integer]]]) as Variant
Elements
sourceArray
Array of type String containing the strings to be modified
replaceArray
Array of type String containing the words or phrases to be replaced
replacementArray
Array of type String containing the replacement words or phrases
start
optional Integer specifying the character position to start at in each String
count
optional Integer specifying the maximum number of replacements to make.
compMethod
Optional Integer specifying the type of comparison to use when searching for the delimiter, if the elements are strings.
Number Comparison Mode
0 case sensitive, pitch sensitive
1 case insensitive, pitch sensitive
4 case sensitive, pitch insensitive
5 case insensitive, pitch insensitive
If you omit compMethod, the default comparison mode is the mode set by the Option Compare statement for this module. If there is no statement for the module, the default is case sensitive and pitch sensitive.
Return value
Replace returns an Array of type String that contains sourceArray, where any values in replaceArray have been replaced by the corresponding values in replacementArray.
Usage
Replace searches the String in sourceArray for the String in replaceArray. If a match is found, the substring is replaced with a corresponding substring from replacementArray. Each String in replaceArray is scanned against each String in sourceArray as modified by prior substitutions. Replace is case sensitive.
If no matches are found, then a copy of sourceArray is returned.
If more strings are specified in replaceArray than in replacementArray, the extra strings in replaceArray are replaced with the last string in replacementArray. Extra strings in replacementArray are ignored.
Andreas
theBastian:
--- Zitat von: Glombi am 22.07.05 - 09:49:02 ---Du hast meine Dimensionierungen weggelassen ;)
--- Ende Zitat ---
Sorry, waren unter Declarations und ich hatte vergessen die zu kopieren.
--- Code: ---Sub Initialize
Dim ws As New NotesUIWorkspace
Dim session As New NotesSession
Dim db As notesdatabase
Dim collection As NotesDocumentCollection
Dim doc As NotesDocument
Set db = session.CurrentDatabase
Set collection = db.UnprocessedDocuments
Set doc = collection.GetFirstDocument()
'Get the names from the user
Dim oldname(0) As String
Dim newname(0) As String
oldname(0) = ws.Prompt (PROMPT_OKCANCELEDIT, "Old user name", "Type or change the old name in the box below.")
newname(0) = ws.Prompt (PROMPT_OKCANCELEDIT, "New user name", "Type or change the new name in the box below.")
While Not(doc Is Nothing)
'Get the Agents field
Dim oldAgents As Variant
oldAgents = doc.Agents
'Change the username
newAgents = Replace (oldAgents, oldname, newname)
'Set the Agents field
doc.Agents = newAgents
Call doc.Save( True, True )
Set doc = collection.GetNextDocument(doc)
Wend
End Sub
--- Ende Code ---
Hilfe habe ich gelesen und so verstanden, dass das gesamte Array verglichen wird. Egal wieviele Elemente und dass dann einzelne Elemente replaced werden.
Aber es werden die Arrays als Variant erwartet. Warum dann Dim oldname as String?
Sebastian
flaite:
nicht Dim oldname as String. Sondern Dim oldname(0) as String. Das ist ein somit als Array von String deklariert und nicht als String. Das ist etwas anderes.
Variant ist eine Art Superklasse von Arrays (und anderen Objekt-Datentypen).
JEDER String-Array is-a Variant.
Eben nur ein speziellerer Variant.
Wenn ich jetzt noch den Dreh mit der Liskov Substitution hinkriegen würde. >:(
Aber ich lasse das und würde vielleicht Unsinn reden.
Der Kontrakt von der Signatur der Methode sagt ja:
replace (Variant, Variant, Variant).
Der zweite und dritte Parameter, die du übergibst sind String-Arrays.
Oben habe ich festgestellt, dass String-Array is-a-Variant.
Also ist der Kontrakt der Signatur vom Caller (=dein Skript) erfüllt.
Mich wundert nur, dass bei replace (Variant, String, String) der Compiler keinen Fehler meldet.
Denk dir Variant einfach nicht als Datentyp sondern als Klammer über mehrere Datentypen. D.h. eine als Variant deklarierte Variable kann so einiges beinhalten. Unter anderem auch Arrays.
theBastian:
Danke Dir.
Könnte sein, dass ich es verstanden habe.
Zumindest bis zum nächsten Problem. ;)
cu
Sebastian
flaite:
Liskov Substition Principle:
FUNCTIONS THAT USE POINTERS OR REFERENCES TO BASE CLASSES MUST BE ABLE TO USE OBJECTS OF DERIVED CLASSES WITHOUT KNOWING IT.
Variant ist Base Klasse zu Array_Of_Strings. Array_Of_Strings ist ein Spezialfall von Variant.
Die Funktion replace muß also in der Lage sein, statt Variant array_of_strings zu verarbeiten.
Interessanterweise ist die Funktion replace ein Verstoß gegen Liskov Substitution. In Variant kann ja auch z.B. auch ein Objekt der Klasse NotesDocument verwiesen werden. Damit würde aber die Funktion nicht funktionieren. Sie kann also bestimmte Objekte von Child-Klassen von Variant nicht benutzen. Aber so ist das eben in Basic.
http://www.objectmentor.com/resources/articles/lsp.pdf
Navigation
[0] Themen-Index
[#] Nächste Seite
[*] Vorherige Sete
Zur normalen Ansicht wechseln