Domino 9 und frühere Versionen > ND6: Entwicklung

Feldinhalt per Script mit Replace verändern

(1/4) > >>

theBastian:
Hi,

ich versuche gerade ein Feld per Script zu verändern. Ich frage dazu den alten und den neuen Namen ab und versuche dann den Inhalt des Feldes zu verändern, falls der Name darin auftaucht.


--- Code: ---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()

Dim oldname As String
Dim newname As String
oldname = ws.Prompt (PROMPT_OKCANCELEDIT, "Old user name", "Type or change the old name in the box below.")
newname = ws.Prompt (PROMPT_OKCANCELEDIT, "New user name", "Type or change the new name in the box below.")

While Not(doc Is Nothing)
Dim oldAgents As Variant
oldAgents = doc.Agents

newAgents = Replace (oldAgents, oldname, newname)
doc.Agents = newAgents
Call doc.Save( True, True )
Set doc = collection.GetNextDocument(doc)
--- Ende Code ---

Leider gelingt mir das ganze nicht so recht.
Der Debugger zeigt mir zwar die Daten an aber Replace macht garnichts.  :(

Hat jemand von Euch vielleicht eine Idee?

cu
Sebastian

flaite:
Es kann sein, dass oldname und newname explizit Arrays sein müssen.
Die Funktion replace ist wie folgt spezifiziert:

--- Code: ---Replace(sourceArray as Variant, findArray as Variant, replacementArray as Variant[, start as Integer[, count as Integer[, compMethod as Integer]]]) as Variant

--- Ende Code ---

findArray und replacementArray sind bei dir aber Strings.
Vielleicht so:


--- Code: ---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.")

--- Ende Code ---

Ja. Habs ausprobiert:

1. Version:

--- Code: ---Sub Initialize

Dim arExist (1) As String
Dim arFind(0) As String
Dim arReplace(0) As String


Dim res As Variant

arExist(0) = "rot"
arExist(1) = "blau"

'arFind(0) = "blau"
'arReplace(0) = "gelb"


res = Replace(arExist, arFind, arReplace)

Forall x In res
Print x
End Forall
End Sub

--- Ende Code ---
funktioniert.

2. Kein Fehler vom Kompiler oder Runtime gemeldet. Trotzdem Effekt wie bei dir:

--- Code: ---Sub Initialize

Dim arExist (1) As String
'Dim arFind(0) As String
'Dim arReplace(0) As String
Dim arFind As String
Dim arReplace As String


Dim res As Variant

arExist(0) = "rot"
arExist(1) = "blau"

'arFind(0) = "blau"
'arReplace(0) = "gelb"
arFind = "blau"
arFind="gelb"


res = Replace(arExist, arFind, arReplace)

Forall x In res
Print x
End Forall



End Sub

--- Ende Code ---

Das generiert einen Kompilierfehler:

--- Code: ---Sub Initialize

Dim arExist (1) As String
Dim arFind As Variant
Dim arReplace As Variant


Dim res As Variant

arExist(0) = "rot"
arExist(1) = "blau"

arFind = "blau"
arFind="gelb"


res = Replace(arExist, arFind, arReplace)

Forall x In res
Print x
End Forall



End Sub

--- Ende Code ---

Glombi:
Replace erwartet jeweils Arrays. Daher würde ich es mal wie folgt versuchen:

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()

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)
Dim oldAgents As Variant
oldAgents = doc.Agents

newAgents = Replace (oldAgents, oldname, newname)
doc.Agents = newAgents
Call doc.Save( True, True )
Set doc = collection.GetNextDocument(doc)

Andreas

flaite:
genau das gleiche, was ich auch gesagt habe  ;D

flaite:
Ich finde das übrigens ziemlich unlogisch von der Sprache Basic her.
Ich kann einer Variant-Variablen kein String zuweisen. Weil String kein Objekt ist? und Variant nur Objekttypen aufnimmt?
Wenn dies so ist.
Dann müsste schon der Compiler einen Funktionsaufruf

--- Code: ---
replace (Variant, string, String)

--- Ende Code ---
zurückweisen,
wenn die korrekte Funktionssignatur

--- Code: ---replace (variant, variant, variant)

--- Ende Code ---
heisst
und es eine Funktion mit der Signatur: 

--- Code: ---
replace (Variant, string, String)

--- Ende Code ---
nicht gibt.

Navigation

[0] Themen-Index

[#] Nächste Seite

Zur normalen Ansicht wechseln